You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|`handleReturnedServerError?`| When an error occurs on the server after executing the action on the client, it lets you define custom logic to returns a custom `serverError`message instead of the default one. More information [here](/docs/safe-action-client/initialization-options#handlereturnedservererror). |
16
+
|`handleReturnedServerError?`| When an error occurs on the server after executing the action on the client, it lets you define custom logic to returns a custom `serverError` instead of the default string. More information [here](/docs/safe-action-client/initialization-options#handlereturnedservererror). |
17
17
|`handleServerErrorLog?`| When an error occurs on the server after executing the action on the client, it lets you define custom logic to log the error on the server. By default the error is logged via `console.error`. More information [here](/docs/safe-action-client/initialization-options#handleservererrorlog). |
Copy file name to clipboardexpand all lines: website/docs/safe-action-client/initialization-options.md
+6-4
Original file line number
Diff line number
Diff line change
@@ -7,9 +7,9 @@ description: You can initialize a safe action client with these options.
7
7
8
8
## `handleReturnedServerError?`
9
9
10
-
You can provide this optional function to the safe action client. It is used to customize the server error message returned to the client, if one occurs during action's server execution. This includes errors thrown by the action server code, and errors thrown by the middleware.
10
+
You can provide this optional function to the safe action client. It is used to customize the server error returned to the client, if one occurs during action's server execution. This includes errors thrown by the action server code, and errors thrown by the middleware.
11
11
12
-
Here's a simple example, changing the message for every error thrown on the server:
12
+
Here's a simple example, changing the default message for every error thrown on the server:
A more useful one would be to customize the message based on the error type. We can, for instance, create a custom error class and check the error type inside this function:
// Every other error that occurs will be masked with the default message.
42
-
returnDEFAULT_SERVER_ERROR;
42
+
returnDEFAULT_SERVER_ERROR_MESSAGE;
43
43
},
44
44
});
45
45
```
46
46
47
+
Note that the return type of this function will determine the type of the server error that will be returned to the client. By default it is a string with the `DEFAULT_SERVER_ERROR_MESSAGE` for all errors.
48
+
47
49
## `handleServerErrorLog?`
48
50
49
51
You can provide this optional function to the safe action client. This is used to define how errors should be logged when one occurs while the server is executing an action. This includes errors thrown by the action server code, and errors thrown by the middleware. Here you get as argument the **original error object**, not a message customized by `handleReturnedServerError`, if provided.
|`data?`| Execution is successful. | What you returned in action's server code. |
14
14
| `validationErrors?` | Input data doesn't pass validation. | An object whose keys are the names of the fields that failed validation. Each key's value is either an `ErrorList` or a nested key with an `ErrorList` inside.<br />`ErrorList` is defined as: `{ errors?: string[] }`.<br />It follows the same structure as [Zod's `format` function](https://zod.dev/ERROR_HANDLING?id=formatting-errors).
15
-
|`serverError?`| An error occurs during action's server code execution. | A `string`that by default is "Something went wrong while executing the operation" for every server error that occurs, but this is [configurable](/docs/safe-action-client/initialization-options#handlereturnedservererror) when instantiating a new client. |
15
+
|`serverError?`| An error occurs during action's server code execution. | A generic type that by default is the string "Something went wrong while executing the operation." for every server error that occurs, but this is [configurable](/docs/safe-action-client/initialization-options#handlereturnedservererror) when instantiating a new client. |
Copy file name to clipboardexpand all lines: website/docs/usage/middleware.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ Instance level is the right place when you want to share middleware behavior for
20
20
Here we'll use a logging middleware in the base client and then extend it with an authorization middleware in `authActionClient`. We'll also define a safe action called `editProfile`, that will use `authActionClient` as its client. Note that the `handleReturnedServerError` function passed to the base client will also be used for `authActionClient`:
0 commit comments