-
Notifications
You must be signed in to change notification settings - Fork 30
Added error context to the json #87
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
Conversation
2 similar comments
|
Thanks for the PR! I think we default to not returning it for security - frequently However, you should able to override the Error instance's |
|
Hey! That is what we were going to do, but it is a pretty heavy handed approach which we were trying to avoid. What do you think about adding a new option for exposing this type of context in the response? Here are a few ideas we thought could work:
|
|
We just released 6.x today, which migrates Maybe I should take a step back, I kind of assumed you meant to return the If you are using this library with a restify web server, then a response with a header a content-type of Would you mind sharing code snippets of your use case? That might help me to better understand why you feel toJSON feels heavy handed. |
|
Unfortunately, I am not familiar enough with the full context around this issue to know the right way to move forward. I just figured that I would open this PR because it was such an easy addition. Also since it seemed like a feature removal it seemed like it may not have been intentional, or that with the proper implementation you would be open to adding it back. Unless @ghermeto wants to push this conversation forward, we can close this. |
|
Bummer, sorry about the poor first PR experience! 😞 As under-resourced as we are we appreciate all the help we can get! The proactiveness is definitely 💯! Here are some things you can try. If the use case is for a custom error, it's easy to provide a toJSON when you create the constructor itself: restifyErrs.makeConstructor('MyCustomError', {
statusCode: 500,
toJSON: function toJSON() {
const self = this;
return {
message: self.message,
context: self.context
};
}
});
var myErr = new MyCustomError();
JSON.stringify(myErr); // => contains context fieldIf this is for any of the standard errors that restify-errors ships out of the box, and you happen to be using restify, you can implement a custom formatter to do last mile interception/formatting: const server = restify.createServer({
name: 'foo',
formatters: {
// all application/json responses will be funneled through this formatter. in restify,
// formatters are a last mile serializer for preparing your payload based on content-type.
'application/json': function(req, res, body) {
// in formatters, body is the object passed to res.send()
if (body instanceof Error) {
// overwrite the built-in toJSON
body.toJSON = function() {
return {
message: body.message,
context: body.context
};
};
// alternatively, you can also directly return a response:
// return JSON.stringify({
// message: body.message,
// context: body.context
// });
}
// for everything else, stringify as normal
return JSON.stringify(body);
}
}
});If neither of these work, let me know - would love to know if there's a use case we haven't addressed properly. |
|
Hi @DonutEspresso, that is exactly what we are doing: errors.makeConstructor('RuleConflictError', {
statusCode: 409,
message: '... my message ...',
toJSON: function () {
const { body, context } = this;
return JSON.stringify({ ...body, context });
}
});The question we had was if I believe that it is valid to allow your APIs to return some sort of context to the clients, so that they can have important details for errors which the code and HTTP status code are not enough. If that is using Right now we will just build a wrapper around |
|
I hear you! For historical context, this module came out of need for capturing errors through multiple server side layers in our production stacks - so the truth is that we use it to capture a ton of sensitive info that was never intended to go out the wire (and especially not back to our customers). However, I understand that not every service is subject to those same restrictions, and it sounds like you're actually using the error fields to build up the payload that is returned to the client. That's definitely not a use case we had in mind, but sounds like fair game. Rather than change the default serialization, which would just swing us from one end of the spectrum to the other, I think there's probably a way we can provide the ability to set a library wide defaults for all errors (of which serialization would be one of many). A strawman might be something along the lines of: const errs = require('restify-errors');
errs.defaults.toJSON = function toJSON() { ... } . // => would get applied to prototype of all errors created by this moduleWould that work? |
|
yes, that would work! |
|
Closing since this is probably not moving forward. |
We need to return extra metadata with our errors, and the existing functionality explicitly stripped the extra context. This is similar to the functionality that existed in in restify 4.x (although it was in a different format). Do you think this is a good addition?
CC: @ghermeto @m5m1th