Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(android): log the address and sender of a page that fails with an HTTP error

Maurice493571c55966
diff --git a/client/android/app/src/main/java/com/liketrek/trek/PageErrorLog.java b/client/android/app/src/main/java/com/liketrek/trek/PageErrorLog.java
new file mode 100644
index 000000000..b1601772f
--- /dev/null
+++ b/client/android/app/src/main/java/com/liketrek/trek/PageErrorLog.java
@@ -0,0 +1,60 @@
+package com.liketrek.trek;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+/**
+ * The log line for a page of the server that came back with an error status.
+ * "HTTP 503" on the address screen cannot tell the server, a proxy in front of
+ * it and something on the phone apart, so the line names the page and the
+ * headers that say who answered (#3014).
+ */
+final class PageErrorLog {
+
+ /** The headers that name whoever answered a page, in the order they are logged. */
+ private static final String[] SENDER_HEADERS = { "Server", "Via", "CF-Ray", "Content-Type" };
+
+ private PageErrorLog() {}
+
+ /**
+ * For example "Page answered HTTP 503 Service Unavailable for
+ * https://trek.example.com/login (server: cloudflare, cf-ray: 8f1a2b3c4d5e6f70-FRA)".
+ * The address goes without its query and fragment, where a sign-in page
+ * carries a challenge or a code. A header the answer did not have is left
+ * out, and an answer with none of them says so: no server sends a page
+ * without at least its type, so that points at something on the phone.
+ */
+ static String describe(String url, int status, String reason, Map<String, String> headers) {
+ StringBuilder line = new StringBuilder("Page answered HTTP ").append(status);
+ if (reason != null && !reason.trim().isEmpty()) line.append(' ').append(reason.trim());
+ line.append(" for ").append(withoutQuery(url));
+ List<String> senders = new ArrayList<>();
+ for (String name : SENDER_HEADERS) {
+ String value = header(headers, name);
+ if (value != null && !value.trim().isEmpty()) senders.add(name.toLowerCase(Locale.ROOT) + ": " + value.trim());
+ }
+ line.append(senders.isEmpty() ? " (no server headers)" : " (" + String.join(", ", senders) + ")");
+ return line.toString();
+ }
+
+ /** A header by name, whatever case the answer wrote it in. */
+ private static String header(Map<String, String> headers, String name) {
+ if (headers == null) return null;
+ for (Map.Entry<String, String> entry : headers.entrySet()) {
+ if (name.equalsIgnoreCase(entry.getKey())) return entry.getValue();
+ }
+ return null;
+ }
+
+ private static String withoutQuery(String url) {
+ if (url == null) return "";
+ int end = url.length();
+ int query = url.indexOf('?');
+ if (query >= 0) end = query;
+ int fragment = url.indexOf('#');
+ if (fragment >= 0 && fragment < end) end = fragment;
+ return url.substring(0, end);
+ }
+}
diff --git a/client/android/app/src/main/java/com/liketrek/trek/TrekWebViewClient.java b/client/android/app/src/main/java/com/liketrek/trek/TrekWebViewClient.java
index e7e7031d0..8cdc461e2 100644
--- a/client/android/app/src/main/java/com/liketrek/trek/TrekWebViewClient.java
+++ b/client/android/app/src/main/java/com/liketrek/trek/TrekWebViewClient.java
@@ -202,14 +202,23 @@ class TrekWebViewClient extends BridgeWebViewClient {
* none, as Cloudflare does with a 403, sends the app to the address screen,
* which says so and offers to add one. Any other 4xx page (a proxy's block
* page, a certificate it refused) stays up with the bar and its way out, so
- * the app is never stuck on a page without TREK's UI.
+ * the app is never stuck on a page without TREK's UI. Each one is logged
+ * with its address and whoever answered it (PageErrorLog).
*/
@Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
int status = errorResponse.getStatusCode();
if (!request.isForMainFrame()) {
super.onReceivedHttpError(view, request, errorResponse);
- } else if (status >= 500 || (status >= 400 && wantedCertificate(request.getUrl()))) {
+ return;
+ }
+ Log.w("TrekShell", PageErrorLog.describe(
+ request.getUrl().toString(),
+ status,
+ errorResponse.getReasonPhrase(),
+ errorResponse.getResponseHeaders()
+ ));
+ if (status >= 500 || (status >= 400 && wantedCertificate(request.getUrl()))) {
TrekServerStore.setFailure(view.getContext(), "HTTP " + status);
onUnreachable.run();
} else if (status >= 400) {
diff --git a/client/android/app/src/test/java/com/liketrek/trek/PageErrorLogTest.kt b/client/android/app/src/test/java/com/liketrek/trek/PageErrorLogTest.kt
new file mode 100644
index 000000000..436b930ea
--- /dev/null
+++ b/client/android/app/src/test/java/com/liketrek/trek/PageErrorLogTest.kt
@@ -0,0 +1,51 @@
+package com.liketrek.trek
+
+import org.junit.Assert.assertEquals
+import org.junit.Test
+
+/** The log line for a page that came back with an error status (#3014). */
+class PageErrorLogTest {
+
+ @Test
+ fun namesThePageItsStatusAndWhoAnswered() {
+ val line = PageErrorLog.describe(
+ "https://trek.example.com/login?redirect=%2F#top",
+ 503,
+ "Service Unavailable",
+ mapOf(
+ "server" to "cloudflare",
+ "CF-RAY" to "8f1a2b3c4d5e6f70-FRA",
+ "content-type" to "text/html; charset=UTF-8",
+ "Set-Cookie" to "session=secret",
+ ),
+ )
+
+ // Any case of a header name is read, only the naming headers are logged,
+ // and the query with its sign-in state is left off.
+ assertEquals(
+ "Page answered HTTP 503 Service Unavailable for https://trek.example.com/login " +
+ "(server: cloudflare, cf-ray: 8f1a2b3c4d5e6f70-FRA, content-type: text/html; charset=UTF-8)",
+ line,
+ )
+ }
+
+ @Test
+ fun anAnswerWithoutHeadersSaysSo() {
+ assertEquals(
+ "Page answered HTTP 503 for https://trek.example.com/ (no server headers)",
+ PageErrorLog.describe("https://trek.example.com/", 503, null, emptyMap()),
+ )
+ assertEquals(
+ "Page answered HTTP 502 Bad Gateway for (no server headers)",
+ PageErrorLog.describe(null, 502, "Bad Gateway", null),
+ )
+ }
+
+ @Test
+ fun aFragmentBeforeAQuestionMarkEndsTheAddressThere() {
+ assertEquals(
+ "Page answered HTTP 500 for https://trek.example.com/trips/4 (via: 1.1 proxy)",
+ PageErrorLog.describe("https://trek.example.com/trips/4#a?b", 500, " ", mapOf("Via" to "1.1 proxy")),
+ )
+ }
+}