Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(geomap): say once what a failing tile server did, without a stack
Elian Dorande9ca81aadab
diff --git a/apps/client/src/widgets/collections/geomap/map.spec.ts b/apps/client/src/widgets/collections/geomap/map.spec.tsindex 63cb5af9a0..542ca06f9f 100644--- a/apps/client/src/widgets/collections/geomap/map.spec.ts+++ b/apps/client/src/widgets/collections/geomap/map.spec.ts@@ -9,7 +9,7 @@import type { LayerSpecification, StyleSpecification } from "maplibre-gl";import { describe, expect, it } from "vitest";-import { keepAdditions, styleContents } from "./map";+import { keepAdditions, styleContents, summarizeMapError } from "./map";/** A style with the given sources and layers, as MapLibre serializes one. */function style(sources: string[], layers: string[]): StyleSpecification {@@ -85,3 +85,48 @@ describe("keepAdditions", () => {expect(transformed).toBe(next);});});++/**+ * The console, which a tile server refusing us used to fill.+ *+ * MapLibre prints an error nobody listens for itself, with the stack of the fetch behind it, and a+ * map asks for a hundred tiles: the same stack a hundred times over. One line per distinct failure+ * is what the map settles for instead.+ */+describe("summarizeMapError", () => {+ /** A failed request, as MapLibre's own `AJAXError` carries it. */+ function ajaxError(status: number, statusText: string, url: string) {+ return Object.assign(new Error(`AJAXError: ${statusText} (${status}): ${url}`), { status, statusText, url });+ }++ it("names the server and the status, and says which URL asked", () => {+ const { message } = summarizeMapError(ajaxError(403, "Forbidden", "https://tiles.mapgenie.io/games/fallout-4/10/512/510.png"));++ expect(message).toBe("tiles.mapgenie.io answered 403 Forbidden — could not load https://tiles.mapgenie.io/games/fallout-4/10/512/510.png");+ // The stack the message would otherwise be printed with is what makes it verbose.+ expect(message).not.toContain("maplibre");+ });++ it("counts every tile the same server refuses as the one failure", () => {+ const first = summarizeMapError(ajaxError(403, "Forbidden", "https://tiles.mapgenie.io/10/512/510.png"));+ const second = summarizeMapError(ajaxError(403, "Forbidden", "https://tiles.mapgenie.io/10/513/510.png"));++ expect(second.key).toBe(first.key);+ // A different server, or a different thing going wrong with the same one, is still worth saying.+ expect(summarizeMapError(ajaxError(404, "Not Found", "https://tiles.mapgenie.io/10/512/510.png")).key).not.toBe(first.key);+ expect(summarizeMapError(ajaxError(403, "Forbidden", "https://tiles.versatiles.org/10/512/510.png")).key).not.toBe(first.key);+ });++ it("copes with a status that has no text and a URL that is not one", () => {+ expect(summarizeMapError(ajaxError(0, "", "/local/tiles/10/512/510.png")).message)+ .toBe("/local/tiles/10/512/510.png answered 0 — could not load /local/tiles/10/512/510.png");+ });++ it("falls back to whatever the error says when it is not a failed request", () => {+ expect(summarizeMapError(new Error("Style is not done loading"))).toEqual({+ key: "Style is not done loading",+ message: "Style is not done loading"+ });+ expect(summarizeMapError("something went wrong").message).toBe("something went wrong");+ });+});diff --git a/apps/client/src/widgets/collections/geomap/map.tsx b/apps/client/src/widgets/collections/geomap/map.tsxindex 1354233b87..8c784e65e1 100644--- a/apps/client/src/widgets/collections/geomap/map.tsx+++ b/apps/client/src/widgets/collections/geomap/map.tsx@@ -1,6 +1,6 @@import "maplibre-gl/dist/maplibre-gl.css";-import { Map as MapLibreGLMap, MapMouseEvent, NavigationControl, type Point, ScaleControl, type StyleSpecification, type TransformStyleFunction } from "maplibre-gl";+import { type ErrorEvent as MapErrorEvent, Map as MapLibreGLMap, MapMouseEvent, NavigationControl, type Point, ScaleControl, type StyleSpecification, type TransformStyleFunction } from "maplibre-gl";import { ComponentChildren, createContext, RefObject } from "preact";import { useEffect, useImperativeHandle, useRef, useState } from "preact/hooks";@@ -106,6 +106,18 @@ export default function Map({ coordinates, zoom, layerData, viewportChanged, chirenderWorldCopies: false});+ // An error MapLibre finds no listener for goes to `console.error` with the stack of the+ // fetch that failed, and a tile server answering 403 fails once per tile: a screenful of+ // identical stacks, none of which says anything the first one did not. Listening takes that+ // over, and what is left is one warning per distinct failure, with no stack on it.+ const reported = new Set<string>();+ mapInstance.on("error", ({ error }: MapErrorEvent) => {+ const { key, message } = summarizeMapError(error);+ if (reported.has(key)) return;+ reported.add(key);+ console.warn(`Geo map: ${message}`);+ });+// Zoom buttons, which Leaflet added of its own accord. No compass: nothing here persists a// bearing, so the button would offer to undo a rotation the map never remembers.mapInstance.addControl(new NavigationControl({@@ -209,6 +221,53 @@ export default function Map({ coordinates, zoom, layerData, viewportChanged, chi);}+/** An HTTP failure as MapLibre reports one: what {@link summarizeMapError} reads off an `AJAXError`. */+interface HttpFailure {+ status: number;+ statusText?: string;+ url: string;+}++function isHttpFailure(error: unknown): error is HttpFailure {+ return (+ typeof error === "object" && error !== null &&+ typeof (error as HttpFailure).status === "number" &&+ typeof (error as HttpFailure).url === "string"+ );+}++/**+ * Says in one line what went wrong, and under what to count it as already said.+ *+ * A tile that cannot be fetched is not an error about that tile: the tile server is unreachable, or+ * refuses us, and every other tile it is asked for will fail the same way. The key therefore names+ * the server and the status rather than the URL, so the hundred tiles behind the first one are+ * recognised as the same failure and stay quiet — the URL is still in the message, as an example of+ * what was asked for.+ */+export function summarizeMapError(error: unknown): { key: string; message: string } {+ if (isHttpFailure(error)) {+ const host = hostOf(error.url);+ const status = [ String(error.status), error.statusText ].filter(Boolean).join(" ");+ return {+ key: `${error.status} ${host}`,+ message: `${host} answered ${status} — could not load ${error.url}`+ };+ }++ const message = error instanceof Error ? error.message : String(error);+ return { key: message, message };+}++/** The server a URL names, or the URL itself where it names none. */+function hostOf(url: string): string {+ try {+ return new URL(url).host;+ } catch {+ return url;+ }+}+/** What a style is made of, by name — all {@link keepAdditions} needs of the one it is given. */export interface StyleContents {sources: Set<string>;