title: How do I work with custom error types in React error boundaries?
body: <p>I have an error boundary set at the top of my component tree, like this:</p>
  
  <pre><code>renderApp() {
    return (
      &lt;ErrorBoundary&gt;
        { this.props.children }
      &lt;/ErrorBoundary&gt;
    );
  }
  </code></pre>
  
  <p>Should an error find its way up the tree, the error boundary renders a simple error page, passing the error itself as a prop:</p>
  
  <pre><code>componentDidCatch(error, info) {
    this.setState({
      hasError: true,
      error
    });
  }
  
  render() {
    if (this.state.hasError) {
      return (
        &lt;ErrorPage error={this.state.error}/&gt;
      );
    }
    return this.props.children;
  }
  </code></pre>
  
  <p>I've started to define custom errors to throw around the application when certain exceptions are raised, simply extending the Error class:</p>
  
  <pre><code>class CustomError extends Error {}
  </code></pre>
  
  <p>Then in my components, I'm throwing these custom errors, which I'd like to detect in my error page and construct messaging based on their type. E.g., if I'm throwing this error further down in the component tree:</p>
  
  <pre><code>throw new CustomError('This is the custom error message');
  </code></pre>
  
  <p>I'd like to be able to sniff the type in my error page, and display messaging that's pertinent for that type.</p>
  
  <pre><code>import CustomError from '../../errors/custom-error';
  
  // This code has obviously been edited for brevity's sake
  
  class ErrorPage extends React.Component {
    render() {
      if (this.props.error instanceof CustomError) {
        errorMessage = "A custom error occurred.";
      }
      return &lt;div&gt;{errorMessage}&lt;/div&gt;;
    }
  }
  </code></pre>
  
  <p>However, in the error page, React only recognizes that this.props.error is an instanceof <code>Error</code>, not of <code>CustomError</code>. If I throw, say, a <code>TypeError</code> instead of a <code>CustomError</code>, then the error page can detect the error type. But passing the custom error type results in the error page knowing nothing about the type other than that it's an error, which obviously defeats the purpose of defining custom error types. What am I missing here? Is this a bug in React? Any input would be most appreciated.</p>
  
  <p>I discovered this issue using React 16.0.0, have updated to 16.2.0 and the problem persists.</p>