handle-errors-and-exceptions-in-javascript.s
Owner: Maxime Sraïki
Why
On scaling projects you can easily be overwhelmed by a pretty large code source. When this occurs, if you don't have a proper way to handle errors/exceptions, you could easily start raising a lot of bug or regression and spend a lot of time trying to fix it.
Definitions
First let's make the distinction between Erros and Exceptions:
Errors are javascript objects that are thrown when something goes wrong in the code during runtime. It will usually have:
a
message
key containing a human readable error messageinformations about where the error was raised like
fileName
,lineNumber
, or even the wholestack
Exceptions are custom Errors objects created by developers to implement business logic.
Checks
Every Exceptions should be caught in a .catch statement and treated.
Every caught Error should be rethrown (Even if it makes your application crash, you have to see it QUICKLY)
Every .catch statement should take the error as argument
[OPTIONAL] If you have a logging system, in every .catch statement, you should log a message with the context and the whole error object
Examples
Bad Examples:
Example 1: No catch statement
Here if myFunction throws an error, it will remain unhandled and can make my server crash without prior notice !
Example 2: Error no rethrown
Here if myFunction throws an error, it will be caught in the .catch statement and logged to the console logger. The problem are:
The statement
is equivalent to
which is
therefore it will go in your next .then and act as if everything was normal with an undefined response...
You make the error disappear and it will not be caught in your next .catch
Example 3: Error not even considered
If you don't even use the error that has been caught, you are sure you don't react correctly to it.
Good Examples:
Example 1: Error
Example 2: Exception
Last updated