Treehouse errors
Custom NodeJS error classes and definitions with an error parser utility function
Installation
Install via npm
npm install tree-house-errors
or via yarn
yarn add tree-house-errors
Error types
ApiError
Base error class which extends from the Error
class.
// All keys are requiredconst error = code: 'BAD_REQUEST_DUE_TO' message: 'This is a bad request' // All keys are optionalconst optionalArgs = message: 'Overwrite the message for custom error' detail: 'Extra details containing pertinent information' stack: 'stacktrace...' throw 400 error optionalArgs;
BadRequestError
extends from ApiError with a preset of status code 400 and BAD_REQUEST as error.
throw ; // orthrow error optionalArgs;
NotFoundError
extends from ApiError with a preset of status code 404 and RESOURCE_NOT_FOUND as error.
throw ; // orthrow error optionalArgs;
ForbiddenError
extends from ApiError with a preset of status code 403 and FORBIDDEN as error.
throw ; // orthrow error optionalArgs;
InternalServerError
extends from ApiError with a preset of status code 500 and INTERNAL_ERROR as error.
throw ; // orthrow error optionalArgs;
UnauthorizedError
extends from ApiError with a preset of status code 401 and UNAUTHORIZED as error.
throw ; // orthrow error optionalArgs;
ValidationError
extends from ApiError with a preset of status code 400 and INVALID_INPUT as error.
throw ; // orthrow error optionalArgs;
AuthenticationError
extends from ApiError with a preset of status code 400 and AUTHENTICATION_FAILED as error.
throw ; // orthrow error optionalArgs;
Error definitions
Predefined error types that can be used over multiple projects with a message and code per type. The current list provides following errors:
INTERNAL_ERROR: code: 'INTERNAL_ERROR'i18n: 'internal_error'message: 'An unkown error occurred' INVALID_INPUT: code: 'INVALID_INPUT' i18n: 'invalid_input' message: 'Invalid input provided' AUTHENTICATION_FAILED: code: 'AUTHENTICATION_FAILED' i18n: 'authentication_failed' message: 'Authentication failed' BAD_REQUEST: code: 'BAD_REQUEST' i18n: 'bad_request' message: 'Bad request' MISSING_HEADERS: code: 'MISSING_HEADERS' i18n: 'missing_headers' message: 'Missing headers' UNAUTHORIZED: code: 'UNAUTHORIZED' i18n: 'unauthorized' message: 'Unauthorized' FORBIDDEN: code: 'FORBIDDEN' i18n: 'forbidden' message: 'No access' RESOURCE_NOT_FOUND: code: 'RESOURCE_NOT_FOUND' i18n: 'resource_not_found' message: 'Resource not found'
Example
throw 400 errorsBAD_REQUEST;
Error parsing
parseErrors(error, i18nOptions (optional))
Parse any data into an error object with all properties needed for jsonade parser. Also parses express-validation
errors.
const error = ...;const parsedError = ; // jsonade serializer afterwards (optional)serializer;
With i18n support (optional):
const error = ...;const parsedError = ; // jsonade serializer afterwards (optional)serializer;
The
parseErrors
function will load the i18n configuration once, and reuse the same instance afterwards. It is not possible to overwrite the configuration after the first call. This has to do with performance and caching of translations.
Tests
- You can run
yarn test
to run all tests - You can run
yarn test:coverage
to run all tests with coverage report
Bugs
When you find issues, please report them:
Be sure to include all of the output from the npm command that didn't work as expected. The npm-debug.log file is also helpful to provide.
Authors
See the list of contributors who participated in this project.
License
This project is licensed under the ISC License - see the LICENSE.md file for details