javascript - How to create Node.js custom errors and have the stack property return after res.json() -


i’m attempting create custom error objects in node use error prototype , return these errors client via express res.json() method. can create instance of custom error, set it’s properties when call res.json(), stack property missing. believe due json.stringify() not following prototypes?

i can’t see way of creating custom error objects arbitrary number of properties , use json.stringify() on these objects return properties , base class / superclass error stack property. please help! example below:

(function (customerrorcontroller) {       function customerror() {          this.name = 'customerror'; // string         this.message = null; // string         this.errorcode = null; // string         // this.stack exists customerror inherits error     }      customerror.prototype = new error();       customerrorcontroller.init = function (app) {          app.get('/test', function (req, res) {              var customerror = new customerror();             customerror.message = 'the message';             customerror.errorcode = 'err001';              console.log(customerror.stack); // logs stack fine              res.json(customerror); // stack missing when outputting object json          });     };  })(module.exports); 

thanks comments. these , bit more digging, i’m using following code create custom errors , have correct stack on stack property return after calling res.json() error:

// redefine properties on error enumerable object.defineproperty(error.prototype, 'message', { configurable: true, enumerable: true }); object.defineproperty(error.prototype, 'stack', { configurable: true, enumerable: true });   (function (customerrorcontroller) {      function customerror(message, errorcode) {          error.capturestacktrace(this, customerror);          this.name = 'customerror'; // string         this.message = message; // string         this.errorcode = errorcode; // string     }      customerror.prototype = object.create(error.prototype);      customerrorcontroller.init = function (app) {          app.get('/test', function (req, res) {              var customerror = new customerror('the message', 'err001');             res.json(customerror); // stack property included          });     };  })(module.exports); 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

python - Django-cities exits with "killed" -

python - How to get a widget position inside it's layout in Kivy? -