@expresso/errors
TypeScript icon, indicating that this package has built-in type declarations

1.3.3 • Public • Published

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

Package Sidebar

Install

npm i @expresso/errors

Weekly Downloads

18

Version

1.3.3

License

GPL-3.0

Unpacked Size

12.2 kB

Total Files

18

Last publish

Collaborators

  • rjmunhoz
  • khaosdoctor