server-error

1.0.1 • Public • Published

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

Package Sidebar

Install

npm i server-error

Weekly Downloads

7

Version

1.0.1

License

MIT

Last publish

Collaborators

  • yoshuawuyts