Skip to content

Error standardization middleware for expresso

Notifications You must be signed in to change notification settings

expresso/errors

Repository files navigation

Errors

Error middleware for expresso

Summary

How this works

The error middleware will receive all errors from @expresso/app (and every other error thrown inside any routes) and it is going to sanitize it to a default response pattern with message, code and stack informations. It'll also catch any unknown error messages and parse them as HTTP 500 errors.

Basic usage

Install it:

$ npm i @expresso/errors

Import and use it:

import route from './route'
import expresso, { IExpressoConfigOptions } from '@expresso/app'
import server from '@expresso/server'
import errors from '@expresso/errors'

interface IAppConfig extends IAuthConfig, IExpressoConfigOptions {}

const appFactory = expresso((app, config: IAppConfig, environment) => {

    app.get('/', route)

    app.use(errors(environment))
})

const options = {
  name: 'myApp',
  jwt: {
    algorithms: ['HS256'],
    audience: 'your-audience',
    issuer: 'your-issuer',
    secret: 'shhhhh'
  }
}

server.start(appFactory, options)

The error middleware receives a string declaring the current environment for your application. If this environment is different from production, then all the error stack will also be displayed.

Adding additional data

If you'd like to include more data in the error, pass on a boom error with { additionalData: yourData } as second parameter.

Example:

if (err instanceof ExternalAPIError) return next(boom.serverUnavailable(err.message, { additionalProperties: err.data }))

This will render the added data to the response