question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

An error in `MetaFunction` or `LinkFunction` is not caught by `ErrorBoundary`

See original GitHub issue

What version of Remix are you using?

1.4.1

Steps to Reproduce

Create a MetaFunction with an error inside:

export let meta: MetaFunction = () => {
  throw new Error("boom")
}

Or create a LinkFunction:

export let links: LinkFunction = () => {
  throw new Error("boom")
}

This can happen if, for example, you access a property of data that doesn’t exists.

Expected Behavior

It should show the ErrorBoundary from the route or parent route.

Actual Behavior

It shows a Unexpected Server Error white screen with black text and the error message below.

Screen Shot 2022-05-09 at 11 30 19

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:7
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
emilbryggarecommented, May 26, 2022

This is a problem for me as well, I just wanted to add that for me the real error happened in the LoaderFunction and the MetaFunction threw an error when trying to access that data.

  1. Error in LoaderFunction
  2. Error in MetaFunction when accessing that data, e.g. trying to access X of undefined.
  3. ErrorBoundary in Route hierarchy is not activated
  4. Resulting in the white screen with Unexpected Server Error.

Do we want errors in the MetaFunction to actually trigger the ErrorBoundary? The application can probably render ok most of the time and fall back to a parent MetaFunction.

This is what I did in my application to handle this for now.

export const meta: MetaFunction = ({ data }) => {
  try {
    return getSocialMetas(data);
  } catch (error) {
    console.error(error);
    return {};
  }
};

This will fall back to the parent MetaFunction if an error in just the MetaFunction, and if the error is actually in a LoaderFunction that ErrorBoundary is activated.

0reactions
jacknevittcommented, Dec 1, 2022

I have just encountered this, and I find it quite odd that data is the thrown error instead of the expected type. I got around this by checking data instanceof Error before using properties on data. Would be nice not to do this for every meta function.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ErrorBoundary not catching error thrown by imported function
In your code, the error is thrown from an event handler (addPlaylistToSpotify) so componentDidCatch can't catch the error.
Read more >
Error Boundaries - React
Error boundaries are React components that catch JavaScript errors ... As of React 16, errors that were not caught by any error boundary...
Read more >
React Error Boundaries: Complete Guide - Meticulous
A complete guide to implementing React Error Boundaries, and how to use a third-party tool for handling more sophisticated scenarios.
Read more >
A Beginners Guide to Remix Framework - Bejamas
The error boundary template will be rendered in place of the actual component or route, and this error will only affect that component...
Read more >
Catching Asynchronous Errors in React using Error Boundaries
If we run this code, the ErrorBoundary will catch the error, ... Abramov on “Throwing Error from hook not caught in error boundary”...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found