Error handling is needed for unavoidable errors or unexpected bugs in projects. When I first started out, I was not a big fan of error handling. During the testing phase, there were no problems, but now I see that the code is very buggy, and some unexpected errors usually cause the page to crash. Users do not know why, after the button clicked, the page directly blank screen and so on, may directly lead to the loss of users, if you give some friendly hints, may retain some users.

Here do not talk about how to concrete practice, talk about some personal messy thoughts.

Projects typically have scenarios like this:
  • Accidental problems in the test environment are difficult to reproduce, and the cause of recurrence cannot be found.
  • A bug solved during the test was replicated by other users in the online environment.
  • Some can not avoid the error or abnormal display of information, some articles or lists have no data, to Ali cloud video watermark and other asynchronous operations, users upload operations, immediately watch the video can not display watermark…

Errors are inevitable and should be taken into account as often as possible and user friendly.

  • When the list has no content, there is a content style that has no data for the time being, or an action that directs the user to some other page.
  • Use window.error and try-catch methods appropriately, and listen for code errors when they occur, so as to minimize the impact of code errors on other functions of the page.
  • When an HTTP request error occurs, an error message is displayed along with the data returned by the server,errMessage, or HTTP status code 404,502.
  • The encapsulation approach uses then and catch that use promises, and also distinguishes between success and error scenarios for trivial features.
Thoughts when writing features

Functions should be distinguished as small as possible. Attention should be paid to distinguish between success and error (it may take time, but the robustness of the code has been improved). In the case of insufficient development time, success and error methods can be temporarily set aside without processing or drawing too much time.

That’s all I can think of for now.