Skip to content

A middy middleware that returns errors as http errors, compatible with http-errors.

License

Notifications You must be signed in to change notification settings

maximpn/middy-middleware-json-error-handler

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

middy-middleware-json-error-handler

npm version downloads open issues FOSSA Status debug build status codecov dependency status devDependency status semantic release

A middy middleware that returns errors as http errors, compatible with http-errors.

Installation

Download node at nodejs.org and install it, if you haven't already.

npm install middy-middleware-json-error-handler --save

Documentation

There is additional documentation.

Usage

import middy from '@middy/core'
import JSONErrorHandlerMiddleware from 'middy-middleware-json-error-handler'
import createHttpError from 'http-errors'
import { APIGatewayEvent } from 'aws-lambda'

// This is your AWS handler
async function helloWorld (event: APIGatewayEvent) {
  if (event.queryStringParameters?.search == null) {
    // If you throw an error with status code, the error will be returned as stringified JSON.
    // Only the stack will be omitted.
    throw createHttpError(400, 'Query has to include a search')
  }

  // If you throw an error with no status code, only a generic message will be shown to the user
  // instead of the full error
  throw new Error('Search is not implemented yet')
}

// Let's "middyfy" our handler, then we will be able to attach middlewares to it
export const handler = middy(helloWorld)
  .use(JSONErrorHandlerMiddleware()) // This middleware is needed do handle the errors thrown by the handler

About

A middy middleware that returns errors as http errors, compatible with http-errors.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 78.5%
  • JavaScript 21.5%