JSPM

server-error

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q36684F
  • License MIT

Create an error and send it to a logger

Package Exports

  • server-error

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (server-error) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

server-error stability

npm version build status test coverage downloads js-standard-style

Create an error and send it to a logger.

Usage

const serverError = require('server-error')
const bole = require('bole')

const log = bole('my-package')
const error = serverError(log)

error.client('oh no!')
// {
//   type: 'client',
//   statusCode: 400,
//   message: 'oh no!',
//   url: null
// }

error.client({ message: 'not found', statusCode: 404 })
// {
//   type: 'client',
//   statusCode: 404,
//   message: 'not found',
//   url: null
// }

error.server({ message: 'something went wrong', url: 'http://api.foo.io' })
// {
//   type: 'server',
//   statusCode: 500,
//   message: 'something went wrong',
//   url: 'http://api.foo.io'
// }

error.wrap(new Error('something critical went wrong'))
// {
//   type: 'server',
//   statusCode: 500,
//   message: 'Internal Server Error'
// }

API

error = serverError(log, opts?)

Create a new error object that logs to a logger.

error.client(message)

Create a new client error that defaults to statusCode 400. Takes either a string or object. Uses log.warn.

error.server(message)

Create a new server error that defaults to statusCode 500. Takes either a string or object. Uses log.error.

error.wrap(error)

Wrap an existing error into an error that can be returned to the client. Useful to handle unexpected state without leaking information to the outside world. Uses statusCode 500 and log.error.

Installation

$ npm install server-error

License

MIT