Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restify next(err) don't serialize extraneous options created by makeConstructor #105

Open
deguich opened this issue Oct 9, 2020 · 1 comment

Comments

@deguich
Copy link

deguich commented Oct 9, 2020

When sending error with next(error) callback, response sent to client does not contain optionnal properties of my new Error Object created with makeConstructor restify-errors module.

Env : Node v12.18.3 with ESM loader (type: "module") in package.json

Example :

import errs from 'restify-errors';
const TestError = errs.makeConstructor("TestError", {
   statusCode: 500, 
   info: {errcode: "ERR_GENERIC"},
   message:"App Error"
});
const tmp = new TestError();
console.log("tmp.info exists : ", JSON.stringify(tmp.info)); 
console.log("But this is sent to client when next(tmp) called : ", JSON.stringify(tmp)); 

Print :

tmp.info exists : {"errcode":"ERR_GENERIC"}
But this is sent to client when next(tmp) called : {"code":"Test","message":"App Error"}

It seems to be caused by JSON.stringify that only check for ownProperties (it don't look for prototype chain).

@deguich
Copy link
Author

deguich commented Oct 9, 2020

I finally used this workaround :

import errs from 'restify-errors';

const toJSONf= function() {
    let tmp = {};
    for(let key in this) {
        if(typeof this[key] !== 'function' && !key.startsWith('jse_') && key!=="body")
            tmp[key] = this[key];
    }
    return tmp;
}

const TestError = errs.makeConstructor("TestError", {
    statusCode: 500, 
    info: {errcode: "ERR_GENERIC"},
    message:"App Error",
    toJSON: toJSONf
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant