3

Here I created Custom Error class in Node.js. I created this ErrorClass to send Custom error response of api call.

I want to catch this CustomError class in Bluebird Catch promises.

Object.defineProperty(Error.prototype, 'message', {
    configurable: true,
    enumerable: true
});

Object.defineProperty(Error.prototype, 'stack', {
    configurable: true,
    enumerable: true
});

Object.defineProperty(Error.prototype, 'toJSON', {
    value: function () {
        var alt = {};
        Object.getOwnPropertyNames(this).forEach(function (key) {
            alt[key] = this[key];
        }, this);

        return alt;
    },
    configurable: true
});

Object.defineProperty(Error.prototype, 'errCode', {
    configurable: true,
    enumerable: true
});

function CustomError(errcode, err, message) {
    Error.captureStackTrace(this, this.constructor);
    this.name = 'CustomError';
    this.message = message;
    this.errcode = errcode;
    this.err = err;
}

CustomError.prototype = Object.create(Error.prototype);

I wanna convert this into node-module but I am not getting how to do that.

6
  • 1
    Why are you adding message, stack and toJSON to Error.prototype? Why not just set them in CustomError? Commented Apr 21, 2015 at 15:20
  • @thefourtheye why define message/stack at all, they are already being inherited from Error.prototype. Commented Apr 21, 2015 at 15:24
  • @James True. Why didn't I think of that? :D Commented Apr 21, 2015 at 15:24
  • @James Ah, I see why OP does that. He is making everything enumerable. Commented Apr 21, 2015 at 15:30
  • @thefourtheye ah, so they are. To be honest I only scanned the actual implementation as that wasn't really relevant to the question being asked. Commented Apr 21, 2015 at 15:41

2 Answers 2

2

I want to catch this CustomError class in Bluebird Catch promises.

Quoting bluebird's documentation,

For a parameter to be considered a type of error that you want to filter, you need the constructor to have its .prototype property be instanceof Error.

Such a constructor can be minimally created like so:

function MyCustomError() {}
MyCustomError.prototype = Object.create(Error.prototype);

Using it:

Promise.resolve().then(function() {
    throw new MyCustomError();
}).catch(MyCustomError, function(e) {
    //will end up here now
});

So, you can catch the custom error object, like this

Promise.resolve().then(function() {
    throw new CustomError();
}).catch(CustomError, function(e) {
    //will end up here now
});

I wanna convert this into node-module but I am not getting how to do that.

You just have to assign whatever you want to export as part of the module, to module.exports. In this case, most likely, you might want to export the CustomError function and it can be done like this

module.exports = CustomError;

Read more about module.exports in this question, What is the purpose of Node.js module.exports and how do you use it?

Sign up to request clarification or add additional context in comments.

Comments

1

A node module is nothing more than an exported class. In your example, if you export your CustomError class i.e.

module.exports = CustomError;

Then you would be able to import the module from another class

var CustomError = require("./CustomError");
...
throw new CustomError();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.