loading...

Central Error Handling in Express

Chinedu Orie on July 15, 2019

Express is a fast, unopinionated, minimalist web framework for Node.js - docs Error handling is a routine that one can not do without while buil... [Read Full]
markdown guide
 

The best error handling mechanism I've seen so far. I have a question though.

I understand that i have to require this in the index file:

const { handleError } = require("path-to-code")

Will I have to include this code

const { ErrorHandler } = require("path-to-code");

in every file where I need to handle errors apart from the index file?

 

It's like any other module, you will have to require/import it anywhere you need to use it. No specific exceptions, it depends on the use case.

 
 

Hello,
I really like this post as I'm learning a better way of handling errors.

I have a question about when you add in the .catch(err) { next(err) }
Is this actually throwing out an error from server or I should still use like you did before
throw new ErrorHandler(500, 'Internal server error');

Or that next(err)is doing it?

I just not get that and I would like to understand it.

I have an example of how I used:

async getAll(req, res, next) {
        try {
            const profiles = await db.Profile.find({});
            if (!profiles)
                throw new ErrorHandler(404, "No profiles are found!");

            res.send({
                profiles
            });

            next();
        } catch (err) {
            next(err);
        }
    },

Thanks for help :)
 

Great Article, Thanks

btw, I got a small question in this own Error class :

class ErrorHandler extends Error {
  constructor(statusCode, message) {
    super();
    this.statusCode = statusCode;
    this.message = message;
  }
}
module.exports = {
  ErrorHandler
}

Why we have to write like this (1)

super();
this.message = message;

but not like this (2)

super(message);

?

Actually, I wrote like (2) and then my own class lost a message property

It only comes back when I change it to (1).

What is the difference between them ?

Thanks,

 

The super() method is used when a class (child) inherits from another class (parent).
The super() method provides a means of syncing the child's constructor to the parent's constructor.
Let me illustrate with an example

class Foor {
    constructor(name) {
        this.name = name
   }
    printName = () => {
      console.log(this.name);
   }
}

class Bar extends Foo {

     constructor(name) {
         super(name)
   }
}

const bar = new Bar('Test');

bar.printName() // Test



Now, looking at the code above,
Foorequires anameto be passed to its constructor in order to function, whenBarinherited fromFoo, there's no way thenamecould be passed down toFooif not with thesuper()`

So, in relation to the snippet that you shared above, you are passing the message to the parent which in this case is Error, that way the child ErrorHandler has no access to message

I hope this helps.

 

This is cool,

Just one opinion, i feel it should be expanded to be not just Error Handler but statusHandler

```
class statusHandler extends Error {
constructor(statusCode, message, data) {
super();
this.statusCode = statusCode;
this.message = message;
this.data = data || null

}
}
module.exports = {
statusHandler
}




Likewise for handleError but generally this approach is effectively DRY. Nice one
 

Just like Express is unopinionated, how you apply the concept in the article is also unopinionated. And except I do not understand you clearly, it'd not make sense semantically to throw a success response as an error. Thanks for sharing your views.

 

Thank you for the article, I learned something.

I have question, how does Express know that next(error), should be passed to the Error Handling middleware (which has 4 arguments)?

If there is a middleware that has (res, req, next) that was sat up before the Error Handling middleware, will it get first to handle the error?

Nasser

 

Thanks for the article Orie, it really help me a lot.

 
 

This is great, thank you! Exactly what I needed.

 

Glad to learn you found it helpful. ✌️

 

ReferenceError: ErrorHandler is not defined

const { handleError, ErrorHandler } = require('./helpers/error')
Imported ErrorHandler but not used into Index file.

 

Why for a success case the last step is next(), but not a return res.status(200)?

 

It's because, the function is a middleware, not a final destination, with next() returned, it would proceed to the next code to be executed.

 

Nice Article

Can we make common structure for Error & Success both ?

code of conduct - report abuse