The errorHandler
submodule helps you monitor, track, and cover errors in your app or site, and provides default content for common errors. This allows you to replace generic error messages with meaningful, actionable feedback for Wix users.
Note: This module is meant to handle errors originating from HTTP requests to the server, not client-side errors.
When you wrap SDK backend modules or httpClient.fetchWithAuth() calls with the withErrorHandler()
hook, you can define custom content and actions for specific errors that can occur during the request.
The hook maps the returned response to an error message according to either the custom error mapping you provide, or the default error content. You can display the error message to a Wix user with the showError()
or getResolvedError()
method.
You retain the ability to catch the error, handle additional logic in the catch block, and show the errors to Wix users. The errorHandler
module gives you flexibility while ensuring that each request has tailored error handling with clear, actionable feedback.
In addition to handling custom errors, the ErrorHandler provides default content for common error codes:
Status Code | Default Message |
---|---|
UNAUTHENTICATED | You're logged out. Log back in to continue. |
INVALID_ARGUMENT | Some fields have invalid or missing information. |
PERMISSION_DENIED | You don't have permission to do this. Request access from the site owner. |
NOT_FOUND | It looks like this doesn't exist anymore. |
ALREADY_EXISTS | Can't save changes because this page was updated somewhere else. Take note of your changes, then refresh and try editing again. |
UNAVAILABLE | There was a technical issue on our end. Try again or refresh. |
RESOURCE_EXHAUSTED | Too many actions were done too quickly. Wait a minute and try again. |
BAD_GATEWAY | There was a technical issue on our end. Try again or refresh. |
GATEWAY_TIMEOUT | There was a technical issue on our end. Try again or refresh. |
INTERNAL | There was a technical issue on our end. Try again or refresh. |
Network Error | There was an issue connecting to your network. Check your connection and try again. |
The relevant content for each error is automatically attached when the error occurs, but you can override the content in the error codes map and options if needed.
Once you catch an error, you have two options for handling it:
Use the default showError()
function: This function displays the error to the Wix user in a default UI using the appropriate content and actions.
Customize your own UI: Use getResolvedError()
to retrieve the relevant error content and display it to the Wix user in a custom UI.
withErrorHandler()
Wraps requests with standardized error handling. It provides a consistent way to manage different types of errors using a configurable error mapping:
Name | Type | Description |
---|---|---|
fn | () => Promise<T> | The function executing the HTTP client request. |
errorCodesMap | ErrorCodesMap | Maps application and validation errors to custom error content. |
options | withErrorHandlerOptions | Optional. Additional options for error handling. |
A promise that resolves with the response or rejects with a customized error.
getResolvedError()
Resolves errors into a standardized format that you can use in your custom UI for consistent error handling and messaging.
If the specific error code was not mapped in withErrorHandler
, this function will return a pre-defined message based on the HTTP status code, as described in the Built-in Error Content section.
Name | Type | Description |
---|---|---|
error | unknown | The error to resolve. |
A ResolvedError
object. The Partial<T>
in the return type allows access to any additional properties that were passed to ShowErrorProps
in withErrorHandler
.
showError()
Displays a standardized error message to the Wix user. It merges error details with optional custom properties and returns the error to the user with the Wix platform's default display mechanism and UI.
Note: The showError()
UI may look different depending on which environment the error is displayed in. For example, the error UI in the site dashboard may differ from the error UI in the editor.
If the specific error code was not mapped in withErrorHandler
, this function displays a pre-defined message based on the HTTP status code, as described in the Built-in Error Content section.
Name | Type | Description |
---|---|---|
error | unknown | The error object to be displayed. |
props | Partial< ShowErrorProps > | Optional. Additional properties to customize the error display. |
Maps application and validation errors to error handling functions.
Returns error display properties.
Maps HTTP status codes to error handling functions.
String literal union type representing HTTP status codes.
Name | Type | Description |
---|---|---|
message | string | The error message. |
action | Action | The action to take when user acknowledges the error. |
key | string | The error key. |
Name | Type | Description |
---|---|---|
statusCodesOverrides | StatusCodeMap | Maps HTTP status codes to error handling functions. |
serverErrorOverride | ShowErrorMapFunction | A function that returns error display properties. It overrides all server errors to display a single message. |
Name | Type | Description |
---|---|---|
message | string | The error message. |
action | Action | The action to take when user acknowledges the error. |
Name | Type | Description |
---|---|---|
text | string | A short of the action that occurs when the user acknowledges the error. For example, "Go to homepage". |
onClick | void | A function that defines the action to carry out when the user clicks to acknowledge the error. For example, navigates the user to the homepage. |
When you pass a typed SDK function to withErrorHandler
, TypeScript automatically provides auto-completion for the error codes that function can return:
For more advanced scenarios or when defining error maps separately, you can use the generic ErrorCodesMap
type with a function type argument to get explicit auto-completion:
This example shows how to use getResolvedError
with additional custom properties that extend ShowErrorProps
:
This example demonstrates using withErrorHandler
with httpClient.fetchWithAuth
for making authenticated HTTP requests. Unlike SDK functions, fetchWithAuth
does not throw on unsuccessful HTTP responses but still throws on network errors. This is why you need to check response.ok
and use the response object as an error when the request fails:
Key differences from SDK usage:
fetchWithAuth
returns a Response object even for HTTP error status codes (4xx, 5xx).response.ok
to determine if the request was successful.showError()
when response.ok
is false.For more information about making authenticated requests, see the httpClient
documentation.