From 92ceffb92b38eb17ca04987e1d7b0fb00893650d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Boduro=C4=9Flu?= <80620802+burakboduroglu@users.noreply.github.com> Date: Fri, 25 Sep 2026 14:56:38 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=94=81=20show=20the=20full=20load-?= =?UTF-8?q?error=20page=20when=20the=20session=20read=20fails?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - RouteGuard renders PageLoadError instead of a bare red box with "N sn sonra tekrar dene": cause, countdown retry and technical details, centered like every other page state - PageLoadError moves to its own file so the router fallback and the guard share it --- src/components/layout/page-load-error.tsx | 108 ++++++++++++++++++++++ src/components/layout/route-error.tsx | 105 +-------------------- src/components/layout/route-guard.tsx | 19 +--- 3 files changed, 115 insertions(+), 117 deletions(-) create mode 100644 src/components/layout/page-load-error.tsx diff --git a/src/components/layout/page-load-error.tsx b/src/components/layout/page-load-error.tsx new file mode 100644 index 0000000..5a3faef --- /dev/null +++ b/src/components/layout/page-load-error.tsx @@ -0,0 +1,108 @@ +import { useLocation } from "@tanstack/solid-router"; +import { createSignal, For, onCleanup } from "solid-js"; +import { ApiError, formatApiError } from "@/api/client"; +import { Button } from "@/components/ui/button"; +import { EmptyState } from "@/components/ui/empty-state"; +import { IconChevronDown } from "@/components/ui/icons"; +import { PAGE_STATE_CARD, PageCenter, pageStateText as tt } from "@/components/layout/page-center"; +import type { MessageKey } from "@/i18n/messages"; + +function errorKind(err: unknown): MessageKey { + if (err instanceof ApiError) { + if (err.status === 429) return "errors.pageLoad.kind.rateLimited"; + if (err.status === 403 && err.module != null) return "errors.pageLoad.kind.moduleOff"; + if (err.status === 401) return "errors.pageLoad.kind.unauthorized"; + if (err.status === 403) return "errors.pageLoad.kind.forbidden"; + if (err.status === 408) return "errors.pageLoad.kind.timeout"; + if (err.status === 503) return "errors.pageLoad.kind.unavailable"; + if (err.status >= 500) return "errors.pageLoad.kind.server"; + return "errors.pageLoad.kind.request"; + } + // fetch() rejects with a TypeError when the request never got an answer. + if (err instanceof TypeError && /fetch|network|load failed/i.test(err.message)) { + return typeof navigator !== "undefined" && navigator.onLine === false + ? "errors.pageLoad.kind.offline" + : "errors.pageLoad.kind.network"; + } + return "errors.pageLoad.kind.render"; +} + +function rawMessage(err: unknown): string { + if (err instanceof ApiError) return err.message; + if (err instanceof Error) return err.message ? `${err.name}: ${err.message}` : err.name; + return String(err); +} + +/** + * A page that could not load: the reason, a retry that waits out a + * rate limit, and the technical details behind a disclosure. Used by the + * router's error component and by RouteGuard when the session read fails. + */ +export function PageLoadError(props: { error: unknown; reset: () => void }) { + const location = useLocation(); + const api = props.error instanceof ApiError ? props.error : null; + + // A rate-limited or unavailable answer says when a retry can work; retrying + // sooner only earns another refusal, so the button waits it out. + const [wait, setWait] = createSignal( + api && (api.status === 429 || api.status === 503) && api.retryAfter != null ? Math.max(0, Math.ceil(api.retryAfter)) : 0, + ); + if (wait() > 0) { + const timer = setInterval(() => { + setWait((s) => { + const next = Math.max(0, s - 1); + if (next === 0) clearInterval(timer); + return next; + }); + }, 1000); + onCleanup(() => clearInterval(timer)); + } + + const rows = (): { label: string; value: string; mono?: boolean }[] => { + const list: { label: string; value: string; mono?: boolean }[] = []; + list.push({ label: tt("errors.pageLoad.kind"), value: tt(errorKind(props.error)) }); + if (api) list.push({ label: tt("errors.pageLoad.status"), value: String(api.status), mono: true }); + if (api?.code) list.push({ label: tt("errors.pageLoad.code"), value: api.code, mono: true }); + if (api?.module) list.push({ label: tt("errors.pageLoad.module"), value: api.module, mono: true }); + if (api?.retryAfter != null) + list.push({ label: tt("errors.pageLoad.retryAfter"), value: tt("errors.pageLoad.seconds", { seconds: api.retryAfter }) }); + list.push({ label: tt("errors.pageLoad.page"), value: location().href, mono: true }); + const raw = rawMessage(props.error).trim(); + if (raw) list.push({ label: tt("errors.pageLoad.message"), value: raw, mono: true }); + return list; + }; + + return ( + + + +
+ + {tt("errors.pageLoad.details")} + + +
+ + {(row) => ( + <> +
{row.label}
+
{row.value}
+ + )} +
+
+
+ + } + /> +
+ ); +} diff --git a/src/components/layout/route-error.tsx b/src/components/layout/route-error.tsx index 7582091..d3e28b0 100644 --- a/src/components/layout/route-error.tsx +++ b/src/components/layout/route-error.tsx @@ -1,12 +1,6 @@ -import { useLocation, type ErrorComponentProps } from "@tanstack/solid-router"; -import { createSignal, For, onCleanup } from "solid-js"; -import { ApiError, formatApiError } from "@/api/client"; -import { Button } from "@/components/ui/button"; -import { EmptyState } from "@/components/ui/empty-state"; -import { IconChevronDown } from "@/components/ui/icons"; -import { PAGE_STATE_CARD, PageCenter, pageStateText as tt } from "@/components/layout/page-center"; +import type { ErrorComponentProps } from "@tanstack/solid-router"; +import { PageLoadError } from "@/components/layout/page-load-error"; import { RecordNotFound } from "@/components/layout/record-not-found"; -import type { MessageKey } from "@/i18n/messages"; import { isNotFoundError } from "@/lib/record-list-path"; /** @@ -27,98 +21,3 @@ export function RouteErrorFallback(props: ErrorComponentProps) { console.error("Route render error:", props.error); return ; } - -function errorKind(err: unknown): MessageKey { - if (err instanceof ApiError) { - if (err.status === 429) return "errors.pageLoad.kind.rateLimited"; - if (err.status === 403 && err.module != null) return "errors.pageLoad.kind.moduleOff"; - if (err.status === 401) return "errors.pageLoad.kind.unauthorized"; - if (err.status === 403) return "errors.pageLoad.kind.forbidden"; - if (err.status === 408) return "errors.pageLoad.kind.timeout"; - if (err.status === 503) return "errors.pageLoad.kind.unavailable"; - if (err.status >= 500) return "errors.pageLoad.kind.server"; - return "errors.pageLoad.kind.request"; - } - // fetch() rejects with a TypeError when the request never got an answer. - if (err instanceof TypeError && /fetch|network|load failed/i.test(err.message)) { - return typeof navigator !== "undefined" && navigator.onLine === false - ? "errors.pageLoad.kind.offline" - : "errors.pageLoad.kind.network"; - } - return "errors.pageLoad.kind.render"; -} - -function rawMessage(err: unknown): string { - if (err instanceof ApiError) return err.message; - if (err instanceof Error) return err.message ? `${err.name}: ${err.message}` : err.name; - return String(err); -} - -function PageLoadError(props: { error: unknown; reset: () => void }) { - const location = useLocation(); - const api = props.error instanceof ApiError ? props.error : null; - - // A rate-limited or unavailable answer says when a retry can work; retrying - // sooner only earns another refusal, so the button waits it out. - const [wait, setWait] = createSignal( - api && (api.status === 429 || api.status === 503) && api.retryAfter != null ? Math.max(0, Math.ceil(api.retryAfter)) : 0, - ); - if (wait() > 0) { - const timer = setInterval(() => { - setWait((s) => { - const next = Math.max(0, s - 1); - if (next === 0) clearInterval(timer); - return next; - }); - }, 1000); - onCleanup(() => clearInterval(timer)); - } - - const rows = (): { label: string; value: string; mono?: boolean }[] => { - const list: { label: string; value: string; mono?: boolean }[] = []; - list.push({ label: tt("errors.pageLoad.kind"), value: tt(errorKind(props.error)) }); - if (api) list.push({ label: tt("errors.pageLoad.status"), value: String(api.status), mono: true }); - if (api?.code) list.push({ label: tt("errors.pageLoad.code"), value: api.code, mono: true }); - if (api?.module) list.push({ label: tt("errors.pageLoad.module"), value: api.module, mono: true }); - if (api?.retryAfter != null) - list.push({ label: tt("errors.pageLoad.retryAfter"), value: tt("errors.pageLoad.seconds", { seconds: api.retryAfter }) }); - list.push({ label: tt("errors.pageLoad.page"), value: location().href, mono: true }); - const raw = rawMessage(props.error).trim(); - if (raw) list.push({ label: tt("errors.pageLoad.message"), value: raw, mono: true }); - return list; - }; - - return ( - - - -
- - {tt("errors.pageLoad.details")} - - -
- - {(row) => ( - <> -
{row.label}
-
{row.value}
- - )} -
-
-
- - } - /> -
- ); -} diff --git a/src/components/layout/route-guard.tsx b/src/components/layout/route-guard.tsx index 75d603d..483f77b 100644 --- a/src/components/layout/route-guard.tsx +++ b/src/components/layout/route-guard.tsx @@ -1,9 +1,7 @@ import { Link, Navigate, useLocation } from "@tanstack/solid-router"; import { type ParentProps, Show } from "solid-js"; import type { Role } from "@/api/client"; -import { formatApiError } from "@/api/client"; -import { Alert } from "@/components/ui/alert"; -import { Button } from "@/components/ui/button"; +import { PageLoadError } from "@/components/layout/page-load-error"; import { IconHome, IconLock } from "@/components/ui/icons"; import { PageSpinner } from "@/components/ui/page-spinner"; import { hasExactRole, hasMinRole, roleInRange } from "@/lib/roles"; @@ -99,7 +97,6 @@ export function RouteGuard( }>, ) { const auth = useAuth(); - const t = useT(); const allowed = (role: Role) => { if (props.exactRole) return hasExactRole(role, props.exactRole); @@ -109,16 +106,10 @@ export function RouteGuard( return ( }> - -
-

{formatApiError(auth.error())}

- -
- - }> + {/* A failed session read (often a rate limit) gets the same full-page + state as a failed page: the cause, a retry that waits out the + limit, and the details — not a bare red box. */} + void auth.refresh()} />}> }> {(u) => ( }>