Skip to content

[Docs]: Better documentation in middleware error handling #14054

@gjc14

Description

@gjc14

Describe what's incorrect/missing in the documentation

In middleware error handling section, it demonstrates that we could re-throw error to let react router handle it.

However, in a basic example like Error: You made a GET request to "/" but did not provide a loader for route..., the error after re-throw becomes [object, object], because it becomes an route error response object, and we are throwing it without handling the error.

export const unstable_middleware: Route.unstable_MiddlewareFunction[] = [
  async ({ request, context, params }, next) => {
    const { searchParams } = new URL(request.url);
    const callNext = searchParams.has("next");

    try {
      callNext && (await next());
    } catch (error) {
      if (isRouteErrorResponse(error)) {
        // The error is extracted in middleware. How to re-throw as if no middleware was applied?
        console.log("Middleware route error response", error);
      }
      throw error;
    }
  },
];
  1. If this is expected behavior of the middleware, maybe add a isRouteErrorResponse(error) calling to make it clearer.
  2. Or we need a way to reconstruct a route error response.

Reproduction: gjc14/middleware-error-handling

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions