Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ErrorBoundary gets error with "Unexpected error" message instead of actual returned error from loader [only prod build, navigation with useNavigate] #5115

Closed
skvortsovstepan opened this issue Jan 17, 2023 · 2 comments

Comments

@skvortsovstepan
Copy link

skvortsovstepan commented Jan 17, 2023

What version of Remix are you using?

1.10.1

Steps to Reproduce

Hi there!
Thanks for the awesome framework!

The issue is related to error passed into the ErrorBoundary in case of useNavigate usage only on prod build:

  • assume there are: loader that throws error and related ErrorBoundary in the component
  • user navigates to that component via useNavigate
  • build is prod build

Env:
MacOS Monterey 12.5.1
node 18.12.1
npm 8.19.2

Here are steps to reproduce:

  1. npx create-remix@latest - install basic version of remix with express server (and typescript)
  2. add into app/routes following files (index.tsx, test.tsx):
// index.tsx

import { useNavigate } from "@remix-run/react";

export default function Index() {
  const navigate = useNavigate();

  return (
    <div style={{ fontFamily: "system-ui, sans-serif", lineHeight: "1.4" }}>
      <button onClick={() => {
        navigate('/test');
      }}>Go to test</button>
    </div>
  );
}

and

// test.tsx
export const loader = async () => {
    // api-request with error-response - e.g. "no access" 
    throw new Error('Error from api!')
}

export const ErrorBoundary = ({error}: {error: Error}) => {
    return <div style={{background: 'red', color: 'white', padding: 5, maxWidth: 200}}>{error.message}</div>
}

export default function() {
    return 'test route'
}
  1. create prod build with npm run build
  2. npm run start
  3. go to localhost:3000 and click Go to test

Expected Behavior

The error message in ErrorBoundary is the message that was thrown from loader: "Error from api!"

If run dev build with npm run dev - it is the case, it is working correct
correct-dev

Actual Behavior

The error message shown is "Unexpected Server Error" in prod build
incorrect-prod

@skvortsovstepan skvortsovstepan changed the title ErrorBoundary gets error with "Unexpected error" message instead of actual returned error from loader [only prod build, navigation with useNavigate]] ErrorBoundary gets error with "Unexpected error" message instead of actual returned error from loader [only prod build, navigation with useNavigate] Jan 17, 2023
@brophdawg11
Copy link
Contributor

This is intended behavior. Error Boundaries are for unexpected error scenarios, stuff like "cannot read property 'x' of undefined," and therefore in production builds they are all converted to generic Unexpected Server Error messages. If you want to short circuit from a loader for a known reason, like Unauthorized, then you should throw a Response to your CatchBoundary. In this case, it sounds like you want to throw a response with a 401 status.

@skvortsovstepan
Copy link
Author

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants