remix-run / react-router-website

The React Router website
https://reactrouter.com
MIT License
152 stars 35 forks source link

Tutorial: throwing a Response #113

Closed zspitz closed 3 months ago

zspitz commented 3 months ago

In the Not found data section, when the parameter doesn't match a contact, you suggest throwing a Response object:

throw new Response("", {
  status: 404,
  statusText: "Not Found",
});

Is there any specific reason not to throw a simple Error object:

throw new Error('No contact found')

which is generally preferable?


The same throwing of a Response can also be seen in the useLoader documentation.

mcansh commented 3 months ago

throwing a response will render your nearest errorElement and allow you to check the status code via isRouteErrorResponse and render a 404 (or other status code specific) page instead of rendering a more generic error screen