@loves/loves-web-error-handler
Advanced tools
Comparing version 0.1.0 to 0.2.0
76
app.js
@@ -1,12 +0,70 @@ | ||
const webErrorHandler = (err, env = 'production') => { | ||
const res = { | ||
message: err.message, | ||
statusCode: err.status || 500, | ||
}; | ||
if (env === 'development') { | ||
res.error = err.stack; | ||
} | ||
return res; | ||
const webErrorHandler = { | ||
formatError(err) { | ||
const resData = {}; | ||
resData.message = err.message; | ||
resData.statusCode = err.status || err.statusCode || 500; | ||
if (err.errorCode) { | ||
resData.errorCode = err.errorCode; | ||
} | ||
if (err.errors) { | ||
resData.errors = err.errors; | ||
} | ||
return resData; | ||
}, | ||
badRequest(message, errorCode) { | ||
const err = {}; | ||
err.message = message || 'Invalid request.'; | ||
err.statusCode = 400; | ||
if (errorCode) { | ||
err.errorCode = errorCode; | ||
} | ||
return err; | ||
}, | ||
resourceNotFound(message, errorCode) { | ||
const err = {}; | ||
err.message = message || 'The requested resource could not be found.'; | ||
err.statusCode = 404; | ||
if (errorCode) { | ||
err.errorCode = errorCode; | ||
} | ||
return err; | ||
}, | ||
internalServerError(message, errorCode) { | ||
const err = {}; | ||
err.message = message || 'An error has occurred.'; | ||
err.statusCode = 500; | ||
if (errorCode) { | ||
err.errorCode = errorCode; | ||
} | ||
return err; | ||
}, | ||
validationError(message, errorCode) { | ||
const err = {}; | ||
err.message = message || 'Form validation failed.'; | ||
err.statusCode = 422; | ||
if (errorCode) { | ||
err.errorCode = errorCode; | ||
} | ||
return err; | ||
}, | ||
addValidationError(err, field, message) { | ||
if (err) { | ||
const errors = err.errors || []; | ||
const fieldError = { | ||
field, | ||
message, | ||
}; | ||
errors.push(fieldError); | ||
const retError = { | ||
message: err.message, | ||
statusCode: err.statusCode, | ||
errorCode: err.errorCode, | ||
errors, | ||
}; | ||
return retError; | ||
} | ||
return err; | ||
}, | ||
}; | ||
module.exports = webErrorHandler; |
{ | ||
"name": "@loves/loves-web-error-handler", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A web error handler that formats responses.", | ||
@@ -21,3 +21,3 @@ "main": "app.js", | ||
"mocha": "^3.3.0", | ||
"nyc": "^10.2.0", | ||
"nyc": "^11.2.1", | ||
"sinon": "^2.1.0" | ||
@@ -30,3 +30,6 @@ }, | ||
}, | ||
"homepage": "https://github.com/LovesTravelStops/loves-web-error-handler#readme" | ||
"homepage": "https://github.com/LovesTravelStops/loves-web-error-handler#readme", | ||
"dependencies": { | ||
"chai": "^4.1.2" | ||
} | ||
} |
# Love's Web Error Handler | ||
A library to format errors as JSON to send back to web clients based on where the application is | ||
running. | ||
A library to format errors as JSON to send back to web clients based on where the application is | ||
running. | ||
## Installation | ||
`npm install @loves/loves-web-error-handler` | ||
## Usage | ||
Creates consistent web api errors in the format: | ||
{ | ||
statusCode: 500, | ||
errorCode: '', | ||
message: '', | ||
errors: [ | ||
{ | ||
field: '', | ||
message: '' | ||
} | ||
] | ||
} | ||
`const lovesErrorHandler = require('@loves/loves-web-error-handler');` | ||
`formatError(err)` -- Converts Error object to above. Retains all above fields if provided. | ||
`badRequest(message, errorCode)` -- Creates a 400 with customizable error messaging (Default: 'Invalid request.') | ||
`resourceNotFound(message, errorCode)` -- Creates a 404 with customizable error messaging (Default: 'The requested resource could not be found.') | ||
`internalServerError(message, errorCode)` -- Creates a 500 with customizable error messaging (Default: 'An error has occurred.') | ||
`validationError(message, errorCode)` -- Creates a 422 with customizable error messaging (Default: 'Form validation failed.') | ||
`addValidationError(err, field, message)` -- Appends field level validation error messaging to existing error object. |
@@ -1,51 +0,147 @@ | ||
const assert = require('assert'); | ||
const chai = require('chai'); | ||
const testableApp = require('../app'); | ||
const testable = require('../app'); | ||
const expect = chai.expect; | ||
describe('The error controller...', () => { | ||
it('should send an error obj in dev with the specified error code.', async () => { | ||
const err = new Error('Error parsing data.'); | ||
err.status = 500; | ||
const env = 'development'; | ||
describe('The error controller', () => { | ||
it('should send an error obj in dev with the error code', async () => { | ||
// setup | ||
const err = new Error('This is a big old error.'); | ||
err.status = 404; | ||
const expected = { | ||
message: err.message, | ||
error: err.stack, | ||
statusCode: err.status, | ||
}; | ||
const actual = testable(err, env); | ||
assert.deepEqual(actual, expected); | ||
// exercise | ||
const actual = testableApp.formatError(err); | ||
// assert | ||
expect(actual).to.deep.equal(expected); | ||
// tear down | ||
}); | ||
it('should default to a 500 when no error code is present.', async () => { | ||
const err = new Error('Error parsing data.'); | ||
const errStatus = 500; | ||
const env = 'development'; | ||
it('should send an error obj in dev with a 500', async () => { | ||
// setup | ||
const err = new Error('Here is an error to read.'); | ||
const expected = { | ||
message: err.message, | ||
error: err.stack, | ||
statusCode: errStatus, | ||
statusCode: 500, | ||
}; | ||
const actual = testable(err, env); | ||
assert.deepEqual(actual, expected); | ||
// exercise | ||
const actual = testableApp.formatError(err); | ||
// assert | ||
expect(actual).to.deep.equal(expected); | ||
// tear down | ||
}); | ||
it('should send a trimmed error obj in prod with the specified error code.', async () => { | ||
const env = 'production'; | ||
const err = new Error('You should not see this error.'); | ||
err.status = 500; | ||
it('should create 400 error correctly', () => { | ||
const expected = { | ||
message: err.message, | ||
message: 'Invalid request.', | ||
statusCode: 400, | ||
}; | ||
const actual = testableApp.badRequest(); | ||
expect(actual).to.deep.equal(expected); | ||
}); | ||
it('should create 400 error correctly with special message', () => { | ||
const message = 'Different 400 error message'; | ||
const errorCode = 'error_code'; | ||
const expected = { | ||
message, | ||
errorCode, | ||
statusCode: 400, | ||
}; | ||
const actual = testableApp.badRequest(message, errorCode); | ||
expect(actual).to.deep.equal(expected); | ||
}); | ||
it('should create 404 error correctly', () => { | ||
const expected = { | ||
message: 'The requested resource could not be found.', | ||
statusCode: 404, | ||
}; | ||
const actual = testableApp.resourceNotFound(); | ||
expect(actual).to.deep.equal(expected); | ||
}); | ||
it('should create 404 error correctly with special message', () => { | ||
const message = 'Different 404 error message'; | ||
const errorCode = 'error_code'; | ||
const expected = { | ||
message, | ||
errorCode, | ||
statusCode: 404, | ||
}; | ||
const actual = testableApp.resourceNotFound(message, errorCode); | ||
expect(actual).to.deep.equal(expected); | ||
}); | ||
it('should create internal server error correctly', () => { | ||
const expected = { | ||
message: 'An error has occurred.', | ||
statusCode: 500, | ||
}; | ||
const actual = testable(err, env); | ||
assert.deepEqual(actual, expected); | ||
const actual = testableApp.internalServerError(); | ||
expect(actual).to.deep.equal(expected); | ||
}); | ||
it('should assume the highest env level when none is provided.', () => { | ||
const err = new Error('You should not see this error.'); | ||
err.status = 500; | ||
it('should create internal server error correctly with special message', () => { | ||
const message = 'Different 500 error message'; | ||
const errorCode = 'error_code'; | ||
const expected = { | ||
message: err.message, | ||
message, | ||
errorCode, | ||
statusCode: 500, | ||
}; | ||
const actual = testable(err); | ||
assert.deepEqual(actual, expected); | ||
const actual = testableApp.internalServerError(message, errorCode); | ||
expect(actual).to.deep.equal(expected); | ||
}); | ||
it('should add validation errors correctly', () => { | ||
const message = 'Form validation failed.'; | ||
const errorCode = 'error_code'; | ||
const statusCode = 422; | ||
const fieldErrorMessage = 'Field is required'; | ||
const errorField = 'zipCode'; | ||
const err = testableApp.validationError(message, errorCode); | ||
const expected = { | ||
message, | ||
errorCode, | ||
statusCode, | ||
errors: [ | ||
{ | ||
field: errorField, | ||
message: fieldErrorMessage, | ||
}, | ||
], | ||
}; | ||
const actual = testableApp.addValidationError(err, errorField, fieldErrorMessage); | ||
expect(actual).to.deep.equal(expected); | ||
}); | ||
it('should format errors and preserve all necessary validation errors fields correctly', () => { | ||
const message = 'Form validation failed.'; | ||
const errorCode = 'error_code'; | ||
const statusCode = 422; | ||
const fieldErrorMessage = 'Field is required'; | ||
const errorField = 'zipCode'; | ||
const err = testableApp.validationError(message, errorCode); | ||
const expected = { | ||
message, | ||
errorCode, | ||
statusCode, | ||
errors: [ | ||
{ | ||
field: errorField, | ||
message: fieldErrorMessage, | ||
}, | ||
], | ||
}; | ||
const thrownError = testableApp.addValidationError(err, errorField, fieldErrorMessage); | ||
const actual = testableApp.formatError(thrownError); | ||
expect(actual).to.deep.equal(expected); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
79803
210
39
1
1
+ Addedchai@^4.1.2
+ Addedassertion-error@1.1.0(transitive)
+ Addedchai@4.5.0(transitive)
+ Addedcheck-error@1.0.3(transitive)
+ Addeddeep-eql@4.1.4(transitive)
+ Addedget-func-name@2.0.2(transitive)
+ Addedloupe@2.3.7(transitive)
+ Addedpathval@1.1.1(transitive)
+ Addedtype-detect@4.1.0(transitive)