Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.2k
Suppress error handler stack traces in production#564#1502
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
3baba47791c7ac32f758d5bfed4d7eba03c227470506cad8207666d4b700801826dc98c60ff5e8527f52File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| var expressErrorHandler = require('errorhandler'); | ||
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. One more thing to address. Please make | ||
| expressErrorHandler.title = 'Loopback'; | ||
| module.exports = errorHandler; | ||
| function errorHandler(options) { | ||
| if (!options || options.includeStack !== false) { | ||
| return expressErrorHandler(options); | ||
| } else { | ||
| return function errorHandler(err, req, res, next) { | ||
| delete err.stack; | ||
| return expressErrorHandler(options)(err, req, res, next); | ||
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. Creating a new | ||
| }; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| var loopback = require('../'); | ||
| var app; | ||
| var assert = require('assert'); | ||
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 don't mock req/res objects, express maintainers are discouraging that. Setup a full loopback app and make a real request using supertest instead. varapp=loopback();app.use(loopback.urlNotFound());app.use(loopback.errorHandler(/* opts */));request(app).get('/url-does-not-exist')// verify that the response does or does not contain stack | ||
| var request = require('supertest'); | ||
| describe('loopback.errorHandler(options)', function() { | ||
| it('should return default middleware when options object is not present', function(done) { | ||
| //arrange | ||
| var app = loopback(); | ||
| app.use(loopback.urlNotFound()); | ||
| app.use(loopback.errorHandler({ log: false })); | ||
| //act/assert | ||
| request(app) | ||
| .get('/url-does-not-exist') | ||
| .end(function(err, res) { | ||
| assert.ok(res.error.text.match(/<ul id="stacktrace"><li> at raiseUrlNotFoundError/)); | ||
| done(); | ||
| }); | ||
| }); | ||
| it('should delete stack when options.includeStack is false', function(done) { | ||
| //arrange | ||
| var app = loopback(); | ||
| app.use(loopback.urlNotFound()); | ||
| app.use(loopback.errorHandler({ includeStack: false, log: false })); | ||
| //act/assert | ||
| request(app) | ||
| .get('/url-does-not-exist') | ||
| .end(function(err, res) { | ||
| assert.ok(res.error.text.match(/<ul id="stacktrace"><\/ul>/)); | ||
| done(); | ||
| }); | ||
| }); | ||
| it('should pass options on to error handler module', function(done) { | ||
| //arrange | ||
| var app = loopback(); | ||
| app.use(loopback.urlNotFound()); | ||
| app.use(loopback.errorHandler({ includeStack: false, log: customLogger })); | ||
| //act | ||
| request(app).get('/url-does-not-exist').end(); | ||
| //assert | ||
| function customLogger(err, str, req) { | ||
| assert.ok(err.message === 'Cannot GET /url-does-not-exist'); | ||
| done(); | ||
| } | ||
| }); | ||
| }); | ||
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.
Nitpick: group by property name, not by operations.
In this particular case, I'd personally keep
loopback[error-handler]so that it can be loaded in middleware.json vialoopback#error-handler. Not a big deal though.