-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
close #2757, refs #5286 - moves error formatting from api/index into errors lib - moves error handling from api/index into its own middleware - adds extra middleware for method not allowed which captures all unsupported routes
- Loading branch information
Showing
10 changed files
with
141 additions
and
69 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var errors = require('../errors'); | ||
|
||
module.exports.methodNotAllowed = function methodNotAllowed(req, res, next) { | ||
next(new errors.MethodNotAllowedError('Unknown method: ' + req.path)); | ||
}; | ||
|
||
module.exports.errorHandler = function errorHandler(err, req, res, next) { | ||
/*jshint unused:false */ | ||
var httpErrors = errors.formatHttpErrors(err); | ||
errors.logError(err); | ||
// Send a properly formatted HTTP response containing the errors | ||
res.status(httpErrors.statusCode).json({errors: httpErrors.errors}); | ||
}; |
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/*globals describe, beforeEach, afterEach, it*/ | ||
/*jshint expr:true*/ | ||
var should = require('should'), | ||
sinon = require('sinon'), | ||
|
||
middleware = require('../../../server/middleware').middleware, | ||
errors = require('../../../server/errors'); | ||
|
||
// To stop jshint complaining | ||
should.equal(true, true); | ||
|
||
describe('Middleware: API Error Handlers', function () { | ||
var sandbox, req, res, next; | ||
|
||
beforeEach(function () { | ||
sandbox = sinon.sandbox.create(); | ||
req = {}; | ||
res = {}; | ||
res.json = sandbox.spy(); | ||
res.status = sandbox.stub().returns(res); | ||
next = sandbox.spy(); | ||
}); | ||
|
||
afterEach(function () { | ||
sandbox.restore(); | ||
}); | ||
|
||
describe('methodNotAllowed', function () { | ||
it('calls next with an error', function () { | ||
req.path = 'test'; | ||
|
||
middleware.api.methodNotAllowed(req, res, next); | ||
|
||
next.calledOnce.should.be.true; | ||
next.firstCall.args[0].code.should.equal(405); | ||
next.firstCall.args[0].errorType.should.equal('MethodNotAllowedError'); | ||
next.firstCall.args[0].message.should.match(/test$/); | ||
}); | ||
}); | ||
|
||
describe('errorHandler', function () { | ||
it('sends a JSON error response', function () { | ||
errors.logError = sandbox.spy(errors, 'logError'); | ||
errors.formatHttpErrors = sandbox.spy(errors, 'formatHttpErrors'); | ||
|
||
var msg = 'Something got lost', | ||
err = new errors.NotFoundError(msg); | ||
|
||
middleware.api.errorHandler(err, req, res, next); | ||
|
||
next.called.should.be.false; | ||
errors.logError.calledOnce.should.be.true; | ||
errors.formatHttpErrors.calledOnce.should.be.true; | ||
|
||
res.status.calledWith(404).should.be.true; | ||
res.json.calledOnce.should.be.true; | ||
res.json.firstCall.args[0].errors[0].message.should.eql(msg); | ||
res.json.firstCall.args[0].errors[0].errorType.should.eql('NotFoundError'); | ||
}); | ||
}); | ||
}); |
This file contains 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 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