UNPKG

react-router

Version:
30 lines (29 loc) 1.39 kB
/** * react-router v8.0.0 * * Copyright (c) Remix Software Inc. * * This source code is licensed under the MIT license found in the * LICENSE.md file in the root directory of this source tree. * * @license MIT */ import { ErrorResponseImpl } from "./router/utils.js"; //#region lib/errors.ts const ERROR_DIGEST_BASE = "REACT_ROUTER_ERROR"; const ERROR_DIGEST_REDIRECT = "REDIRECT"; const ERROR_DIGEST_ROUTE_ERROR_RESPONSE = "ROUTE_ERROR_RESPONSE"; function decodeRedirectErrorDigest(digest) { if (digest.startsWith(`${ERROR_DIGEST_BASE}:${ERROR_DIGEST_REDIRECT}:{`)) try { let parsed = JSON.parse(digest.slice(28)); if (typeof parsed === "object" && parsed && typeof parsed.status === "number" && typeof parsed.statusText === "string" && typeof parsed.location === "string" && typeof parsed.reloadDocument === "boolean" && typeof parsed.replace === "boolean") return parsed; } catch {} } function decodeRouteErrorResponseDigest(digest) { if (digest.startsWith(`${ERROR_DIGEST_BASE}:${ERROR_DIGEST_ROUTE_ERROR_RESPONSE}:{`)) try { let parsed = JSON.parse(digest.slice(40)); if (typeof parsed === "object" && parsed && typeof parsed.status === "number" && typeof parsed.statusText === "string") return new ErrorResponseImpl(parsed.status, parsed.statusText, parsed.data); } catch {} } //#endregion export { decodeRedirectErrorDigest, decodeRouteErrorResponseDigest };