Skip to content
This repository has been archived by the owner on Nov 3, 2020. It is now read-only.

Handle exceptions properly

sreen020 edited this page Jul 1, 2020 · 10 revisions

Handle exceptions properly
Everybody hates errors… So how do you handle these exceptions properly? And what is the difference between exceptions and errors? Errors are are always in an Error classes. These errors can be build-in this way and will be passed or thrown to another function. When the error is thrown it will become an exception.

Here you see an example of using an error as an exception:
throw new Error('something bad happened');

But you can also have an error without throwing it:
callback(new Error('something bad happened'));

In other languages an exceptional error is very common. In NodeJS it’s not. Because in Node, most errors are asynchronous, because of this it is not needed to catch an error.

There are 2 types of errors: operational errors and programmer errors.

Operational errors
These errors are not caused by a program mistake or bug. These errors are caused by the system or connection. Such as: “failed to connect, invalid user input, request timeout, 500 response and system out of memory”.

Programmer errors
These errors actually are caused by bugs in the program. Programmer errors can always be fixed by refactoring the code. Errors like these can never be handled properly. Examples of programmer errors are: “undefined, called an asynchronous function without a callback and received a sting when object was expected”

There are 4 ways to deliver an error in nodeJS:

  • throw
  • Pass the error to a callback
  • Pass the error to a rejectPromise function

Throw
When using throw you can deliver an error synchronously. This means the error will be handled in the same context as where the function is called.

Callback
Callbacks is a more basic way to handle errors asynchronously. The user passes a function, and you invoke it later when the function completes. callback(err, result)

Promise
Since the support of async/await this is the most populair way of handling errors in NodeJS. This way allows asynchronous code to be written to look like synchronous code and to catch errors using try/catch.

The promise way of handling exceptions is also the way I have chosen to do it. This is because I found a lot of documentation about it and my research shows that it is the most populair way of handling errors in NodeJS.

So here is the example of my error screen where you can find the information about the error (error code and error message). When you get here, you know what’s going on and you can find a big call to action button saying “go back” to redirect to the home screen.

When im looking for http://localhost:3001/asdasd this is the page I get.

In my EJS file this is how I can show the error values:
<%= errorCode %> <%= message %>

This is the JS code:
router.get('/error', (req, res) => {
if (!req.session.user) {
return res.redirect('/');
}
return res.render('error');
});

router.use((req, res, next) => {
const error = new Error('Not found');
error.status = 404;
return res.render('error', {message: error.message, errorCode: error.status});
});

router.use((error, req, res, next) => {
res.status(error.status || 500);
res.json({
error: {
message: error.message,
},
});
});