1.0.0-alpha.1 • Published 4 years ago

@sstack/errors v1.0.0-alpha.1

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

@sstack/errors

Parse and create a valid JSON API spec error response from Error instances.

npm i @sstack/errors --save

Usage

Because this middleware produces a JSON response, you must stringify it after the error has been handled.

const { sstack, main } = require('sstack')
const { stringify } = require('@sstac/json')
const errors = require('@sstack/errors')

exports.handler = sstack([
  main(event => {
    throw new Error('Something went wrong')
  })
], [
  errors(),
  stringify()
])

The above will return the following response:

{
  "errors": [
    {
      "status": 500,
      "details": "Something went wrong"
    }
  ]
}

To attach more data, do so on the event, following the JSON API spec.

exports.handler = sstack([
  main(event => {
    const error = new Error(`Sorry, we couldn't process that.`)
    error.statusCode = 422
    error.title = 'UnprocessableEntity'
    error.code = 'code_for_error'
    throw error
  })
], [
  errors(),
  stringify()
])

This will produce the following response:

{
  "errors": [
    {
      "status": 422,
      "title": "UnprocessableEntity",
      "details": "Sorry, we couldn't process that.",
      "code": "code_for_error"
    }
  ]
}

To ease this, consider using http-errors when throwing errors in your application.

License

MIT License © Eric Bailey

1.0.0-alpha.1

4 years ago

0.1.4

5 years ago

0.1.0

5 years ago

0.0.4

5 years ago