@celestia-island/hikari 0.55.497 → 0.55.498
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/types/errorReporting/index.d.ts +11 -0
- package/dist/types/errorReporting/serverLanding.d.ts +103 -0
- package/dist/types/errorReporting/state.d.ts +10 -2
- package/dist/types/index.d.ts +1 -0
- package/package.json +1 -1
- package/src/errorReporting/HkErrorReportingOverlay.tsx +39 -3
- package/src/errorReporting/index.test.tsx +222 -0
- package/src/errorReporting/index.ts +27 -0
- package/src/errorReporting/serverLanding.test.ts +192 -0
- package/src/errorReporting/serverLanding.ts +187 -0
- package/src/errorReporting/state.ts +24 -2
- package/src/index.ts +10 -0
|
@@ -2,6 +2,7 @@ import { type Plugin } from "vue";
|
|
|
2
2
|
import { type HkErrorReportingOptions, type HkErrorSource } from "./state";
|
|
3
3
|
export type { HkErrorReportingOptions, HkErrorSource, HkReportedError, } from "./state";
|
|
4
4
|
export { HkErrorReportingOverlay } from "./HkErrorReportingOverlay";
|
|
5
|
+
export { HkServerErrorLandingError, parseServerErrorLanding, readServerErrorLanding, reportServerErrorLanding, serializeServerErrorLanding, SERVER_ERROR_LANDING_ID, SERVER_ERROR_LANDING_VERSION, type HkServerErrorLanding, } from "./serverLanding";
|
|
5
6
|
/**
|
|
6
7
|
* createErrorReporting — the install-and-forget global error reporting hook.
|
|
7
8
|
*
|
|
@@ -26,6 +27,16 @@ export { HkErrorReportingOverlay } from "./HkErrorReportingOverlay";
|
|
|
26
27
|
* Any pre-existing `app.config.errorHandler` is chained, not discarded.
|
|
27
28
|
* All capture channels are optional; `shouldReport` filters, `onError` is
|
|
28
29
|
* the telemetry hook.
|
|
30
|
+
*
|
|
31
|
+
* INSTALL ALSO SETTLES THE SERVER'S VERDICT ON THIS DOCUMENT: when the
|
|
32
|
+
* server answered the navigation with an error it hands it over through the
|
|
33
|
+
* `hk-server-error` JSON island in index.html (see `./serverLanding`), and
|
|
34
|
+
* install raises the landing for it right away — source `server`, the HTTP
|
|
35
|
+
* status and the raw response on the card. So `app.use(createErrorReporting())`
|
|
36
|
+
* is the one line that covers BOTH halves of "no bare error ever reaches a
|
|
37
|
+
* visitor": what crashes after boot, and what the server said before it.
|
|
38
|
+
* The island is read once and consumed; a host that wants to decide itself
|
|
39
|
+
* reads it with `readServerErrorLanding()` BEFORE installing.
|
|
29
40
|
*/
|
|
30
41
|
export declare function createErrorReporting(options?: HkErrorReportingOptions): Plugin;
|
|
31
42
|
/**
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { type HkReportedError } from "./state";
|
|
2
|
+
/**
|
|
3
|
+
* Server error landing handoff — the contract between a family SERVER and
|
|
4
|
+
* the SPA it serves, for the errors a browser reaches by NAVIGATION.
|
|
5
|
+
*
|
|
6
|
+
* `createErrorReporting` catches what happens after the app is running. It
|
|
7
|
+
* cannot catch the failure that never lets the app start: a server that
|
|
8
|
+
* answers a top-level navigation with a bare `{"error": …}` body (an OAuth
|
|
9
|
+
* callback whose token exchange failed, a revoked share link, a 502 from a
|
|
10
|
+
* restart window) shows the visitor raw JSON in the tab — no landing, no
|
|
11
|
+
* way back, nothing to copy but what the viewport holds (the 2026-11-26
|
|
12
|
+
* res.celestia.world report).
|
|
13
|
+
*
|
|
14
|
+
* The handoff closes that gap with the page itself as the carrier. The
|
|
15
|
+
* server answers with the SPA's own `index.html` at the REAL status code,
|
|
16
|
+
* with one JSON data island injected:
|
|
17
|
+
*
|
|
18
|
+
* ```html
|
|
19
|
+
* <script type="application/json" id="hk-server-error">
|
|
20
|
+
* {"v":1,"status":502,"code":"identity_provider_unreachable",
|
|
21
|
+
* "message":"identity provider unreachable: …","raw":{"error":"…"}}
|
|
22
|
+
* </script>
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* `createErrorReporting().install` reads the island (once — the element is
|
|
26
|
+
* consumed) and raises the same takeover a runtime error gets, with the
|
|
27
|
+
* HTTP status and the server's raw payload on the card. A data block is
|
|
28
|
+
* never executed, so it passes a `script-src 'self'` policy untouched, and
|
|
29
|
+
* {@link serializeServerErrorLanding} escapes the few characters that could
|
|
30
|
+
* let the payload close the island or read as markup.
|
|
31
|
+
*
|
|
32
|
+
* The island is the WHOLE contract: nothing here knows a URL scheme, a
|
|
33
|
+
* header name or a service, so any server facing a browser can produce it
|
|
34
|
+
* (the Rust side lives in plana's `plana-error-landing`).
|
|
35
|
+
*/
|
|
36
|
+
/** Element id of the JSON island a server injects into its index.html. */
|
|
37
|
+
export declare const SERVER_ERROR_LANDING_ID = "hk-server-error";
|
|
38
|
+
/** The only island shape this build understands (`v` in the payload). */
|
|
39
|
+
export declare const SERVER_ERROR_LANDING_VERSION = 1;
|
|
40
|
+
/** A validated server handoff: what the card shows, plus the raw body. */
|
|
41
|
+
export interface HkServerErrorLanding {
|
|
42
|
+
/** The HTTP status the server ANSWERED with — shown as the `HTTP <n>` chip. */
|
|
43
|
+
readonly status: number;
|
|
44
|
+
/** Wire error code shown as the code chip (`unknown_provider`). Empty
|
|
45
|
+
* falls back to `http_<status>`. */
|
|
46
|
+
readonly code: string;
|
|
47
|
+
/** The server's human-readable explanation — the card's description. */
|
|
48
|
+
readonly message: string;
|
|
49
|
+
/** The response body the server would have sent as JSON (an object, an
|
|
50
|
+
* array, a string …). Rendered in the raw-details pane; absent when the
|
|
51
|
+
* server had nothing to hand over. */
|
|
52
|
+
readonly raw?: unknown;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The error object a server handoff is reported through — the counterpart
|
|
56
|
+
* of the runtime errors the window hooks catch. `name` is the code chip, so
|
|
57
|
+
* it reads as the wire code when the server sent one and as `http_<status>`
|
|
58
|
+
* when it did not; `status` and `details` ride the report onto the card.
|
|
59
|
+
* `reportError` copies both off ANY object reported with the `server`
|
|
60
|
+
* source (duck-typed), so a host's own subclass can carry them without
|
|
61
|
+
* importing this one; a runtime error is never inspected for them.
|
|
62
|
+
*/
|
|
63
|
+
export declare class HkServerErrorLandingError extends Error {
|
|
64
|
+
readonly status: number;
|
|
65
|
+
readonly code: string;
|
|
66
|
+
readonly raw: unknown;
|
|
67
|
+
readonly landing: HkServerErrorLanding;
|
|
68
|
+
constructor(landing: HkServerErrorLanding);
|
|
69
|
+
/** The wire field under the name the report carries (`reportError` reads
|
|
70
|
+
* `details`, whatever class the error is). Same value as `raw`. */
|
|
71
|
+
get details(): unknown;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Validate an island's text into a handoff, or `null` for anything this
|
|
75
|
+
* build must not render: a JSON syntax error, a non-object, another
|
|
76
|
+
* `v` (a future shape — ignored rather than half-rendered), a status
|
|
77
|
+
* outside the error classes. Unknown keys are ignored, so a newer server
|
|
78
|
+
* can add fields without breaking an older SPA.
|
|
79
|
+
*/
|
|
80
|
+
export declare function parseServerErrorLanding(text: string): HkServerErrorLanding | null;
|
|
81
|
+
/**
|
|
82
|
+
* Serialize a handoff as island text. Besides `v`, the output differs from
|
|
83
|
+
* plain `JSON.stringify` in one way that matters: `<`, `>` and `&` leave as
|
|
84
|
+
* `\u003c`-style escapes, so no payload — however hostile — can close the
|
|
85
|
+
* `<script>` element or be read as markup by a parser that never sees the
|
|
86
|
+
* island as a data block (`JSON.parse` turns the escapes back).
|
|
87
|
+
*/
|
|
88
|
+
export declare function serializeServerErrorLanding(landing: HkServerErrorLanding): string;
|
|
89
|
+
/**
|
|
90
|
+
* Read the handoff out of the document — once. The island is REMOVED on the
|
|
91
|
+
* first read, valid or not: a reinstall (HMR, a second app instance on the
|
|
92
|
+
* same document) must not re-raise a landing the visitor already saw or
|
|
93
|
+
* dismissed, and a malformed island must not be retried on every install.
|
|
94
|
+
* `null` when the document carries no island or the island is unusable.
|
|
95
|
+
*/
|
|
96
|
+
export declare function readServerErrorLanding(root?: ParentNode): HkServerErrorLanding | null;
|
|
97
|
+
/**
|
|
98
|
+
* Raise the global error landing for a server handoff — the navigation-time
|
|
99
|
+
* counterpart of `reportGlobalError`. Goes through the same acceptance
|
|
100
|
+
* (`shouldReport`, first-error-wins, `onError`) as every other source, so
|
|
101
|
+
* a host filters or logs it exactly like a runtime error.
|
|
102
|
+
*/
|
|
103
|
+
export declare function reportServerErrorLanding(landing: HkServerErrorLanding): HkReportedError | null;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type Ref, type VNodeChild } from "vue";
|
|
2
|
-
/** Where a reported error came from.
|
|
3
|
-
|
|
2
|
+
/** Where a reported error came from. `server` = a handoff the server
|
|
3
|
+
* injected into this document (see `./serverLanding`), not a runtime error. */
|
|
4
|
+
export type HkErrorSource = "vue" | "window" | "rejection" | "manual" | "server";
|
|
4
5
|
/** A normalized error record carried by the global error landing. */
|
|
5
6
|
export interface HkReportedError {
|
|
6
7
|
source: HkErrorSource;
|
|
@@ -10,6 +11,13 @@ export interface HkReportedError {
|
|
|
10
11
|
stack: string;
|
|
11
12
|
/** Vue's `info` string for errors surfaced through `app.config.errorHandler`. */
|
|
12
13
|
info?: string;
|
|
14
|
+
/** The HTTP status a SERVER handoff answered with — the `HTTP <n>` chip.
|
|
15
|
+
* Absent for runtime errors (nothing to show). */
|
|
16
|
+
status?: number;
|
|
17
|
+
/** A structured payload the reporter attached to the error, shown in the
|
|
18
|
+
* raw-details pane in place of the stack record: a server handoff's raw
|
|
19
|
+
* response body. Absent for runtime errors. */
|
|
20
|
+
details?: unknown;
|
|
13
21
|
}
|
|
14
22
|
export interface HkErrorReportingOptions {
|
|
15
23
|
/** Headline override; defaults to `hikari::errors.defaultTitle`.
|
package/dist/types/index.d.ts
CHANGED
|
@@ -143,6 +143,7 @@ export { default as HErrorBoundary } from "./components/HkErrorBoundary";
|
|
|
143
143
|
export { HkErrorLanding as HErrorLanding, type HErrorTone, type HErrorLandingVariant, } from "./components/HkErrorLanding";
|
|
144
144
|
export { HkErrorReportingOverlay as HErrorReportingOverlay } from "./errorReporting";
|
|
145
145
|
export { createErrorReporting, reportGlobalError, clearGlobalError, type HkErrorReportingOptions, type HkErrorSource, type HkReportedError, } from "./errorReporting";
|
|
146
|
+
export { HkServerErrorLandingError as HServerErrorLandingError, parseServerErrorLanding, readServerErrorLanding, reportServerErrorLanding, serializeServerErrorLanding, SERVER_ERROR_LANDING_ID, SERVER_ERROR_LANDING_VERSION, type HkServerErrorLanding as HServerErrorLanding, } from "./errorReporting";
|
|
146
147
|
export { default as HDraggableList } from "./components/HkDraggableList";
|
|
147
148
|
export { default as HDraggableGrid } from "./components/HkDraggableGrid";
|
|
148
149
|
export { default as HSelectionGrid } from "./components/HkSelectionGrid";
|
package/package.json
CHANGED
|
@@ -35,6 +35,18 @@ export const HkErrorReportingOverlay = defineComponent({
|
|
|
35
35
|
const detailsValue = computed<Record<string, unknown> | undefined>(() => {
|
|
36
36
|
const err = state.value;
|
|
37
37
|
if (!err) return undefined;
|
|
38
|
+
// A server handoff: what the server answered, never a stack — the
|
|
39
|
+
// only stack it has names the install call that raised it. The raw
|
|
40
|
+
// body rides under its own key so a tree fold shows the wire error
|
|
41
|
+
// exactly as sent.
|
|
42
|
+
if (err.source === "server") {
|
|
43
|
+
return {
|
|
44
|
+
status: err.status,
|
|
45
|
+
code: err.name,
|
|
46
|
+
message: err.message,
|
|
47
|
+
...(err.details !== undefined ? { response: err.details } : {}),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
38
50
|
const record: Record<string, unknown> = {
|
|
39
51
|
name: err.name,
|
|
40
52
|
message: err.message,
|
|
@@ -46,11 +58,22 @@ export const HkErrorReportingOverlay = defineComponent({
|
|
|
46
58
|
});
|
|
47
59
|
|
|
48
60
|
// Clipboard payload for the landing's built-in copy action: the same
|
|
49
|
-
// fields the raw-details pane shows, in plain-text form.
|
|
61
|
+
// fields the raw-details pane shows, in plain-text form. The headline
|
|
62
|
+
// carries the status line of a server handoff; its body is the response
|
|
63
|
+
// the server sent.
|
|
50
64
|
const copyText = computed(() => {
|
|
51
65
|
const err = state.value;
|
|
52
66
|
if (!err) return "";
|
|
53
|
-
const
|
|
67
|
+
const headline =
|
|
68
|
+
err.status !== undefined
|
|
69
|
+
? `HTTP ${err.status} ${err.name}: ${err.message}`
|
|
70
|
+
: `${err.name}: ${err.message}`;
|
|
71
|
+
if (err.source === "server") {
|
|
72
|
+
const parts = [headline];
|
|
73
|
+
if (err.details !== undefined) parts.push("", JSON.stringify(err.details, null, 2));
|
|
74
|
+
return parts.join("\n");
|
|
75
|
+
}
|
|
76
|
+
const parts = [headline];
|
|
54
77
|
if (err.stack) parts.push("", err.stack);
|
|
55
78
|
if (err.source) parts.push("", `source: ${err.source}`);
|
|
56
79
|
if (err.info) parts.push("", `info: ${err.info}`);
|
|
@@ -98,6 +121,12 @@ export const HkErrorReportingOverlay = defineComponent({
|
|
|
98
121
|
|
|
99
122
|
const showHome = Boolean(options.onHome) || options.homeHref !== false;
|
|
100
123
|
|
|
124
|
+
// A 4xx is the request's problem (a spent link, a missing page, a
|
|
125
|
+
// refused sign-in), not a crash of what the visitor is looking at:
|
|
126
|
+
// the card says so with the warning tone. 5xx — and every runtime
|
|
127
|
+
// error, which carries no status — keeps the error tone.
|
|
128
|
+
const tone = err.status !== undefined && err.status < 500 ? "warning" : "error";
|
|
129
|
+
|
|
101
130
|
return (
|
|
102
131
|
<div class="hk-error-reporting-overlay" role="alertdialog" aria-live="assertive" aria-modal="true">
|
|
103
132
|
{/* The overlay is its OWN app instance (it must survive the host
|
|
@@ -115,10 +144,17 @@ export const HkErrorReportingOverlay = defineComponent({
|
|
|
115
144
|
title={options.title || t("hikari::errors.defaultTitle", "Something went wrong")}
|
|
116
145
|
description={description}
|
|
117
146
|
code={err.name}
|
|
147
|
+
status={err.status}
|
|
148
|
+
tone={tone}
|
|
118
149
|
copyText={copyText.value}
|
|
119
150
|
>
|
|
120
151
|
{{
|
|
121
|
-
default: () =>
|
|
152
|
+
default: () => (
|
|
153
|
+
<HkJsonTree
|
|
154
|
+
value={detailsValue.value}
|
|
155
|
+
ariaLabel={err.source === "server" ? "server error details" : "stack trace"}
|
|
156
|
+
/>
|
|
157
|
+
),
|
|
122
158
|
actions: () => [
|
|
123
159
|
...(showHome
|
|
124
160
|
? [(
|
|
@@ -6,7 +6,10 @@ import {
|
|
|
6
6
|
createErrorReporting,
|
|
7
7
|
reportGlobalError,
|
|
8
8
|
resetErrorReportingForTests,
|
|
9
|
+
serializeServerErrorLanding,
|
|
10
|
+
SERVER_ERROR_LANDING_ID,
|
|
9
11
|
type HkErrorReportingOptions,
|
|
12
|
+
type HkServerErrorLanding,
|
|
10
13
|
} from "./index";
|
|
11
14
|
|
|
12
15
|
const Boom = defineComponent({
|
|
@@ -299,3 +302,222 @@ describe("createErrorReporting", () => {
|
|
|
299
302
|
expect(host.querySelector(".host-card")!.getAttribute("data-msg")).toBe("own content");
|
|
300
303
|
});
|
|
301
304
|
});
|
|
305
|
+
|
|
306
|
+
describe("server error landing handoff", () => {
|
|
307
|
+
const OAUTH_502: HkServerErrorLanding = {
|
|
308
|
+
status: 502,
|
|
309
|
+
code: "identity_provider_unreachable",
|
|
310
|
+
message: "identity provider unreachable: identity provider answered 400 Bad Request",
|
|
311
|
+
raw: { error: "identity provider unreachable: identity provider answered 400 Bad Request" },
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
/** Plant the island exactly as a family server injects it into index.html. */
|
|
315
|
+
function plant(landing: HkServerErrorLanding | string): void {
|
|
316
|
+
const island = document.createElement("script");
|
|
317
|
+
island.id = SERVER_ERROR_LANDING_ID;
|
|
318
|
+
island.type = "application/json";
|
|
319
|
+
island.textContent = typeof landing === "string" ? landing : serializeServerErrorLanding(landing);
|
|
320
|
+
document.head.appendChild(island);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function statusChip(): string | null {
|
|
324
|
+
return overlayHost()?.querySelector(".hk-error-landing__status")?.textContent ?? null;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
afterEach(() => {
|
|
328
|
+
document.getElementById(SERVER_ERROR_LANDING_ID)?.remove();
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it("raises the landing at install — before the host tree mounts", async () => {
|
|
332
|
+
const error = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
333
|
+
plant(OAUTH_502);
|
|
334
|
+
installPlugin();
|
|
335
|
+
await nextTick();
|
|
336
|
+
|
|
337
|
+
const host = overlayHost();
|
|
338
|
+
expect(host).not.toBeNull();
|
|
339
|
+
// The wire facts, as chips and copy: the status the server ANSWERED
|
|
340
|
+
// with, the wire code, the server's own explanation.
|
|
341
|
+
expect(statusChip()).toBe("HTTP 502");
|
|
342
|
+
expect(host!.querySelector(".hk-error-landing__code")!.textContent).toBe("identity_provider_unreachable");
|
|
343
|
+
expect(host!.querySelector(".hk-error-landing__desc")!.textContent).toBe(OAUTH_502.message);
|
|
344
|
+
// The raw body the browser would otherwise have shown as bare JSON is
|
|
345
|
+
// now inside the details pane.
|
|
346
|
+
expect(host!.querySelector(".s-tool-json-tree")).not.toBeNull();
|
|
347
|
+
expect(host!.querySelector(".s-tool-json-tree")!.textContent).toContain("response");
|
|
348
|
+
// The visitor still has the way back and the retry.
|
|
349
|
+
const labels = Array.from(host!.querySelectorAll("button")).map((b) => b.textContent);
|
|
350
|
+
expect(labels).toContain("Back to home");
|
|
351
|
+
expect(labels).toContain("Retry");
|
|
352
|
+
// Reported through the ordinary channel, under its own source.
|
|
353
|
+
expect(error).toHaveBeenCalledWith("[hikari:error-reporting:server]", expect.any(Error));
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it("consumes the island: it is gone after install and a reinstall cannot re-raise it", async () => {
|
|
357
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
358
|
+
plant(OAUTH_502);
|
|
359
|
+
installPlugin();
|
|
360
|
+
await nextTick();
|
|
361
|
+
expect(document.getElementById(SERVER_ERROR_LANDING_ID)).toBeNull();
|
|
362
|
+
|
|
363
|
+
clearGlobalError();
|
|
364
|
+
expect(overlayHost()).toBeNull();
|
|
365
|
+
installPlugin();
|
|
366
|
+
await nextTick();
|
|
367
|
+
// The visitor dismissed it; a second install on the same document is
|
|
368
|
+
// not a new server answer.
|
|
369
|
+
expect(overlayHost()).toBeNull();
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
it("falls back to an http_<status> code chip when the server sent no code", async () => {
|
|
373
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
374
|
+
plant({ status: 404, code: "", message: "route not found" });
|
|
375
|
+
installPlugin();
|
|
376
|
+
await nextTick();
|
|
377
|
+
const host = overlayHost()!;
|
|
378
|
+
expect(statusChip()).toBe("HTTP 404");
|
|
379
|
+
expect(host.querySelector(".hk-error-landing__code")!.textContent).toBe("http_404");
|
|
380
|
+
expect(host.querySelector(".hk-error-landing__desc")!.textContent).toBe("route not found");
|
|
381
|
+
// No body came back, so there is no `response` — and what the server
|
|
382
|
+
// answered is the WHOLE record: the only stack a handoff has names the
|
|
383
|
+
// install call that raised it, which tells the visitor nothing.
|
|
384
|
+
expect(host.querySelector('[aria-label="server error details"]')).not.toBeNull();
|
|
385
|
+
const tree = host.querySelector(".s-tool-json-tree")!.textContent!;
|
|
386
|
+
expect(tree).toContain("route not found");
|
|
387
|
+
expect(tree).not.toContain("stack");
|
|
388
|
+
expect(tree).not.toContain(" at ");
|
|
389
|
+
// No body came back, so there is no `response` key to fold open either.
|
|
390
|
+
expect(tree).not.toContain("response");
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it("paints a 4xx as a warning and a 5xx as an error", async () => {
|
|
394
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
395
|
+
plant({ status: 410, code: "share_gone", message: "This link was revoked." });
|
|
396
|
+
installPlugin();
|
|
397
|
+
await nextTick();
|
|
398
|
+
expect(overlayHost()!.querySelector(".hk-error-landing")!.className).toContain("is-warning");
|
|
399
|
+
|
|
400
|
+
clearGlobalError();
|
|
401
|
+
plant({ status: 499, code: "client_closed", message: "Just under the line." });
|
|
402
|
+
installPlugin();
|
|
403
|
+
await nextTick();
|
|
404
|
+
expect(overlayHost()!.querySelector(".hk-error-landing")!.className).toContain("is-warning");
|
|
405
|
+
|
|
406
|
+
// 500 is the server's side of the line, exactly on it.
|
|
407
|
+
clearGlobalError();
|
|
408
|
+
plant({ status: 500, code: "", message: "On the line." });
|
|
409
|
+
installPlugin();
|
|
410
|
+
await nextTick();
|
|
411
|
+
expect(overlayHost()!.querySelector(".hk-error-landing")!.className).toContain("is-error");
|
|
412
|
+
expect(statusChip()).toBe("HTTP 500");
|
|
413
|
+
|
|
414
|
+
clearGlobalError();
|
|
415
|
+
plant({ status: 503, code: "starting", message: "Starting up." });
|
|
416
|
+
installPlugin();
|
|
417
|
+
await nextTick();
|
|
418
|
+
expect(overlayHost()!.querySelector(".hk-error-landing")!.className).toContain("is-error");
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
it("ignores a malformed island — nothing is raised", () => {
|
|
422
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
423
|
+
// Negative control: the same install with the same document, minus a
|
|
424
|
+
// usable payload. A filter that "always raises" would fail here.
|
|
425
|
+
plant("{not json");
|
|
426
|
+
installPlugin();
|
|
427
|
+
expect(overlayHost()).toBeNull();
|
|
428
|
+
// A status outside the error classes is not a handoff either.
|
|
429
|
+
plant({ status: 302, code: "", message: "should be followed, not shown" });
|
|
430
|
+
installPlugin();
|
|
431
|
+
expect(overlayHost()).toBeNull();
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
it("lets shouldReport drop a handoff like any other source", () => {
|
|
435
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
436
|
+
plant(OAUTH_502);
|
|
437
|
+
installPlugin({ shouldReport: (_err, source) => source !== "server" });
|
|
438
|
+
// Consumed even though filtered: the document still carried it.
|
|
439
|
+
expect(document.getElementById(SERVER_ERROR_LANDING_ID)).toBeNull();
|
|
440
|
+
expect(overlayHost()).toBeNull();
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
it("hands status and details to a host landing card", async () => {
|
|
444
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
445
|
+
plant(OAUTH_502);
|
|
446
|
+
installPlugin({
|
|
447
|
+
landing: (err) => h("div", { class: "host-card", "data-status": err.status ?? "none" }, [
|
|
448
|
+
h("span", { class: "host-card__raw" }, JSON.stringify(err.details)),
|
|
449
|
+
]),
|
|
450
|
+
});
|
|
451
|
+
await nextTick();
|
|
452
|
+
// A host that owns its card (chest-style) reads the same two fields
|
|
453
|
+
// the built-in card paints — the handoff is not a built-in-only path.
|
|
454
|
+
expect(overlayHost()!.querySelector(".host-card")!.getAttribute("data-status")).toBe("502");
|
|
455
|
+
expect(overlayHost()!.querySelector(".host-card__raw")!.textContent).toBe(JSON.stringify(OAUTH_502.raw));
|
|
456
|
+
expect(overlayHost()!.querySelector(".hk-error-landing")).toBeNull();
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
it("carries the status line and the raw body on the copy action", async () => {
|
|
460
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
461
|
+
const writeText = vi.fn<(text: string) => Promise<void>>().mockResolvedValue(undefined);
|
|
462
|
+
Object.defineProperty(navigator, "clipboard", { value: { writeText }, configurable: true });
|
|
463
|
+
plant(OAUTH_502);
|
|
464
|
+
installPlugin();
|
|
465
|
+
await nextTick();
|
|
466
|
+
|
|
467
|
+
const copy = Array.from(overlayHost()!.querySelectorAll<HTMLButtonElement>("button"))
|
|
468
|
+
.find((b) => b.textContent === "Copy error details")!;
|
|
469
|
+
copy.click();
|
|
470
|
+
await nextTick();
|
|
471
|
+
await nextTick();
|
|
472
|
+
|
|
473
|
+
expect(writeText).toHaveBeenCalledTimes(1);
|
|
474
|
+
const text = writeText.mock.calls[0]![0];
|
|
475
|
+
// The headline, then exactly the body the server sent — nothing else:
|
|
476
|
+
// no JS stack, no `source:` footer from the runtime record.
|
|
477
|
+
expect(text).toBe(
|
|
478
|
+
"HTTP 502 identity_provider_unreachable: " + OAUTH_502.message + "\n\n" + JSON.stringify(OAUTH_502.raw, null, 2),
|
|
479
|
+
);
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
it("shows no status chip for runtime errors — the handoff fields are opt-in", async () => {
|
|
483
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
484
|
+
installPlugin();
|
|
485
|
+
reportGlobalError(new TypeError("plain runtime error"));
|
|
486
|
+
await nextTick();
|
|
487
|
+
expect(statusChip()).toBeNull();
|
|
488
|
+
expect(overlayHost()!.querySelector(".hk-error-landing")!.className).not.toContain("is-warning");
|
|
489
|
+
expect(overlayHost()!.querySelector('[aria-label="stack trace"]')).not.toBeNull();
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
it("leaves a runtime error alone even when it happens to carry status/details fields", async () => {
|
|
493
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
494
|
+
installPlugin();
|
|
495
|
+
// The shape of an HTTP client's error object (and of validation
|
|
496
|
+
// libraries' `details`): those fields belong to the host, not to the
|
|
497
|
+
// handoff — the runtime card must not start painting them.
|
|
498
|
+
const clientError = Object.assign(new TypeError("Request failed with status code 404"), {
|
|
499
|
+
status: 404,
|
|
500
|
+
details: { field: "email" },
|
|
501
|
+
});
|
|
502
|
+
reportGlobalError(clientError);
|
|
503
|
+
await nextTick();
|
|
504
|
+
expect(statusChip()).toBeNull();
|
|
505
|
+
expect(overlayHost()!.querySelector(".hk-error-landing")!.className).not.toContain("is-warning");
|
|
506
|
+
expect(overlayHost()!.querySelector('[aria-label="stack trace"]')).not.toBeNull();
|
|
507
|
+
expect(overlayHost()!.querySelector(".s-tool-json-tree")!.textContent).not.toContain("response");
|
|
508
|
+
|
|
509
|
+
// The same object reported AS a handoff is the host's explicit choice.
|
|
510
|
+
clearGlobalError();
|
|
511
|
+
reportGlobalError(clientError, "server");
|
|
512
|
+
await nextTick();
|
|
513
|
+
expect(statusChip()).toBe("HTTP 404");
|
|
514
|
+
expect(overlayHost()!.querySelector(".s-tool-json-tree")!.textContent).toContain("response");
|
|
515
|
+
|
|
516
|
+
// A status that is not a number is not a status, even on a handoff:
|
|
517
|
+
// only a real `number` becomes the chip.
|
|
518
|
+
clearGlobalError();
|
|
519
|
+
reportGlobalError(Object.assign(new Error("string status"), { status: "502" }), "server");
|
|
520
|
+
await nextTick();
|
|
521
|
+
expect(statusChip()).toBeNull();
|
|
522
|
+
});
|
|
523
|
+
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createApp, type App, type Plugin } from "vue";
|
|
2
2
|
|
|
3
3
|
import { HkErrorReportingOverlay } from "./HkErrorReportingOverlay";
|
|
4
|
+
import { readServerErrorLanding, reportServerErrorLanding } from "./serverLanding";
|
|
4
5
|
import {
|
|
5
6
|
clearErrorReportingState,
|
|
6
7
|
reportError,
|
|
@@ -15,6 +16,16 @@ export type {
|
|
|
15
16
|
HkReportedError,
|
|
16
17
|
} from "./state";
|
|
17
18
|
export { HkErrorReportingOverlay } from "./HkErrorReportingOverlay";
|
|
19
|
+
export {
|
|
20
|
+
HkServerErrorLandingError,
|
|
21
|
+
parseServerErrorLanding,
|
|
22
|
+
readServerErrorLanding,
|
|
23
|
+
reportServerErrorLanding,
|
|
24
|
+
serializeServerErrorLanding,
|
|
25
|
+
SERVER_ERROR_LANDING_ID,
|
|
26
|
+
SERVER_ERROR_LANDING_VERSION,
|
|
27
|
+
type HkServerErrorLanding,
|
|
28
|
+
} from "./serverLanding";
|
|
18
29
|
|
|
19
30
|
/**
|
|
20
31
|
* createErrorReporting — the install-and-forget global error reporting hook.
|
|
@@ -40,12 +51,28 @@ export { HkErrorReportingOverlay } from "./HkErrorReportingOverlay";
|
|
|
40
51
|
* Any pre-existing `app.config.errorHandler` is chained, not discarded.
|
|
41
52
|
* All capture channels are optional; `shouldReport` filters, `onError` is
|
|
42
53
|
* the telemetry hook.
|
|
54
|
+
*
|
|
55
|
+
* INSTALL ALSO SETTLES THE SERVER'S VERDICT ON THIS DOCUMENT: when the
|
|
56
|
+
* server answered the navigation with an error it hands it over through the
|
|
57
|
+
* `hk-server-error` JSON island in index.html (see `./serverLanding`), and
|
|
58
|
+
* install raises the landing for it right away — source `server`, the HTTP
|
|
59
|
+
* status and the raw response on the card. So `app.use(createErrorReporting())`
|
|
60
|
+
* is the one line that covers BOTH halves of "no bare error ever reaches a
|
|
61
|
+
* visitor": what crashes after boot, and what the server said before it.
|
|
62
|
+
* The island is read once and consumed; a host that wants to decide itself
|
|
63
|
+
* reads it with `readServerErrorLanding()` BEFORE installing.
|
|
43
64
|
*/
|
|
44
65
|
export function createErrorReporting(options: HkErrorReportingOptions = {}): Plugin {
|
|
45
66
|
return {
|
|
46
67
|
install(app: App) {
|
|
47
68
|
setErrorReportingOptions(options);
|
|
48
69
|
|
|
70
|
+
// The server's handoff outranks the runtime channels: the page was
|
|
71
|
+
// delivered AS an error, so its landing goes up before the host tree
|
|
72
|
+
// (which mounts after this install) can draw or redirect anything.
|
|
73
|
+
const handoff = readServerErrorLanding();
|
|
74
|
+
if (handoff && reportServerErrorLanding(handoff)) ensureOverlayMounted();
|
|
75
|
+
|
|
49
76
|
const previous = app.config.errorHandler;
|
|
50
77
|
app.config.errorHandler = (err, instance, info) => {
|
|
51
78
|
if (previous) {
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
HkServerErrorLandingError,
|
|
5
|
+
parseServerErrorLanding,
|
|
6
|
+
readServerErrorLanding,
|
|
7
|
+
serializeServerErrorLanding,
|
|
8
|
+
SERVER_ERROR_LANDING_ID,
|
|
9
|
+
SERVER_ERROR_LANDING_VERSION,
|
|
10
|
+
type HkServerErrorLanding,
|
|
11
|
+
} from "./serverLanding";
|
|
12
|
+
|
|
13
|
+
const VALID = {
|
|
14
|
+
v: SERVER_ERROR_LANDING_VERSION,
|
|
15
|
+
status: 502,
|
|
16
|
+
code: "identity_provider_unreachable",
|
|
17
|
+
message: "identity provider unreachable: identity provider answered 400 Bad Request",
|
|
18
|
+
raw: { error: "identity provider unreachable: identity provider answered 400 Bad Request" },
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** The island exactly as a server injects it into index.html. */
|
|
22
|
+
function plantIsland(text: string, type = "application/json"): HTMLScriptElement {
|
|
23
|
+
const island = document.createElement("script");
|
|
24
|
+
island.id = SERVER_ERROR_LANDING_ID;
|
|
25
|
+
island.type = type;
|
|
26
|
+
island.textContent = text;
|
|
27
|
+
document.head.appendChild(island);
|
|
28
|
+
return island;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe("parseServerErrorLanding", () => {
|
|
32
|
+
it("accepts the documented island shape and keeps every field", () => {
|
|
33
|
+
expect(parseServerErrorLanding(JSON.stringify(VALID))).toEqual({
|
|
34
|
+
status: 502,
|
|
35
|
+
code: "identity_provider_unreachable",
|
|
36
|
+
message: VALID.message,
|
|
37
|
+
raw: VALID.raw,
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("trims the code so the chip never carries padding", () => {
|
|
42
|
+
expect(parseServerErrorLanding('{"v":1,"status":403,"code":" policy_denied ","message":"x"}')?.code)
|
|
43
|
+
.toBe("policy_denied");
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("derives nothing: an absent code is an empty code, an absent message an empty one", () => {
|
|
47
|
+
// The CARD owns the fallbacks (`http_<status>` chip, the generic
|
|
48
|
+
// description) so a host reading the payload sees what the server
|
|
49
|
+
// actually sent.
|
|
50
|
+
expect(parseServerErrorLanding('{"v":1,"status":404}')).toEqual({
|
|
51
|
+
status: 404,
|
|
52
|
+
code: "",
|
|
53
|
+
message: "",
|
|
54
|
+
});
|
|
55
|
+
// No `raw` key at all — not a `raw: undefined` that a lenient equality
|
|
56
|
+
// would let through: the card branches on `details !== undefined`.
|
|
57
|
+
expect(parseServerErrorLanding('{"v":1,"status":404}')).not.toHaveProperty("raw");
|
|
58
|
+
expect(parseServerErrorLanding('{"v":1,"status":404,"raw":null}')?.raw).toBeNull();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("ignores unknown keys (a newer server may add fields)", () => {
|
|
62
|
+
const parsed = parseServerErrorLanding('{"v":1,"status":403,"trace_id":"abc-123"}');
|
|
63
|
+
expect(parsed).toEqual({ status: 403, code: "", message: "" });
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("refuses a future island version instead of half-rendering it", () => {
|
|
67
|
+
expect(parseServerErrorLanding('{"v":2,"status":502,"message":"x"}')).toBeNull();
|
|
68
|
+
// A missing version is not "version 1 by default": the contract is
|
|
69
|
+
// explicit so a shape change can never be mistaken for this one.
|
|
70
|
+
expect(parseServerErrorLanding('{"status":502,"message":"x"}')).toBeNull();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("refuses anything that is not an error status", () => {
|
|
74
|
+
// A 2xx/3xx "handoff" would be a bug in the server; a 3xx is a
|
|
75
|
+
// redirect the browser must follow, never a landing.
|
|
76
|
+
for (const status of [200, 302, 399, 600, 700, 400.5, "502", null]) {
|
|
77
|
+
expect(parseServerErrorLanding(JSON.stringify({ v: 1, status })), `status ${status}`).toBeNull();
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("refuses malformed text and non-object payloads", () => {
|
|
82
|
+
for (const text of [
|
|
83
|
+
"",
|
|
84
|
+
"not json",
|
|
85
|
+
"null",
|
|
86
|
+
"[]",
|
|
87
|
+
// An array carrying an object that would pass on its own: the island
|
|
88
|
+
// is one object, not a list of them.
|
|
89
|
+
'[{"v":1,"status":502,"code":"","message":""}]',
|
|
90
|
+
'"a string"',
|
|
91
|
+
"42",
|
|
92
|
+
"{",
|
|
93
|
+
undefined as unknown as string,
|
|
94
|
+
]) {
|
|
95
|
+
expect(parseServerErrorLanding(text as string), String(text)).toBeNull();
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("reads escapes back: a payload with markup characters survives the round trip", () => {
|
|
100
|
+
const landing: HkServerErrorLanding = {
|
|
101
|
+
status: 400,
|
|
102
|
+
code: "bad_redirect",
|
|
103
|
+
message: "</script><script>alert(1)</script> & <b>",
|
|
104
|
+
raw: { next: "https://example.test/?a=1&b=2" },
|
|
105
|
+
};
|
|
106
|
+
const text = serializeServerErrorLanding(landing);
|
|
107
|
+
// Nothing in the island text can close the element or read as markup.
|
|
108
|
+
expect(text).not.toContain("<");
|
|
109
|
+
expect(text).not.toContain(">");
|
|
110
|
+
expect(text).not.toContain("&");
|
|
111
|
+
expect(parseServerErrorLanding(text)).toEqual(landing);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe("serializeServerErrorLanding", () => {
|
|
116
|
+
it("writes the version and omits `raw` when there is none", () => {
|
|
117
|
+
expect(serializeServerErrorLanding({ status: 404, code: "not_found", message: "no such file" }))
|
|
118
|
+
.toBe('{"v":1,"status":404,"code":"not_found","message":"no such file"}');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("keeps line and paragraph separators escaped (they end a JS string literal)", () => {
|
|
122
|
+
const text = serializeServerErrorLanding({ status: 500, code: "", message: "\u2028\u2029" });
|
|
123
|
+
expect(text).not.toMatch(/[\u2028\u2029]/);
|
|
124
|
+
expect(JSON.parse(text).message).toBe("\u2028\u2029");
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe("HkServerErrorLandingError", () => {
|
|
129
|
+
it("names itself after the wire code, or http_<status> without one", () => {
|
|
130
|
+
const withCode = new HkServerErrorLandingError({ status: 502, code: "upstream_down", message: "m" });
|
|
131
|
+
expect(withCode.name).toBe("upstream_down");
|
|
132
|
+
expect(withCode.message).toBe("m");
|
|
133
|
+
expect(withCode.status).toBe(502);
|
|
134
|
+
expect(withCode.raw).toBeUndefined();
|
|
135
|
+
|
|
136
|
+
const bare = new HkServerErrorLandingError({ status: 404, code: " ", message: "" });
|
|
137
|
+
expect(bare.name).toBe("http_404");
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("is an Error, so the ordinary error channels treat it like any other", () => {
|
|
141
|
+
const err = new HkServerErrorLandingError({ status: 503, code: "", message: "down", raw: { a: 1 } });
|
|
142
|
+
expect(err).toBeInstanceOf(Error);
|
|
143
|
+
expect(err.raw).toEqual({ a: 1 });
|
|
144
|
+
// The report reads `details`; the wire calls it `raw` — one value.
|
|
145
|
+
expect(err.details).toEqual({ a: 1 });
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
describe("readServerErrorLanding", () => {
|
|
150
|
+
it("returns null when the document carries no island", () => {
|
|
151
|
+
expect(readServerErrorLanding()).toBeNull();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("ignores a same-id element that is not a JSON data block", () => {
|
|
155
|
+
// An executable script is not read as a payload, whatever its id.
|
|
156
|
+
plantIsland(JSON.stringify(VALID), "module");
|
|
157
|
+
expect(readServerErrorLanding()).toBeNull();
|
|
158
|
+
document.getElementById(SERVER_ERROR_LANDING_ID)?.remove();
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("reads the island and CONSUMES it — a second read sees nothing", () => {
|
|
162
|
+
plantIsland(serializeServerErrorLanding(VALID));
|
|
163
|
+
const first = readServerErrorLanding();
|
|
164
|
+
expect(first).toEqual({
|
|
165
|
+
status: 502,
|
|
166
|
+
code: VALID.code,
|
|
167
|
+
message: VALID.message,
|
|
168
|
+
raw: VALID.raw,
|
|
169
|
+
});
|
|
170
|
+
expect(document.getElementById(SERVER_ERROR_LANDING_ID)).toBeNull();
|
|
171
|
+
expect(readServerErrorLanding()).toBeNull();
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("consumes a malformed island too, so it is not re-parsed on every install", () => {
|
|
175
|
+
plantIsland("{not json");
|
|
176
|
+
expect(readServerErrorLanding()).toBeNull();
|
|
177
|
+
expect(document.getElementById(SERVER_ERROR_LANDING_ID)).toBeNull();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("searches the given root, not just the document", () => {
|
|
181
|
+
const host = document.createElement("div");
|
|
182
|
+
document.body.appendChild(host);
|
|
183
|
+
const island = document.createElement("script");
|
|
184
|
+
island.id = SERVER_ERROR_LANDING_ID;
|
|
185
|
+
island.type = "application/json";
|
|
186
|
+
island.textContent = '{"v":1,"status":410,"message":"gone"}';
|
|
187
|
+
host.appendChild(island);
|
|
188
|
+
expect(readServerErrorLanding(host)?.status).toBe(410);
|
|
189
|
+
expect(host.children.length).toBe(0);
|
|
190
|
+
host.remove();
|
|
191
|
+
});
|
|
192
|
+
});
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { reportError, type HkReportedError } from "./state";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Server error landing handoff — the contract between a family SERVER and
|
|
5
|
+
* the SPA it serves, for the errors a browser reaches by NAVIGATION.
|
|
6
|
+
*
|
|
7
|
+
* `createErrorReporting` catches what happens after the app is running. It
|
|
8
|
+
* cannot catch the failure that never lets the app start: a server that
|
|
9
|
+
* answers a top-level navigation with a bare `{"error": …}` body (an OAuth
|
|
10
|
+
* callback whose token exchange failed, a revoked share link, a 502 from a
|
|
11
|
+
* restart window) shows the visitor raw JSON in the tab — no landing, no
|
|
12
|
+
* way back, nothing to copy but what the viewport holds (the 2026-11-26
|
|
13
|
+
* res.celestia.world report).
|
|
14
|
+
*
|
|
15
|
+
* The handoff closes that gap with the page itself as the carrier. The
|
|
16
|
+
* server answers with the SPA's own `index.html` at the REAL status code,
|
|
17
|
+
* with one JSON data island injected:
|
|
18
|
+
*
|
|
19
|
+
* ```html
|
|
20
|
+
* <script type="application/json" id="hk-server-error">
|
|
21
|
+
* {"v":1,"status":502,"code":"identity_provider_unreachable",
|
|
22
|
+
* "message":"identity provider unreachable: …","raw":{"error":"…"}}
|
|
23
|
+
* </script>
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* `createErrorReporting().install` reads the island (once — the element is
|
|
27
|
+
* consumed) and raises the same takeover a runtime error gets, with the
|
|
28
|
+
* HTTP status and the server's raw payload on the card. A data block is
|
|
29
|
+
* never executed, so it passes a `script-src 'self'` policy untouched, and
|
|
30
|
+
* {@link serializeServerErrorLanding} escapes the few characters that could
|
|
31
|
+
* let the payload close the island or read as markup.
|
|
32
|
+
*
|
|
33
|
+
* The island is the WHOLE contract: nothing here knows a URL scheme, a
|
|
34
|
+
* header name or a service, so any server facing a browser can produce it
|
|
35
|
+
* (the Rust side lives in plana's `plana-error-landing`).
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
/** Element id of the JSON island a server injects into its index.html. */
|
|
39
|
+
export const SERVER_ERROR_LANDING_ID = "hk-server-error";
|
|
40
|
+
|
|
41
|
+
/** The only island shape this build understands (`v` in the payload). */
|
|
42
|
+
export const SERVER_ERROR_LANDING_VERSION = 1;
|
|
43
|
+
|
|
44
|
+
/** Lowest status a handoff may carry: the error classes, nothing below. */
|
|
45
|
+
const MIN_HANDOFF_STATUS = 400;
|
|
46
|
+
const MAX_HANDOFF_STATUS = 599;
|
|
47
|
+
|
|
48
|
+
/** A validated server handoff: what the card shows, plus the raw body. */
|
|
49
|
+
export interface HkServerErrorLanding {
|
|
50
|
+
/** The HTTP status the server ANSWERED with — shown as the `HTTP <n>` chip. */
|
|
51
|
+
readonly status: number;
|
|
52
|
+
/** Wire error code shown as the code chip (`unknown_provider`). Empty
|
|
53
|
+
* falls back to `http_<status>`. */
|
|
54
|
+
readonly code: string;
|
|
55
|
+
/** The server's human-readable explanation — the card's description. */
|
|
56
|
+
readonly message: string;
|
|
57
|
+
/** The response body the server would have sent as JSON (an object, an
|
|
58
|
+
* array, a string …). Rendered in the raw-details pane; absent when the
|
|
59
|
+
* server had nothing to hand over. */
|
|
60
|
+
readonly raw?: unknown;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The error object a server handoff is reported through — the counterpart
|
|
65
|
+
* of the runtime errors the window hooks catch. `name` is the code chip, so
|
|
66
|
+
* it reads as the wire code when the server sent one and as `http_<status>`
|
|
67
|
+
* when it did not; `status` and `details` ride the report onto the card.
|
|
68
|
+
* `reportError` copies both off ANY object reported with the `server`
|
|
69
|
+
* source (duck-typed), so a host's own subclass can carry them without
|
|
70
|
+
* importing this one; a runtime error is never inspected for them.
|
|
71
|
+
*/
|
|
72
|
+
export class HkServerErrorLandingError extends Error {
|
|
73
|
+
readonly status: number;
|
|
74
|
+
readonly code: string;
|
|
75
|
+
readonly raw: unknown;
|
|
76
|
+
readonly landing: HkServerErrorLanding;
|
|
77
|
+
|
|
78
|
+
constructor(landing: HkServerErrorLanding) {
|
|
79
|
+
super(landing.message);
|
|
80
|
+
// Trimmed here too, not only in the parser: a host constructing this
|
|
81
|
+
// class by hand gets the same chip a parsed island would.
|
|
82
|
+
this.name = landing.code.trim() || `http_${landing.status}`;
|
|
83
|
+
this.status = landing.status;
|
|
84
|
+
this.code = landing.code.trim();
|
|
85
|
+
this.raw = landing.raw;
|
|
86
|
+
this.landing = landing;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The wire field under the name the report carries (`reportError` reads
|
|
90
|
+
* `details`, whatever class the error is). Same value as `raw`. */
|
|
91
|
+
get details(): unknown {
|
|
92
|
+
return this.raw;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Validate an island's text into a handoff, or `null` for anything this
|
|
98
|
+
* build must not render: a JSON syntax error, a non-object, another
|
|
99
|
+
* `v` (a future shape — ignored rather than half-rendered), a status
|
|
100
|
+
* outside the error classes. Unknown keys are ignored, so a newer server
|
|
101
|
+
* can add fields without breaking an older SPA.
|
|
102
|
+
*/
|
|
103
|
+
export function parseServerErrorLanding(text: string): HkServerErrorLanding | null {
|
|
104
|
+
let value: unknown;
|
|
105
|
+
try {
|
|
106
|
+
value = JSON.parse(text);
|
|
107
|
+
} catch {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
// An array is an object to `typeof` but never an island: the explicit
|
|
111
|
+
// check is depth behind the `v` lookup (a list has no `v`), kept so the
|
|
112
|
+
// shape is stated, not inferred.
|
|
113
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return null;
|
|
114
|
+
const record = value as Record<string, unknown>;
|
|
115
|
+
if (record.v !== SERVER_ERROR_LANDING_VERSION) return null;
|
|
116
|
+
const status = record.status;
|
|
117
|
+
// Two guards that overlap on purpose: `typeof` narrows the unknown for
|
|
118
|
+
// the compiler and stands on its own should the integer check ever be
|
|
119
|
+
// loosened; `isInteger` refuses fractions and numeric strings outright.
|
|
120
|
+
if (
|
|
121
|
+
typeof status !== "number" ||
|
|
122
|
+
!Number.isInteger(status) ||
|
|
123
|
+
status < MIN_HANDOFF_STATUS ||
|
|
124
|
+
status > MAX_HANDOFF_STATUS
|
|
125
|
+
) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
const landing: { status: number; code: string; message: string; raw?: unknown } = {
|
|
129
|
+
status,
|
|
130
|
+
code: typeof record.code === "string" ? record.code.trim() : "",
|
|
131
|
+
message: typeof record.message === "string" ? record.message : "",
|
|
132
|
+
};
|
|
133
|
+
if (record.raw !== undefined) landing.raw = record.raw;
|
|
134
|
+
return landing;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Serialize a handoff as island text. Besides `v`, the output differs from
|
|
139
|
+
* plain `JSON.stringify` in one way that matters: `<`, `>` and `&` leave as
|
|
140
|
+
* `\u003c`-style escapes, so no payload — however hostile — can close the
|
|
141
|
+
* `<script>` element or be read as markup by a parser that never sees the
|
|
142
|
+
* island as a data block (`JSON.parse` turns the escapes back).
|
|
143
|
+
*/
|
|
144
|
+
export function serializeServerErrorLanding(landing: HkServerErrorLanding): string {
|
|
145
|
+
const body =
|
|
146
|
+
landing.raw === undefined
|
|
147
|
+
? { v: SERVER_ERROR_LANDING_VERSION, status: landing.status, code: landing.code, message: landing.message }
|
|
148
|
+
: {
|
|
149
|
+
v: SERVER_ERROR_LANDING_VERSION,
|
|
150
|
+
status: landing.status,
|
|
151
|
+
code: landing.code,
|
|
152
|
+
message: landing.message,
|
|
153
|
+
raw: landing.raw,
|
|
154
|
+
};
|
|
155
|
+
return JSON.stringify(body)
|
|
156
|
+
.replace(/</g, "\\u003c")
|
|
157
|
+
.replace(/>/g, "\\u003e")
|
|
158
|
+
.replace(/&/g, "\\u0026")
|
|
159
|
+
.replace(/\u2028/g, "\\u2028")
|
|
160
|
+
.replace(/\u2029/g, "\\u2029");
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Read the handoff out of the document — once. The island is REMOVED on the
|
|
165
|
+
* first read, valid or not: a reinstall (HMR, a second app instance on the
|
|
166
|
+
* same document) must not re-raise a landing the visitor already saw or
|
|
167
|
+
* dismissed, and a malformed island must not be retried on every install.
|
|
168
|
+
* `null` when the document carries no island or the island is unusable.
|
|
169
|
+
*/
|
|
170
|
+
export function readServerErrorLanding(root: ParentNode = document): HkServerErrorLanding | null {
|
|
171
|
+
const island = root.querySelector<HTMLScriptElement>(
|
|
172
|
+
`script#${SERVER_ERROR_LANDING_ID}[type="application/json"]`,
|
|
173
|
+
);
|
|
174
|
+
if (!island) return null;
|
|
175
|
+
island.remove();
|
|
176
|
+
return parseServerErrorLanding(island.textContent ?? "");
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Raise the global error landing for a server handoff — the navigation-time
|
|
181
|
+
* counterpart of `reportGlobalError`. Goes through the same acceptance
|
|
182
|
+
* (`shouldReport`, first-error-wins, `onError`) as every other source, so
|
|
183
|
+
* a host filters or logs it exactly like a runtime error.
|
|
184
|
+
*/
|
|
185
|
+
export function reportServerErrorLanding(landing: HkServerErrorLanding): HkReportedError | null {
|
|
186
|
+
return reportError(new HkServerErrorLandingError(landing), "server");
|
|
187
|
+
}
|
|
@@ -2,8 +2,9 @@ import { ref, type Ref, type VNodeChild } from "vue";
|
|
|
2
2
|
|
|
3
3
|
import { reportHkRuntime, type HkRuntimeHandle } from "../runtime/registry";
|
|
4
4
|
|
|
5
|
-
/** Where a reported error came from.
|
|
6
|
-
|
|
5
|
+
/** Where a reported error came from. `server` = a handoff the server
|
|
6
|
+
* injected into this document (see `./serverLanding`), not a runtime error. */
|
|
7
|
+
export type HkErrorSource = "vue" | "window" | "rejection" | "manual" | "server";
|
|
7
8
|
|
|
8
9
|
/** A normalized error record carried by the global error landing. */
|
|
9
10
|
export interface HkReportedError {
|
|
@@ -14,6 +15,13 @@ export interface HkReportedError {
|
|
|
14
15
|
stack: string;
|
|
15
16
|
/** Vue's `info` string for errors surfaced through `app.config.errorHandler`. */
|
|
16
17
|
info?: string;
|
|
18
|
+
/** The HTTP status a SERVER handoff answered with — the `HTTP <n>` chip.
|
|
19
|
+
* Absent for runtime errors (nothing to show). */
|
|
20
|
+
status?: number;
|
|
21
|
+
/** A structured payload the reporter attached to the error, shown in the
|
|
22
|
+
* raw-details pane in place of the stack record: a server handoff's raw
|
|
23
|
+
* response body. Absent for runtime errors. */
|
|
24
|
+
details?: unknown;
|
|
17
25
|
}
|
|
18
26
|
|
|
19
27
|
export interface HkErrorReportingOptions {
|
|
@@ -118,6 +126,20 @@ export function reportError(err: unknown, source: HkErrorSource, info?: string):
|
|
|
118
126
|
stack: normalized.stack,
|
|
119
127
|
info,
|
|
120
128
|
};
|
|
129
|
+
// Structured attachments, for the SERVER source only. Duck-typed so a
|
|
130
|
+
// host raising a handoff itself (`reportGlobalError(err, "server")`)
|
|
131
|
+
// needs no import of hikari's class — but gated on the source so a
|
|
132
|
+
// runtime error that merely HAS a `status` (HTTP clients' error objects)
|
|
133
|
+
// or a `details` (validation libraries) field keeps rendering exactly as
|
|
134
|
+
// before: no status chip, the stack record still in the details pane.
|
|
135
|
+
// The HTTP status is the landing's status chip; `details` replaces the
|
|
136
|
+
// stack record in the raw-details pane. Reading a property off a
|
|
137
|
+
// primitive or null would throw, so only objects are inspected.
|
|
138
|
+
if (source === "server" && err !== null && typeof err === "object") {
|
|
139
|
+
const carried = err as { status?: unknown; details?: unknown };
|
|
140
|
+
if (typeof carried.status === "number") record.status = carried.status;
|
|
141
|
+
if (carried.details !== undefined) record.details = carried.details;
|
|
142
|
+
}
|
|
121
143
|
currentError.value = record;
|
|
122
144
|
return record;
|
|
123
145
|
}
|
package/src/index.ts
CHANGED
|
@@ -208,6 +208,16 @@ export {
|
|
|
208
208
|
type HkErrorSource,
|
|
209
209
|
type HkReportedError,
|
|
210
210
|
} from "./errorReporting";
|
|
211
|
+
export {
|
|
212
|
+
HkServerErrorLandingError as HServerErrorLandingError,
|
|
213
|
+
parseServerErrorLanding,
|
|
214
|
+
readServerErrorLanding,
|
|
215
|
+
reportServerErrorLanding,
|
|
216
|
+
serializeServerErrorLanding,
|
|
217
|
+
SERVER_ERROR_LANDING_ID,
|
|
218
|
+
SERVER_ERROR_LANDING_VERSION,
|
|
219
|
+
type HkServerErrorLanding as HServerErrorLanding,
|
|
220
|
+
} from "./errorReporting";
|
|
211
221
|
export { default as HDraggableList } from "./components/HkDraggableList";
|
|
212
222
|
export { default as HDraggableGrid } from "./components/HkDraggableGrid";
|
|
213
223
|
export { default as HSelectionGrid } from "./components/HkSelectionGrid";
|