Node.js Error Handling Patterns Demystified (with examples)
Tags: node, errors, interviewquestions, fullstack
Published: 2018-07-19 01:35:19

---
title: Node.js Error Handling Patterns Demystified (with examples)
published: true
description: Error handling in an asynchronous language works in a unique way and presents many challenges, some unexpected. There are four main error handling patterns in Node.js.
tags: nodejs, error handling, interview questions, fullstack
canonical_url: https://www.fullstack.cafe/blog/nodejs-error-handling-patterns-demystified-examples
---
![Node.js Error Handling Demystified](https://images.pexels.com/photos/620335/pexels-photo-620335.jpeg?auto=compress&cs=tinysrgb&h=350)

Error handling in an asynchronous language works in a unique way and presents many challenges, some unexpected. There are seven main error handling patterns in Node.js. Let's briefly check them all.

> Originally published on [FullStack.Cafe - Never Fail Your Tech Interview Again](https://www.fullstack.cafe)

## **Error return value**
Simplest pattern that doesn't work asynchronously. Consider:

```js
var validateObject = function (obj) {
    if (typeof obj !== 'object') {
        return new Error('Invalid object');
    }
};
```
## **Error throwing** 
Well-establish pattern, in which a function does its thing and if an error situation arises, it simply bails out throwing an error. Can leave you in an unstable state. It requires extra work to catch them. Also wrapping the async calls in try/catch won't help because the errors happen asynchronously. To fix this, we need *domains*. Domains provide an asynchronous try...catch.

```js
var validateObject = function (obj) {
    if (typeof obj !== 'object') {
        throw new Error('Invalid object');
    }
};

try {
    validateObject('123');
}
catch (err) {
    console.log('Thrown: ' + err.message);
}
```
## **Error callback**
Returning an error via a callback is the most common error handling pattern in Node.js. Handling error callbacks can become a mess (callback hell or the pyramid of doom).

```js
var validateObject = function (obj, callback) {
    if (typeof obj !== 'object') {
        return callback(new Error('Invalid object'));
    }
    return callback();
};

validateObject('123', function (err) {
    console.log('Callback: ' + err.message);
});
```
## **Error emitting**
When emitting errors, the errors are broadcast to any interested subscribers and handled within the same process tick, in the order subscribed.

```js
var Events = require('events');
var emitter = new Events.EventEmitter();

var validateObject = function (a) {
    if (typeof a !== 'object') {
        emitter.emit('error', new Error('Invalid object'));
    }
};

emitter.on('error', function (err) {
    console.log('Emitted: ' + err.message);
});

validateObject('123');
```
## **Promises** 
Promises used for async error handling. Consider:

```js
doWork()
.then(doWork)
.then(doError)
.then(doWork)
.catch(errorHandler)
.then(verify);
```
## **Try...catch with async/await** 
ES7 Async/await allows us as developers to write asynchronous JS code that look synchronous.

```js
async function f() {

  try {
    let response = await fetch('http://no-such-url');
  } catch(err) {
    alert(err); // TypeError: failed to fetch
  }
}

f();
```

## **Await-to-js lib**
The variation on async/await without try-catch blocks in Javascript. Consider:

```js
import to from 'await-to-js';

async function main(callback) {
    const [err,quote] = await to(getQuote());
    if(err || !quote) return callback(new Error('No Quote found');
    callback(null,quote);
}
```

🔗**Source:** [gist.github.com](https://gist.github.com/hueniverse/5167027)

> *Thanks 🙌 for reading and good luck on your interview!* <br/>*Check more FullStack Interview Questions & Answers on 👉 [www.fullstack.cafe](https://www.fullstack.cafe)*