Response Error Handling

Author: kilanibarhoomCreated Aug 13, 2025Updated Aug 13, 2025

When the server responds with a status code referring to an error for example: 404, 405...

How should this be handled? Cause when using the Error boundary, I can't customize the fallback based on the error response from the server.

For example, in this code the server response with 404 error code.

javascript
const opened_cash = await OpeningBalance.findOne({
      where: {
        user_id,
        branch_id,
        is_previous_closed: false,
      },
      order: [['date', 'DESC']]
    });

    if(!opened_cash) {
      return res.status(404).json({ message: 'Opened cash not found' });
    }

In react i should be able to catch and handle this error based on the response code/message. Instead, I can't, I just show the global error or a default fallback from ErrorBoundary component,

javascript
<ErrorBoundary
        onError={(error) => {
          console.log({ error: error.message });
        }}
        fallback={<div>Error loading dashboard</div>}
      >
// main component

</ErrorBoundary>

Instead I want to do something in the main component, like based on the error show something.

Source: alan2207/bulletproof-react