Skip to content

Include err.message for debug data - #11

Merged
davidcheung merged 1 commit into
masterfrom
debug-error-message
May 20, 2016
Merged

Include err.message for debug data#11
davidcheung merged 1 commit into
masterfrom
debug-error-message

Conversation

@davidcheung

Copy link
Copy Markdown
Contributor

Because err.message is also not enumerable,
therefore needs to be explicited added to the data obj

@davidcheung

Copy link
Copy Markdown
Contributor Author

@bajtos PTAL

Comment thread lib/data-builder.js Outdated
// NOTE err.stack is not an enumerable property
// NOTE `err.stack` and `err.message` is not an enumerable property
data.stack = err.stack;
if (err.message)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't check whether err.message is set in other fill functions, is there any reason why we can't do the same here?

I'd also prefer to set the message among the first items, because JSON serializers preserve the order in which properties were defined.

data.message = err.message;

for (var p in err) {
  if ((p in data)) continue;
  data[p] = err[p];
}

data.stack = err.stack;

Could you please check that err.name is included in debug-mode data too?

@davidcheung davidcheung May 16, 2016

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we do data.message = err.message;
the result will come back with
"message" : ""


and we need to handle scenario where a non-error is thrown (eg. throw 'string' )
https://github.com/strongloop/strong-error-handler/blob/ff2af3b5555ee84bdb110fef74effbf891a176fa/lib/data-builder.js#L20

do you think data.message = data.message || err.message; would work?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the result will come back with "message" : ""

Hmm, I did was not aware of this edge case. In that case, I guess the following would be best?

if (err.message && !data.message) 
  data.message = err.message;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to handle this edge case in fillBadRequestError too, shouldn't we?

Another option, which may be better, is to change the code handling non-errors so that it modified the err object instead of the data object:

 if (typeof err !== 'object') {
   err = {
      statusCode: 500,
      message: '' + err,
  }
}

And then we can do data.message = err.message everywhere else.

@davidcheung Thoughts?

@bajtos bajtos assigned davidcheung and unassigned bajtos May 16, 2016
@davidcheung
davidcheung force-pushed the debug-error-message branch from deda5be to be5dc33 Compare May 17, 2016 03:33
@bajtos

bajtos commented May 17, 2016

Copy link
Copy Markdown
Member

@davidcheung could you please add a testcase (or two?) to cover "the result will come back with "message" : """

Also cross-posting from collapsed #11 (diff)

the result will come back with "message" : ""

Hmm, I did was not aware of this edge case. In that case, perhaps the following would be better?

if (err.message && !data.message) 
  data.message = err.message;

I think we need to handle this edge case in fillBadRequestError too, shouldn't we?

Another option, which may be even better, is to change the code handling non-errors so that it modified the err object instead of the data object:

 if (typeof err !== 'object') {
   err = {
      statusCode: 500,
      message: '' + err,
  }
}

And then we can do data.message = err.message everywhere else, at least I think so.

@davidcheung Thoughts?

@davidcheung
davidcheung force-pushed the debug-error-message branch from 292d5ac to df1f9dc Compare May 17, 2016 19:47
@davidcheung

davidcheung commented May 17, 2016

Copy link
Copy Markdown
Contributor Author

agreed, i think this approach is better too

Another option, which may be better, is to change the code handling non-errors so that it modified the err object instead of the data object:

 if (typeof err !== 'object') {
   err = {
      statusCode: 500,
      message: '' + err,
  }
}

this test case should handle the throw('STRING') scenario, now with err converted to an object with the property the message: "" should not be an issue anymore (sorry edited message a few times the notification you got is probably different)
https://github.com/strongloop/strong-error-handler/blob/a5129595ef672151d6bfd274af51cfcbe224a60b/test/handler.test.js#L319

@davidcheung could you please add a testcase (or two?) to cover "the result will come back with "message" : """

@davidcheung
davidcheung force-pushed the debug-error-message branch 2 times, most recently from e700850 to 53dc723 Compare May 17, 2016 23:53
@davidcheung

Copy link
Copy Markdown
Contributor Author

another issue to bring up is we are nolonger removing the statusCode from Array Error's details
is this intentional? since we are doing it in strong-remoting
https://github.com/strongloop/strong-remoting/blob/aa391d2e65460196ae2a59493f9dd4844622f35d/lib/rest-adapter.js#L380
I guess this could be addressed in a separate issue / PR

@davidcheung
davidcheung force-pushed the debug-error-message branch from a7c4d38 to 1c94fa7 Compare May 18, 2016 04:21
@davidcheung davidcheung assigned bajtos and unassigned davidcheung May 18, 2016
@davidcheung
davidcheung force-pushed the debug-error-message branch 3 times, most recently from f845a74 to 42613b7 Compare May 18, 2016 15:52
@bajtos

bajtos commented May 19, 2016

Copy link
Copy Markdown
Member

another issue to bring up is we are nolonger removing the statusCode from Array Error's details
is this intentional? since we are doing it in strong-remoting
https://github.com/strongloop/strong-remoting/blob/aa391d2e65460196ae2a59493f9dd4844622f35d/lib/rest-adapter.js#L380
I guess this could be addressed in a separate issue / PR

Probably not intentional, please open a new pull request.

Comment thread lib/data-builder.js Outdated
data.message = '' + err;
err = {};
err = {
name: 'Error',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we omit name in this case? Will it cause any failures in existing tests, either here or in strong-remoting?

@bajtos bajtos assigned davidcheung and unassigned bajtos May 20, 2016
@davidcheung

Copy link
Copy Markdown
Contributor Author

@bajtos I have addressed the comments, PTAL

@davidcheung davidcheung assigned bajtos and unassigned davidcheung May 20, 2016
Comment thread test/handler.test.js Outdated
requestJson().end(function(err, res) {
if (err) return done(err);
expect(res.body).to.have.property('error');
expect(res.body.error.name).to.eql('Error');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expect(res.body).to.have.property('error', 'Error');

@bajtos

bajtos commented May 20, 2016

Copy link
Copy Markdown
Member

Two more nitpicks, the rest LGTM. No further reviews are necessary, just wait for CI pass and squash the commits before landing.

err.message and err.name are not enumerable,
therefore needs to be explicited added to the data obj
@davidcheung
davidcheung force-pushed the debug-error-message branch from 384befc to ef72b5c Compare May 20, 2016 15:00
@davidcheung
davidcheung merged commit ec04268 into master May 20, 2016
@davidcheung
davidcheung deleted the debug-error-message branch May 20, 2016 21:38
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

Successfully merging this pull request may close these issues.

2 participants