Error Handling
When developing applications, we often encounter two types of failure scenarios. One type of failure is directly related to the business process, such as when a user does not exist or when the order status has changed. The other type of failure is caused by unexpected errors, such as network issues or system resource overflows.
For failures related to the business process, we can use the reject
method to actively throw errors and provide clear feedback to the client about the failure. For example:
In this way, we can precisely communicate the reason for the request failure to the client, enhancing the robustness and reliability of the system. The caller will receive a response in the following format:
Callers can easily determine the success of the request using code.success
and identify the specific type of failure using fail.code
. This design helps in clearly conveying the request status, making it easier for the caller to handle and respond to failure scenarios effectively.
Error Codes
In the /src/fail-code.ts
file, we have defined all possible error codes.
Each key corresponds to a specific error code, and its value is a function that returns a string describing the reason for the failure. This design facilitates the management and clear organization of different error codes, providing detailed explanations for each error code.
Parameter Passing
When calling the reject
method, you can adjust the error message sent to the client through parameters. This allows developers to flexibly customize error messages as needed.
Please note that the method only accepts one parameter. If you need to pass multiple parameters, you can achieve this by returning an object, as shown below:
These parameters will also be sent to the client for further processing. The client will receive a response in the following format:
Business Failures
Most failures stem from business logic, such as exceeding a deadline or insufficient inventory. For these localized business failures, we usually only need to inform the user of the reason for the failure. In such cases, we can uniformly use the BUSINESS_FAIL
status code:
Unexpected Errors
For any uncaptured errors, the client will receive a fixed INTERNAL_SERVER_ERROR
error code to prevent sensitive information leakage.
User-Friendly Error Messages
Error messages should explain the reason for failure to users, but not all messages are user-friendly enough. For example, in the case of a TYPE_SAFE_ERROR
failure caused by insufficient password length, we may not want to directly show the user:
Instead, we would prefer the user to see a more friendly prompt:
It is recommended that this user-friendly error message handling be done on the client side. The benefits include:
- Achieving thorough decoupling between the front end and back end.
- Simplifying the implementation of language switching functionality, without the need to handle it on both the client and server sides.
- Ensuring that the client is always responsible for writing user-friendly error messages.
For more information, please refer to Milkio Client.