🔁 show the full load-error page when the session read fails
Browse files at this commit92ceffb92b38eb17ca04987e1d7b0fb00893650dParent: 7ef0f99- fix
- 🔁 show the full load-error page when the session read fails
3 changed files +115 -117
- src/components/layout/page-load-error.tsx +108 -0
- src/components/layout/route-error.tsx +2 -103
- src/components/layout/route-guard.tsx +5 -14
src/components/layout/page-load-error.tsxadded+108 -0
| @@ -0,0 +1,108 @@ | |||
| 1 | +import { useLocation } from "@tanstack/solid-router"; | ||
| 2 | +import { createSignal, For, onCleanup } from "solid-js"; | ||
| 3 | +import { ApiError, formatApiError } from "@/api/client"; | ||
| 4 | +import { Button } from "@/components/ui/button"; | ||
| 5 | +import { EmptyState } from "@/components/ui/empty-state"; | ||
| 6 | +import { IconChevronDown } from "@/components/ui/icons"; | ||
| 7 | +import { PAGE_STATE_CARD, PageCenter, pageStateText as tt } from "@/components/layout/page-center"; | ||
| 8 | +import type { MessageKey } from "@/i18n/messages"; | ||
| 9 | + | ||
| 10 | +function errorKind(err: unknown): MessageKey { | ||
| 11 | + if (err instanceof ApiError) { | ||
| 12 | + if (err.status === 429) return "errors.pageLoad.kind.rateLimited"; | ||
| 13 | + if (err.status === 403 && err.module != null) return "errors.pageLoad.kind.moduleOff"; | ||
| 14 | + if (err.status === 401) return "errors.pageLoad.kind.unauthorized"; | ||
| 15 | + if (err.status === 403) return "errors.pageLoad.kind.forbidden"; | ||
| 16 | + if (err.status === 408) return "errors.pageLoad.kind.timeout"; | ||
| 17 | + if (err.status === 503) return "errors.pageLoad.kind.unavailable"; | ||
| 18 | + if (err.status >= 500) return "errors.pageLoad.kind.server"; | ||
| 19 | + return "errors.pageLoad.kind.request"; | ||
| 20 | + } | ||
| 21 | + // fetch() rejects with a TypeError when the request never got an answer. | ||
| 22 | + if (err instanceof TypeError && /fetch|network|load failed/i.test(err.message)) { | ||
| 23 | + return typeof navigator !== "undefined" && navigator.onLine === false | ||
| 24 | + ? "errors.pageLoad.kind.offline" | ||
| 25 | + : "errors.pageLoad.kind.network"; | ||
| 26 | + } | ||
| 27 | + return "errors.pageLoad.kind.render"; | ||
| 28 | +} | ||
| 29 | + | ||
| 30 | +function rawMessage(err: unknown): string { | ||
| 31 | + if (err instanceof ApiError) return err.message; | ||
| 32 | + if (err instanceof Error) return err.message ? `${err.name}: ${err.message}` : err.name; | ||
| 33 | + return String(err); | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +/** | ||
| 37 | + * A page that could not load: the reason, a retry that waits out a | ||
| 38 | + * rate limit, and the technical details behind a disclosure. Used by the | ||
| 39 | + * router's error component and by RouteGuard when the session read fails. | ||
| 40 | + */ | ||
| 41 | +export function PageLoadError(props: { error: unknown; reset: () => void }) { | ||
| 42 | + const location = useLocation(); | ||
| 43 | + const api = props.error instanceof ApiError ? props.error : null; | ||
| 44 | + | ||
| 45 | + // A rate-limited or unavailable answer says when a retry can work; retrying | ||
| 46 | + // sooner only earns another refusal, so the button waits it out. | ||
| 47 | + const [wait, setWait] = createSignal( | ||
| 48 | + api && (api.status === 429 || api.status === 503) && api.retryAfter != null ? Math.max(0, Math.ceil(api.retryAfter)) : 0, | ||
| 49 | + ); | ||
| 50 | + if (wait() > 0) { | ||
| 51 | + const timer = setInterval(() => { | ||
| 52 | + setWait((s) => { | ||
| 53 | + const next = Math.max(0, s - 1); | ||
| 54 | + if (next === 0) clearInterval(timer); | ||
| 55 | + return next; | ||
| 56 | + }); | ||
| 57 | + }, 1000); | ||
| 58 | + onCleanup(() => clearInterval(timer)); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + const rows = (): { label: string; value: string; mono?: boolean }[] => { | ||
| 62 | + const list: { label: string; value: string; mono?: boolean }[] = []; | ||
| 63 | + list.push({ label: tt("errors.pageLoad.kind"), value: tt(errorKind(props.error)) }); | ||
| 64 | + if (api) list.push({ label: tt("errors.pageLoad.status"), value: String(api.status), mono: true }); | ||
| 65 | + if (api?.code) list.push({ label: tt("errors.pageLoad.code"), value: api.code, mono: true }); | ||
| 66 | + if (api?.module) list.push({ label: tt("errors.pageLoad.module"), value: api.module, mono: true }); | ||
| 67 | + if (api?.retryAfter != null) | ||
| 68 | + list.push({ label: tt("errors.pageLoad.retryAfter"), value: tt("errors.pageLoad.seconds", { seconds: api.retryAfter }) }); | ||
| 69 | + list.push({ label: tt("errors.pageLoad.page"), value: location().href, mono: true }); | ||
| 70 | + const raw = rawMessage(props.error).trim(); | ||
| 71 | + if (raw) list.push({ label: tt("errors.pageLoad.message"), value: raw, mono: true }); | ||
| 72 | + return list; | ||
| 73 | + }; | ||
| 74 | + | ||
| 75 | + return ( | ||
| 76 | + <PageCenter alert> | ||
| 77 | + <EmptyState | ||
| 78 | + kind="error" | ||
| 79 | + class={PAGE_STATE_CARD} | ||
| 80 | + title={tt("errors.pageLoad.title")} | ||
| 81 | + description={formatApiError(props.error)} | ||
| 82 | + action={ | ||
| 83 | + <div class="flex w-full flex-col items-center gap-5"> | ||
| 84 | + <Button type="button" size="sm" class="h-9 rounded-lg px-4" disabled={wait() > 0} onClick={() => props.reset()}> | ||
| 85 | + {wait() > 0 ? tt("errors.pageLoad.retryIn", { seconds: wait() }) : tt("errors.pageLoad.retry")} | ||
| 86 | + </Button> | ||
| 87 | + <details class="group w-full rounded-lg border border-border-hairline bg-surface-tint/60 text-left"> | ||
| 88 | + <summary class="flex cursor-pointer list-none items-center justify-between gap-3 rounded-lg px-4 py-2.5 text-sm font-medium text-text-strong outline-hidden focus-visible:ring-2 focus-visible:ring-ring [&::-webkit-details-marker]:hidden"> | ||
| 89 | + <span>{tt("errors.pageLoad.details")}</span> | ||
| 90 | + <IconChevronDown class="h-4 w-4 shrink-0 text-text-subtle transition-transform group-open:rotate-180" /> | ||
| 91 | + </summary> | ||
| 92 | + <dl class="grid grid-cols-1 gap-x-4 gap-y-1 border-t border-border-hairline px-4 py-3 text-xs sm:grid-cols-[max-content_1fr] sm:gap-y-2"> | ||
| 93 | + <For each={rows()}> | ||
| 94 | + {(row) => ( | ||
| 95 | + <> | ||
| 96 | + <dt class="pt-1 font-medium text-text-subtle sm:pt-0">{row.label}</dt> | ||
| 97 | + <dd class={row.mono ? "break-all font-mono text-text-strong" : "text-text-strong"}>{row.value}</dd> | ||
| 98 | + </> | ||
| 99 | + )} | ||
| 100 | + </For> | ||
| 101 | + </dl> | ||
| 102 | + </details> | ||
| 103 | + </div> | ||
| 104 | + } | ||
| 105 | + /> | ||
| 106 | + </PageCenter> | ||
| 107 | + ); | ||
| 108 | +} | ||
src/components/layout/route-error.tsxmodified+2 -103
| @@ -1,12 +1,6 @@ | |||
| 1 | -import { useLocation, type ErrorComponentProps } from "@tanstack/solid-router"; | 1 | +import type { ErrorComponentProps } from "@tanstack/solid-router"; |
| 2 | -import { createSignal, For, onCleanup } from "solid-js"; | 2 | +import { PageLoadError } from "@/components/layout/page-load-error"; |
| 3 | -import { ApiError, formatApiError } from "@/api/client"; | ||
| 4 | -import { Button } from "@/components/ui/button"; | ||
| 5 | -import { EmptyState } from "@/components/ui/empty-state"; | ||
| 6 | -import { IconChevronDown } from "@/components/ui/icons"; | ||
| 7 | -import { PAGE_STATE_CARD, PageCenter, pageStateText as tt } from "@/components/layout/page-center"; | ||
| 8 | import { RecordNotFound } from "@/components/layout/record-not-found"; | 3 | import { RecordNotFound } from "@/components/layout/record-not-found"; |
| 9 | -import type { MessageKey } from "@/i18n/messages"; | ||
| 10 | import { isNotFoundError } from "@/lib/record-list-path"; | 4 | import { isNotFoundError } from "@/lib/record-list-path"; |
| 11 | 5 | ||
| 12 | /** | 6 | /** |
| @@ -27,98 +21,3 @@ export function RouteErrorFallback(props: ErrorComponentProps) { | |||
| 27 | console.error("Route render error:", props.error); | 21 | console.error("Route render error:", props.error); |
| 28 | return <PageLoadError error={props.error} reset={props.reset} />; | 22 | return <PageLoadError error={props.error} reset={props.reset} />; |
| 29 | } | 23 | } |
| 30 | - | ||
| 31 | -function errorKind(err: unknown): MessageKey { | ||
| 32 | - if (err instanceof ApiError) { | ||
| 33 | - if (err.status === 429) return "errors.pageLoad.kind.rateLimited"; | ||
| 34 | - if (err.status === 403 && err.module != null) return "errors.pageLoad.kind.moduleOff"; | ||
| 35 | - if (err.status === 401) return "errors.pageLoad.kind.unauthorized"; | ||
| 36 | - if (err.status === 403) return "errors.pageLoad.kind.forbidden"; | ||
| 37 | - if (err.status === 408) return "errors.pageLoad.kind.timeout"; | ||
| 38 | - if (err.status === 503) return "errors.pageLoad.kind.unavailable"; | ||
| 39 | - if (err.status >= 500) return "errors.pageLoad.kind.server"; | ||
| 40 | - return "errors.pageLoad.kind.request"; | ||
| 41 | - } | ||
| 42 | - // fetch() rejects with a TypeError when the request never got an answer. | ||
| 43 | - if (err instanceof TypeError && /fetch|network|load failed/i.test(err.message)) { | ||
| 44 | - return typeof navigator !== "undefined" && navigator.onLine === false | ||
| 45 | - ? "errors.pageLoad.kind.offline" | ||
| 46 | - : "errors.pageLoad.kind.network"; | ||
| 47 | - } | ||
| 48 | - return "errors.pageLoad.kind.render"; | ||
| 49 | -} | ||
| 50 | - | ||
| 51 | -function rawMessage(err: unknown): string { | ||
| 52 | - if (err instanceof ApiError) return err.message; | ||
| 53 | - if (err instanceof Error) return err.message ? `${err.name}: ${err.message}` : err.name; | ||
| 54 | - return String(err); | ||
| 55 | -} | ||
| 56 | - | ||
| 57 | -function PageLoadError(props: { error: unknown; reset: () => void }) { | ||
| 58 | - const location = useLocation(); | ||
| 59 | - const api = props.error instanceof ApiError ? props.error : null; | ||
| 60 | - | ||
| 61 | - // A rate-limited or unavailable answer says when a retry can work; retrying | ||
| 62 | - // sooner only earns another refusal, so the button waits it out. | ||
| 63 | - const [wait, setWait] = createSignal( | ||
| 64 | - api && (api.status === 429 || api.status === 503) && api.retryAfter != null ? Math.max(0, Math.ceil(api.retryAfter)) : 0, | ||
| 65 | - ); | ||
| 66 | - if (wait() > 0) { | ||
| 67 | - const timer = setInterval(() => { | ||
| 68 | - setWait((s) => { | ||
| 69 | - const next = Math.max(0, s - 1); | ||
| 70 | - if (next === 0) clearInterval(timer); | ||
| 71 | - return next; | ||
| 72 | - }); | ||
| 73 | - }, 1000); | ||
| 74 | - onCleanup(() => clearInterval(timer)); | ||
| 75 | - } | ||
| 76 | - | ||
| 77 | - const rows = (): { label: string; value: string; mono?: boolean }[] => { | ||
| 78 | - const list: { label: string; value: string; mono?: boolean }[] = []; | ||
| 79 | - list.push({ label: tt("errors.pageLoad.kind"), value: tt(errorKind(props.error)) }); | ||
| 80 | - if (api) list.push({ label: tt("errors.pageLoad.status"), value: String(api.status), mono: true }); | ||
| 81 | - if (api?.code) list.push({ label: tt("errors.pageLoad.code"), value: api.code, mono: true }); | ||
| 82 | - if (api?.module) list.push({ label: tt("errors.pageLoad.module"), value: api.module, mono: true }); | ||
| 83 | - if (api?.retryAfter != null) | ||
| 84 | - list.push({ label: tt("errors.pageLoad.retryAfter"), value: tt("errors.pageLoad.seconds", { seconds: api.retryAfter }) }); | ||
| 85 | - list.push({ label: tt("errors.pageLoad.page"), value: location().href, mono: true }); | ||
| 86 | - const raw = rawMessage(props.error).trim(); | ||
| 87 | - if (raw) list.push({ label: tt("errors.pageLoad.message"), value: raw, mono: true }); | ||
| 88 | - return list; | ||
| 89 | - }; | ||
| 90 | - | ||
| 91 | - return ( | ||
| 92 | - <PageCenter alert> | ||
| 93 | - <EmptyState | ||
| 94 | - kind="error" | ||
| 95 | - class={PAGE_STATE_CARD} | ||
| 96 | - title={tt("errors.pageLoad.title")} | ||
| 97 | - description={formatApiError(props.error)} | ||
| 98 | - action={ | ||
| 99 | - <div class="flex w-full flex-col items-center gap-5"> | ||
| 100 | - <Button type="button" size="sm" class="h-9 rounded-lg px-4" disabled={wait() > 0} onClick={() => props.reset()}> | ||
| 101 | - {wait() > 0 ? tt("errors.pageLoad.retryIn", { seconds: wait() }) : tt("errors.pageLoad.retry")} | ||
| 102 | - </Button> | ||
| 103 | - <details class="group w-full rounded-lg border border-border-hairline bg-surface-tint/60 text-left"> | ||
| 104 | - <summary class="flex cursor-pointer list-none items-center justify-between gap-3 rounded-lg px-4 py-2.5 text-sm font-medium text-text-strong outline-hidden focus-visible:ring-2 focus-visible:ring-ring [&::-webkit-details-marker]:hidden"> | ||
| 105 | - <span>{tt("errors.pageLoad.details")}</span> | ||
| 106 | - <IconChevronDown class="h-4 w-4 shrink-0 text-text-subtle transition-transform group-open:rotate-180" /> | ||
| 107 | - </summary> | ||
| 108 | - <dl class="grid grid-cols-1 gap-x-4 gap-y-1 border-t border-border-hairline px-4 py-3 text-xs sm:grid-cols-[max-content_1fr] sm:gap-y-2"> | ||
| 109 | - <For each={rows()}> | ||
| 110 | - {(row) => ( | ||
| 111 | - <> | ||
| 112 | - <dt class="pt-1 font-medium text-text-subtle sm:pt-0">{row.label}</dt> | ||
| 113 | - <dd class={row.mono ? "break-all font-mono text-text-strong" : "text-text-strong"}>{row.value}</dd> | ||
| 114 | - </> | ||
| 115 | - )} | ||
| 116 | - </For> | ||
| 117 | - </dl> | ||
| 118 | - </details> | ||
| 119 | - </div> | ||
| 120 | - } | ||
| 121 | - /> | ||
| 122 | - </PageCenter> | ||
| 123 | - ); | ||
| 124 | -} | ||
src/components/layout/route-guard.tsxmodified+5 -14
| @@ -1,9 +1,7 @@ | |||
| 1 | import { Link, Navigate, useLocation } from "@tanstack/solid-router"; | 1 | import { Link, Navigate, useLocation } from "@tanstack/solid-router"; |
| 2 | import { type ParentProps, Show } from "solid-js"; | 2 | import { type ParentProps, Show } from "solid-js"; |
| 3 | import type { Role } from "@/api/client"; | 3 | import type { Role } from "@/api/client"; |
| 4 | -import { formatApiError } from "@/api/client"; | 4 | +import { PageLoadError } from "@/components/layout/page-load-error"; |
| 5 | -import { Alert } from "@/components/ui/alert"; | ||
| 6 | -import { Button } from "@/components/ui/button"; | ||
| 7 | import { IconHome, IconLock } from "@/components/ui/icons"; | 5 | import { IconHome, IconLock } from "@/components/ui/icons"; |
| 8 | import { PageSpinner } from "@/components/ui/page-spinner"; | 6 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 9 | import { hasExactRole, hasMinRole, roleInRange } from "@/lib/roles"; | 7 | import { hasExactRole, hasMinRole, roleInRange } from "@/lib/roles"; |
| @@ -99,7 +97,6 @@ export function RouteGuard( | |||
| 99 | }>, | 97 | }>, |
| 100 | ) { | 98 | ) { |
| 101 | const auth = useAuth(); | 99 | const auth = useAuth(); |
| 102 | - const t = useT(); | ||
| 103 | 100 | ||
| 104 | const allowed = (role: Role) => { | 101 | const allowed = (role: Role) => { |
| 105 | if (props.exactRole) return hasExactRole(role, props.exactRole); | 102 | if (props.exactRole) return hasExactRole(role, props.exactRole); |
| @@ -109,16 +106,10 @@ export function RouteGuard( | |||
| 109 | 106 | ||
| 110 | return ( | 107 | return ( |
| 111 | <Show when={!auth.loading()} fallback={<PageSpinner />}> | 108 | <Show when={!auth.loading()} fallback={<PageSpinner />}> |
| 112 | - <Show when={!auth.error()} fallback={ | 109 | + {/* A failed session read (often a rate limit) gets the same full-page |
| 113 | - <Alert variant="destructive"> | 110 | + state as a failed page: the cause, a retry that waits out the |
| 114 | - <div class="space-y-3"> | 111 | + limit, and the details — not a bare red box. */} |
| 115 | - <p>{formatApiError(auth.error())}</p> | 112 | + <Show when={!auth.error()} fallback={<PageLoadError error={auth.error()} reset={() => void auth.refresh()} />}> |
| 116 | - <Button variant="outline" size="sm" onClick={() => void auth.refresh()}> | ||
| 117 | - {t("common.tryAgain")} | ||
| 118 | - </Button> | ||
| 119 | - </div> | ||
| 120 | - </Alert> | ||
| 121 | - }> | ||
| 122 | <Show when={auth.user()} fallback={<Navigate to="/login" />}> | 113 | <Show when={auth.user()} fallback={<Navigate to="/login" />}> |
| 123 | {(u) => ( | 114 | {(u) => ( |
| 124 | <Show when={allowed(u().role)} fallback={<AccessDeniedCard exactRole={props.exactRole} userRole={u().role} />}> | 115 | <Show when={allowed(u().role)} fallback={<AccessDeniedCard exactRole={props.exactRole} userRole={u().role} />}> |