1.4.0 • Published 6 years ago

inra-server-error v1.4.0

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

inra-server-error

npm Dependency Status

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

Github Open Issues Github Closed Issues Github Pull Requests

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

CommandDescription
buildBuilds inra-server-error
watchRe-builds inra-server-error on changes
cleanDeletes builds ands cache
lintFixes Lint errors
flowChecks Flow errors
testChecks Flow errors and runs tests