useRouteError return type #9628
-
Hi, I've been following the Should the return value actually be an Object { status: 404, statusText: "Not Found", data: null }
data: null
status: 404
statusText: "Not Found"
<prototype>: Object { … }
constructor: class ErrorResponse { constructor(status, statusText, data) }
length: 3
name: "ErrorResponse"
prototype: Object { … }
<prototype>: function ()
<prototype>: Object { … }
__defineGetter__: function __defineGetter__()
__defineSetter__: function __defineSetter__()
__lookupGetter__: function __lookupGetter__()
__lookupSetter__: function __lookupSetter__()
__proto__:
constructor: function Object()
hasOwnProperty: function hasOwnProperty()
isPrototypeOf: function isPrototypeOf()
propertyIsEnumerable: function propertyIsEnumerable()
toLocaleString: function toLocaleString()
toString: function toString()
valueOf: function valueOf()
<get __proto__()>: function __proto__()
<set __proto__()>: function __proto__()
ErrorPage.tsx:5 Should I be type checking the unknown value by adding a type: import { useRouteError } from "react-router-dom";
type ErrorResponse = {
data: any;
status: number;
statusText: string;
message?: string;
};
const errorCheck = (error: any): error is ErrorResponse => {
return "data" in error && "status" in error && "statusText" in error;
};
export default function ErrorPage() {
const error: any = useRouteError();
console.log(error);
// console.log(typeof error);
if (errorCheck(error)) {
return (
<div id="error-page">
<h1>Oops! Page not found</h1>
<p>Sorry the route you are looking for does not exist.</p>
<p>
<i>{error.statusText || error.message}</i>
</p>
</div>
);
} else {
return <></>;
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
Hi, I don't have an answer for your question per se, but you could look into using the Based on isRouteErrorResponse's documentation, you could write something like this: import { isRouteErrorResponse, useRouteError } from "react-router-dom";
export function ErrorPage() {
const error = useRouteError();
if (isRouteErrorResponse(error)) {
if (error.status === 401) {
// ...
}
else if (error.status === 404) {
// ...
}
return (
<div id="error-page">
<h1>Oops! {error.status}</h1>
<p>{error.statusText}</p>
{error.data?.message && (
<p>
<i>{error.data.message}</i>
</p>
)}
</div>
);
} else if (error instanceof Error) {
return (
<div id="error-page">
<h1>Oops! Unexpected Error</h1>
<p>Something went wrong.</p>
<p>
<i>{error.message}</i>
</p>
</div>
);
} else {
return <></>;
}
} I hope this helps! EDIT: Added missing |
Beta Was this translation helpful? Give feedback.
-
Is any updates with this hook? Any type definition can be used for it? Or we good just define |
Beta Was this translation helpful? Give feedback.
-
import { isRouteErrorResponse, useRouteError } from 'react-router-dom';
function MyErrorElement() {
let error = useRouteError();
if (isRouteErrorResponse(error)) {
return <p>{error.status} {error.statusText}</p>
}
return <p>{error.message || "Unknown Error"}</p>
} But there is nothing stopping you from throwing other types of values, so we can't infer the type of |
Beta Was this translation helpful? Give feedback.
-
I came across this from a tutorial while learning TypeScript with React Router. function errorMessage(error: unknown): string {
if (isRouteErrorResponse(error)) {
return `${error.status} ${error.statusText}`
} else if (error instanceof Error) {
return error.message
} else if (typeof error === 'string') {
return error
} else {
console.error(error)
return 'Unknown error'
}
} You can get more control by mixing these checks into your Component, but that is often overkill, especially if you're just following a long with a tutorial. |
Beta Was this translation helpful? Give feedback.
Hi,
I don't have an answer for your question per se, but you could look into using the
isRouteErrorResponse
function fromreact-router-dom
.Based on isRouteErrorResponse's documentation, you could write something like this: