forked from bcgov/common-hosted-form-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into test/forms-1226
- Loading branch information
Showing
8 changed files
with
208 additions
and
135 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,77 @@ | ||
const Problem = require('api-problem'); | ||
const Objection = require('objection'); | ||
|
||
/** | ||
* Given a subclass of DBError will create and throw the corresponding Problem. | ||
* If the error is of an unknown type it will not be converted. | ||
* | ||
* @param {DBError} error the error to convert to a Problem. | ||
* @returns nothing | ||
*/ | ||
const _throwObjectionProblem = (error) => { | ||
if (error instanceof Objection.DataError) { | ||
throw new Problem(422, { | ||
detail: 'Sorry... the database does not like the data you provided :(', | ||
}); | ||
} | ||
|
||
if (error instanceof Objection.NotFoundError) { | ||
throw new Problem(404, { | ||
detail: "Sorry... we still haven't found what you're looking for :(", | ||
}); | ||
} | ||
|
||
if (error instanceof Objection.ConstraintViolationError) { | ||
throw new Problem(422, { | ||
detail: 'Constraint Violation Error', | ||
}); | ||
} | ||
|
||
if (error instanceof Objection.ValidationError) { | ||
throw new Problem(422, { | ||
detail: 'Validation Error', | ||
errors: error.data, | ||
}); | ||
} | ||
}; | ||
|
||
/** | ||
* Send an error response for all errors except 500s, which are handled by the | ||
* code in app.js. | ||
* | ||
* @param {*} err the Error that occurred. | ||
* @param {*} _req the Express object representing the HTTP request - unused. | ||
* @param {*} res the Express object representing the HTTP response. | ||
* @param {*} next the Express chaining function. | ||
* @returns nothing | ||
*/ | ||
module.exports.errorHandler = async (err, _req, res, next) => { | ||
try { | ||
if (err instanceof Objection.DBError || err instanceof Objection.NotFoundError || err instanceof Objection.ValidationError) { | ||
_throwObjectionProblem(err); | ||
} | ||
|
||
// Express throws Errors that are not Problems, but do have an HTTP status | ||
// code. For example, 400 is thrown when POST bodies are malformed JSON. | ||
if (!(err instanceof Problem) && (err.status || err.statusCode)) { | ||
throw new Problem(err.status || err.statusCode, { | ||
detail: err.message, | ||
}); | ||
} | ||
|
||
// Not sure what it is, so also handle it in the catch block. | ||
throw err; | ||
} catch (error) { | ||
if (error instanceof Problem && error.status !== 500) { | ||
// Handle here when not an internal error. These are mostly from buggy | ||
// systems using API Keys, but could also be from frontend bugs. Note that | ||
// this does not log the error (see below). | ||
error.send(res); | ||
} else { | ||
// HTTP 500 Problems and all other exceptions should be handled by the | ||
// error handler in app.js. It will log them at the ERROR level and | ||
// include a full stack trace. | ||
next(error); | ||
} | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module.exports = { | ||
...require('./dataErrors'), | ||
...require('./errorHandler'), | ||
...require('./rateLimiter'), | ||
}; |
This file was deleted.
Oops, something went wrong.
120 changes: 120 additions & 0 deletions
120
app/tests/unit/forms/common/middleware/errorHandler.spec.js
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,120 @@ | ||
const { getMockRes } = require('@jest-mock/express'); | ||
const Problem = require('api-problem'); | ||
const Objection = require('objection'); | ||
|
||
const middleware = require('../../../../../src/forms/common/middleware'); | ||
|
||
describe('test error handler middleware', () => { | ||
it('should handle an objection data error', () => { | ||
const error = new Objection.DataError({ | ||
nativeError: { message: 'This is a DataError' }, | ||
}); | ||
const { res, next } = getMockRes(); | ||
|
||
middleware.errorHandler(error, {}, res, next); | ||
|
||
expect(next).not.toBeCalled(); | ||
expect(res.end).toBeCalledWith(expect.stringContaining('422')); | ||
}); | ||
|
||
it('should handle an objection not found error', () => { | ||
const error = new Objection.NotFoundError({ | ||
nativeError: { message: 'This is a NotFoundError' }, | ||
}); | ||
const { res, next } = getMockRes(); | ||
|
||
middleware.errorHandler(error, {}, res, next); | ||
|
||
expect(next).not.toBeCalled(); | ||
expect(res.end).toBeCalledWith(expect.stringContaining('404')); | ||
}); | ||
|
||
it('should handle an objection unique violation error', () => { | ||
const error = new Objection.UniqueViolationError({ | ||
nativeError: { message: 'This is a UniqueViolationError' }, | ||
}); | ||
const { res, next } = getMockRes(); | ||
|
||
middleware.errorHandler(error, {}, res, next); | ||
|
||
expect(next).not.toBeCalled(); | ||
expect(res.end).toBeCalledWith(expect.stringContaining('422')); | ||
}); | ||
|
||
it('should handle an objection validation error', () => { | ||
const error = new Objection.ValidationError({ | ||
nativeError: { message: 'This is a ValidationError' }, | ||
}); | ||
const { res, next } = getMockRes(); | ||
|
||
middleware.errorHandler(error, {}, res, next); | ||
|
||
expect(next).not.toBeCalled(); | ||
expect(res.end).toBeCalledWith(expect.stringContaining('422')); | ||
}); | ||
|
||
it('should handle non-problem errors with a status', () => { | ||
const error = new Error('This is a 400 status.'); | ||
error.status = 400; | ||
const { res, next } = getMockRes(); | ||
|
||
middleware.errorHandler(error, {}, res, next); | ||
|
||
expect(next).not.toBeCalled(); | ||
expect(res.end).toBeCalledWith(expect.stringContaining('400')); | ||
expect(res.end).toBeCalledWith(expect.stringContaining('This is a 400 status.')); | ||
}); | ||
|
||
it('should handle non-problem errors with a status code', () => { | ||
const error = new Error('This is a 400 status code.'); | ||
error.statusCode = 400; | ||
const { res, next } = getMockRes(); | ||
|
||
middleware.errorHandler(error, {}, res, next); | ||
|
||
expect(next).not.toBeCalled(); | ||
expect(res.end).toBeCalledWith(expect.stringContaining('400')); | ||
expect(res.end).toBeCalledWith(expect.stringContaining('This is a 400 status code.')); | ||
}); | ||
|
||
it('should handle any non-500 Problems', () => { | ||
const error = new Problem(429); | ||
const { res, next } = getMockRes(); | ||
|
||
middleware.errorHandler(error, {}, res, next); | ||
|
||
expect(next).not.toBeCalled(); | ||
expect(res.end).toBeCalledWith(expect.stringContaining('429')); | ||
}); | ||
|
||
it('should pass through unknown objection errors', () => { | ||
const error = new Objection.DBError({ | ||
nativeError: { | ||
message: 'This base class is never actually instantiated', | ||
}, | ||
}); | ||
const { res, next } = getMockRes(); | ||
|
||
middleware.errorHandler(error, {}, res, next); | ||
|
||
expect(next).toBeCalledWith(error); | ||
}); | ||
|
||
it('should pass through any 500s', () => { | ||
const error = new Problem(500); | ||
const { next } = getMockRes(); | ||
|
||
middleware.errorHandler(error, {}, {}, next); | ||
|
||
expect(next).toBeCalledWith(error); | ||
}); | ||
|
||
it('should pass through any Errors without statuses', () => { | ||
const error = new Error(); | ||
const { next } = getMockRes(); | ||
|
||
middleware.errorHandler(error, {}, {}, next); | ||
|
||
expect(next).toBeCalledWith(error); | ||
}); | ||
}); |