-
Notifications
You must be signed in to change notification settings - Fork 35
Allow safeFields to work with arrays #73
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -397,43 +397,96 @@ describe('strong-error-handler', function() { | |
|
|
||
| requestJson().expect(500).end(function(err, res) { | ||
| if (err) return done(err); | ||
| expect(res.body).to.eql({ | ||
| error: { | ||
| statusCode: 500, | ||
| message: 'Internal Server Error', | ||
| }, | ||
| }); | ||
| const data = res.body.error; | ||
| expect(data).to.have.property('message').that.match(/multiple errors/); | ||
| expect(data).to.have.property('details').eql([ | ||
| {statusCode: 500, message: 'Internal Server Error'}, | ||
| {statusCode: 500, message: 'Internal Server Error'}, | ||
| {statusCode: 500, message: 'Internal Server Error'}, | ||
| ]); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('returns all array items when debug=true', function(done) { | ||
| var testError = new ErrorWithProps({ | ||
| const testError = new ErrorWithProps({ | ||
| message: 'expected test error', | ||
| statusCode: 400, | ||
| }); | ||
| var anotherError = new ErrorWithProps({ | ||
| const anotherError = new ErrorWithProps({ | ||
| message: 'another expected error', | ||
| statusCode: 500, | ||
| }); | ||
| var errors = [testError, anotherError, 'ERR STRING']; | ||
| const errors = [testError, anotherError, 'ERR STRING']; | ||
| givenErrorHandlerForError(errors, {debug: true}); | ||
|
|
||
| requestJson().expect(500).end(function(err, res) { | ||
| if (err) return done(err); | ||
|
|
||
| var data = res.body.error; | ||
| const data = res.body.error; | ||
| expect(data).to.have.property('message').that.match(/multiple errors/); | ||
| var expectTestError = getExpectedErrorData(testError); | ||
| delete expectTestError.statusCode; | ||
| var expectAnotherError = getExpectedErrorData(anotherError); | ||
| delete expectAnotherError.statusCode; | ||
|
|
||
| var expectedDetails = [ | ||
| expectTestError, | ||
| expectAnotherError, | ||
| 'ERR STRING', | ||
|
|
||
| const expectedDetails = [ | ||
| getExpectedErrorData(testError), | ||
| getExpectedErrorData(anotherError), | ||
| {message: 'ERR STRING', statusCode: 500}, | ||
| ]; | ||
| expect(data).to.have.property('details').to.eql(expectedDetails); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('includes safeFields of array items when debug=false', (done) => { | ||
| const internalError = new ErrorWithProps({ | ||
| message: 'a test error message', | ||
| code: 'MACHINE_READABLE_CODE', | ||
| details: 'some details', | ||
| extra: 'sensitive data', | ||
| }); | ||
| const validationError = new ErrorWithProps({ | ||
| name: 'ValidationError', | ||
| message: 'The model instance is not valid.', | ||
| statusCode: 422, | ||
| code: 'VALIDATION_ERROR', | ||
| details: 'some details', | ||
| extra: 'sensitive data', | ||
| }); | ||
|
|
||
| const errors = [internalError, validationError, 'ERR STRING']; | ||
| givenErrorHandlerForError(errors, { | ||
| debug: false, | ||
| safeFields: ['code'], | ||
| }); | ||
|
|
||
| requestJson().end(function(err, res) { | ||
| if (err) return done(err); | ||
| const data = res.body.error; | ||
|
|
||
| const expectedInternalError = { | ||
| statusCode: 500, | ||
| message: 'Internal Server Error', | ||
| code: 'MACHINE_READABLE_CODE', | ||
| // notice the property "extra" is not included | ||
| }; | ||
| const expectedValidationError = { | ||
| statusCode: 422, | ||
| message: 'The model instance is not valid.', | ||
| name: 'ValidationError', | ||
| code: 'VALIDATION_ERROR', | ||
| details: 'some details', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please notice that for 4xx errors, we always include additional fields beyond |
||
| // notice the property "extra" is not included | ||
| }; | ||
| const expectedErrorFromString = { | ||
| message: 'Internal Server Error', | ||
| statusCode: 500, | ||
| }; | ||
| const expectedDetails = [ | ||
| expectedInternalError, | ||
| expectedValidationError, | ||
| expectedErrorFromString, | ||
| ]; | ||
|
|
||
| expect(data).to.have.property('message').that.match(/multiple errors/); | ||
| expect(data).to.have.property('details').to.eql(expectedDetails); | ||
| done(); | ||
| }); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit ugly. Before, we would return a short error response:
Now we are returning more details, but these details are not really helpful:
It may be possible to implement some heuristics to omit these "generic" entries from the output, but don't think it's worth the effort and additional complexity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed