TritonDataCenter / node-verror

Rich JavaScript errors
MIT License
1.18k stars 61 forks source link

info, cause and name properties of the WError instance disappear #44

Closed kasongoyo closed 7 years ago

kasongoyo commented 7 years ago

May be I am missing something about this library well I have created an instance of WError in my mongoose schema and I propagate that error to other places that use the schema then to my surprise I can't find the properties like info and cause I previously set when an error occured and instead I get normal nodejs error properties(stack and message). Where do I go wrong? I thought ths library could help to wrap my errors so that I can add custom properties etc and use it anywhere in my application.

My mongoose schema plugin;

     //dependencies
     let WError = require('verror').WError;

     module.exports = schema => {
         let handleMongoError = (error, next) => {
             switch (error.code) {
                 case 11000:
                     next(new Error('Duplicate Key Error'));
                     break;
                 default:
                     next(error);
             }
         };

         let handleValidationError = (error, next) => {
             let errorPaths = Object.keys(error.errors).map(key => {
                 return {
                     name: error.errors[key].path,
                     message: error.errors[key].message
                 }
             });
             let customError = new WError({
                 name: error.name,
                 cause: error,
                 info: {
                     paths: errorPaths
                 }
             });
             // customError.name = error.name;
             // customError.message = error.message;
             // customError.info = errorPaths;
             console.log(customError);
             next(customError);
         };

         let handleError = (error, res, next) => {
             switch (error.name) {
                 case 'MongoError':
                     handleMongoError(error, next);
                     break;
                 case 'ValidationError':
                     handleValidationError(error, next);
                     break;
                 default:
                     next(error);
             }
         };

         schema.post('save', handleError);
         schema.post('update', handleError);
         schema.post('findOneAndUpdate', handleError);
         schema.post('insertMany', handleError);
     };

My test case that utilize error;

    //dependencies
    let path = require('path');
    let faker = require('faker');
    let expect = require('chai').expect;
    let venueCtrl = require(path.join(__dirname, '..','..','app','venue','venue_controller'));

    describe.only('Venue Controller', function() {
        describe('', function() {
            it('should create venue', function(done) {
                let venue = {
                    // name: faker.company.companyName(),
                    // description: faker.lorem.sentence(),
                    email: faker.internet.email(),
                    mobile: faker.phone.phoneNumber()
                };
                venueCtrl.create(venue).then(newVenue => {
                    // expect(newVenue._id).to.exist;
                    // expect(venue.name).to.equal(newVenue.name);
                    done();
                }).catch(error => {
                    console.log(error.info);//undefined
                    console.log(error.cause);//undefined
                });
            });
        });
    });
davepacheco commented 7 years ago

My expectation would be that you would not see the "info" and "error" properties on the Error itself when you use console.log, but that you can get them with the VError.cause(error) and VError.info(error) functions.

kasongoyo commented 7 years ago

@davepacheco Thanks for your promptness well your guess is correct and I have succeeded. I've also discovered the existence of jse_info property in VError instance.

davepacheco commented 7 years ago

I'm glad that worked.

The jse_info property is private and may change or disappear in future versions (even micro or minor versions).