wcp-errors
Package for normalizing api errors using the following format:
Install
npm install wcp-errors
Usage
const { notFound } = require('wcp-errors');badRequest('first name is required.');
Examples (ExpressJS)
app; // Bad request exampleapp; app; // Throw! exampleapp; // Error handlerapp;
Run the examples
cd example/express
npm install
npm start
Open a browser and run
- http://localhost:3000/not_found
- http://localhost:3000/bad_request
- http://localhost:3000/multiple_errors
- http://localhost:3000/throws
APIs
Basic
All basic Apis take the following three optional arguments:
message
: a string describing the errorerror
: anError
objecttarget
: an object with shape{ type, name }
All APIs
;;;;;;;;;;
Optionally, add additional errors to a wcp error
// Create an error and add additional error(s) to the wcp error
Raw
The raw API is only necessary in circumstances where the Basic are not sufficient.
const ApiError = ; // Manually create a new API errorconst e = statusCode: 409 code: 'conflict' message error // optional error target // optional target
TODO
- Create basic api functions for all http errors
- Normalize basic apis with add api
- Create dedicated Express middleware, such that a user does not have to write the fallback error handler middleware.
ex: (currently user's must create a fallback middleware similiar to the following)
app;
Contributers
Contributers are welcome! Please submit a PR.
License
MIT