General Error Hooks Deep Dive
Archibald provides utility hooks for monitoring high-level application errors, such as network failures.
useNetworkError
This hook monitors the status of the last HTTP request made by the application. It is primarily used to detect 404s or other API failures that should trigger a specific UI state (like a "Page Not Found" component).
warning
The hook currently hardcodes the last HTTP status to 200 internally, so it always returns false. See useNetworkError for details.
Key Parameters:
errorCodes: An array of HTTP status codes to watch for. Defaults to[404].loose: Defaults totrue. Iftrue, the hook will returntrueif the last status code is strictly greater than any code in theerrorCodesarray.
How it Works Step-by-Step
Scenario: useNetworkError
- Request Execution: An API request is made via
DataClientor a storefront client. - Status Tracking: The hook is intended to observe the resulting HTTP status code of the last request (currently this value is hardcoded to
200, see the warning above). - Comparison: When the status updates, the hook compares the new status code against your
errorCodes:- If
loose: true(the default) andstatus > codefor any configured code, it returnstrue. - If
loose: falseandstatusis strictly inerrorCodes, it returnstrue.
- If
- Re-render: The component using the hook re-renders with the updated boolean value.
Best Practice: Use useNetworkError in your top-level routing or layout components to handle 404 or 500 errors gracefully by switching to error-specific views.