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

JSON stringified error does not contain message #20

Closed
Jokero opened this issue Oct 12, 2015 · 7 comments
Closed

JSON stringified error does not contain message #20

Jokero opened this issue Oct 12, 2015 · 7 comments
Assignees

Comments

@Jokero
Copy link

Jokero commented Oct 12, 2015

In my app I use res.status(err.status).send(err) to send error. send uses JSON.stringify for objects. Stringified error does not contain message.

Example:

var createError = require('http-errors');

var error  = createError(404, 'message', { description: 'description' });
var error2 = createError(404, { message: 'message', description: 'description' });

console.log(JSON.stringify(error));  // {"description":"description"}
console.log(JSON.stringify(error2)); // {"description":"description"}
@dougwilson dougwilson self-assigned this Oct 13, 2015
@dougwilson
Copy link
Contributor

As far as I know, this is the way JavaScript itself works (try running JSON.stringify(new Error('oops!'))). If you would like to fix this, please feel free to make a PR :)

@dougwilson
Copy link
Contributor

@Jokero any interest in making a PR for this?

@Jokero
Copy link
Author

Jokero commented Oct 24, 2015

I want to make a PR in the future to completely update this module. https://github.com/Jokero/http-error/blob/master/index.js

@dougwilson
Copy link
Contributor

Ok, @Jokero , though this module's interface is part of Koa, so any major changes please make sure you discuss with the Koa project first.

@jonathanong
Copy link
Member

i'm 👍 to making everything stringify-able, particularly err.message and err.code (i think it was code that couldn't be stringified...)

here's of an example of us hacking around it: https://github.com/stream-utils/raw-body/blob/1e45346682da648ab02e5c92577de10caab905ac/index.js#L167

i don't see how this would break backwards incompatibility, but we could just add it to koa v2

@Jokero
Copy link
Author

Jokero commented Mar 4, 2016

@jonathanong As workaround I created separate npm module https://github.com/Jokero/http-error-constructor

Every property instead of statusCode, status and name is stringified. I excluded statusCode and status because you can always get it from response without any additional field in error body.

It's possible to create error with custom status code (there is http-errors issue for it). I use real constructors instead of error factories, so additionally you can do like this:

var err = new HttpError.BadRequest();
console.log(err instanceof HttpError);            // true
console.log(err instanceof HttpError.BadRequest); // true

It fixes another issue.

@dougwilson
Copy link
Contributor

@jonathanong what do you think if we just added a toJSON method to the root prototype that would understand how to serialize all the properties (especially the non-own properties like statusCode)? It could potentially implement the .expose logic in the serialization, but not sure if that's a good idea or not.

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

No branches or pull requests

3 participants