What are the best practices for JavaScript error handling?

前端 未结 5 567
孤城傲影
孤城傲影 2021-01-29 17:25

I\'m looking to start making my JavaScript a bit more error proof, and I\'m finding plenty of documentation on using try, catch, finally,

5条回答
  •  野的像风
    2021-01-29 18:07

    An immensely interesting set of slides on Enterprise JavaScript Error Handling can be found at https://web.archive.org/web/20140126104824/http://www.devhands.com/2008/10/javascript-error-handling-and-general-best-practices/

    In short it summarizes:

    1. Assume your code will fail
    2. Log errors to the server
    3. You, not the browser, handle errors
    4. Identify where errors might occur
    5. Throw your own errors
    6. Distinguish fatal versus non-fatal errors
    7. Provide a debug mode

    The slides go into much more detail and most probably will give you some direction.

    EDIT:

    The presentation mentioned above can be found here: https://www.slideshare.net/nzakas/enterprise-javascript-error-handling-presentation

提交回复
热议问题