Exception handling is very important in front-end, including client-side and server-side exceptions. Previously, exception handling was to add err handling for each asynchronous function, which not only increased the workload, but also easily missed some exceptions. Fortunately, Angular6 provides ErrorHandler to handle exceptions (Ionic4 for IonicErrorHandler), the default ErrorHandler handling exceptions is to output it on the console, which obviously can not meet the demand, so you need to implement a GlobalErrorHandler.
Why use ErrorHandler to unify exception handling
- Improve efficiency (for example, before for each asynchronous function need to pass err parameters to handle, now use ErrorHandler unified processing) The previous code for asynchronous exception handling is the following, just see so many err will feel dizzy, not to mention the logic code above, of course, for multiple asynchronous requests this is not the best way to handle, here only discuss exceptions.
Exceptions that are not easily reproducible can be caught, especially problems that are not easily reproducible on the client side. If a client has a problem that cannot be reproduced locally and happens to forget to catch the exception, it would be very bad. Unified processing can input the exception information into the log and can quickly locate the problem.
It is also very bad to handle uncommon exceptions or runtime exceptions, and to prompt the user in a timely manner without crashing the program or exposing the exception directly to the user by not catching it.
For the user, a uniform, friendly exception message prompt can be displayed, creating a unique system style. For some exceptions, it is even possible to explain to the user why the exception is thrown and guide the user behavior to eliminate the exception. For example, if the user throws an insufficient balance exception when making a payment, the user should be prompted that the balance is insufficient, in addition to displaying a recharge portal (if the current system supports recharge) or prompting the user where to recharge.
Concrete implementation of catching exceptions with ErrorHandler
Step 1: Create
LoggingService to get exception information and record exception logs
ErrorService: Get the exception information and stack of client side, exception information and status code of server side.
LoggingService: Send exception information to the backend through Beacon API and record exception logs, including current logged-in user, exception occurrence time, exception information, etc.
Step 2: Create GlobalErrorHandler
- Create global-error-handler.ts
- Add to app.module.ts
- Add exception handling logic
- The current framework used is Ant Design, so the user prompts use NzMessageService.
- When an exception occurs in the program, the handleError hook is automatically called, which can determine whether the exception is from the client or from the server.
- In practical projects, it may be necessary to provide Services that display exception messages to satisfy the display of different exception messages.
- For exception logging, it can be filtered according to the content of exceptions, and it is not necessary to log every exception.
Note: Many people previously handled exceptions in the Interceptor, Interceptor can only handle HttpErrorResponse type of error, if handled here, then ErrorHandler will not capture. So in the Interceptor, encounter HttpErrorResponse error needs to be thrown.