inra-server-error
A simple, yet powerful error handler shipped as a middleware for Koa.
inra-server-error
provides a set of utilities for defining and returning HTTP errors. Each utility returns an error response object which includes the following properties:
{
"success": "…",
"errorCode": "…",
"userMessage": "…",
"developerMessage": "…"
}
Note: full documentation with more examples is published on our Wiki. Please, refer to our Wiki for installation details and API references.
Installation
$ npm install --save inra-server-error
API
import error, {defineError} from "inra-server-error";
error(options)
Koa errors middleware which catches exceptions thrown inside other middlewares or routes and generates a graceful response.
Example:
app.use(error({
errorCode: "Default error code"
httpStatus: "Default HTTP status",
userMessage: "Default user message",
callback(error) {
// Will be executed on each exception
}
}));
defineError(definition)
Defines an error handler for a specified exception type.
Note: to make our middleware handle custom errors, you must define an error with exception's instance. Other fields are optional.
Example:
defineError({
instance: AuthWrongUsernameError,
errorCode: "Optional error code"
httpStatus: "Optional HTTP status",
userMessage: "Optional user message",
callback(error) {
// …
}
});
class CustomError extends Error {
errorCode = 103;
httpStatus = 401;
userMessage = "Something went wrong";
};
defineError({
instance: CustomError
});
Contributing
Bug reporting
We want contributing to Inra Server to be fun, enjoyable, and educational for anyone, and everyone. Changes and improvements are more than welcome! Feel free to fork and open a pull request. If you have found any issues, please report them here - they are being tracked on GitHub Issues.
Development
We have prepared multiple commands to help you develop inra-server-error
on your own. Don't forget to install all Node.js
dependencies from npm. You will need a local copy of Node.js installed on your machine.
$ npm install
Usage
$ npm run <command>
List of commands
Command | Description |
---|---|
build |
Builds inra-server-error
|
watch |
Re-builds inra-server-error on changes |
clean |
Deletes builds ands cache |
lint |
Fixes Lint errors |
flow |
Checks Flow errors |
test |
Checks Flow errors and runs tests |