Skip to content

middy-jsonapi treats BadRequest errors from Middy validation as unhandled errors #6

Description

@kpeters-cbsi

I have a Typescript Lambda set up like so:

import { APIGatewayProxyHandler } from 'aws-lambda'
import middy from '@middy/core'
import cors from '@middy/http-cors'
import httpJsonBodyParser from '@middy/http-json-body-parser'
import jsonapi from 'middy-jsonapi'
import validator from '@middy/validator'
import httpContentNegotiation from '@middy/http-content-negotiation'

const main: APIGatewayProxyHandler = async (event) => { 
  ...
}

export const handler = middy(main)
  .use(httpJsonBodyParser())
  .use(cors())
  .use(jsonapi({ response }))
  .use(
    httpContentNegotiation({
      availableLanguages: ['en-US'],
      availableMediaTypes: ['application/vnd.api+json'],
    })
  )
  .use(validator({ inputSchema }))

But when I send an invalid payload to it, I get a 500 back instead of a 400. I've traced the error to line 72 of index.js. The error class is BadRequestError, but handler.error instanceof createError.BadRequest evaluates to false. Not sure where to take it from there, otherwise I'd whip up a PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions