Skip to content

Error – API

Bartosz Łaniewski edited this page Mar 30, 2018 · 1 revision

API

import errors, {handlers, defineError} from "inra-server-error";

A simple, yet powerful error handler shipped as a middleware for Koa. Defines a global error handler which will generate responses when exceptions are thrown inside your app. Returns two kinds of messages:

  • a userMessage which is intended to be shown for final clients;
  • a developerMessage which is intended to be used for debuggins and error tracking (disabled on production);
{
  "status": 500,
  "errorCode": "ERR_CONSTANT_SERVICE_BAR",
  "userMessage": "Something went wrong!",
  "developerMessage": "Stacktrace, error details",
}

errors(options)

Returns a Koa middleware which can be used inside app.use().

handlers

Returns a Map containing all created error definitions.

defineError(definition)

Adds an error definition to the list. Defines an error handler for a specified exception type.

Examples

app.use(errors({
  httpStatus: 500, // default HTTP Status,
  userMessage: "Internal Error" // default user message
}));

defineError({instance: AuthError});
defineError({instance: ConnectionError, errorCode: "…"});
defineError({instance: ValidationError, httpStatus: 400});