2

I've read the docs for serverless and AWS but I'm just not getting the response templates. I have a lambda function that calls another module and that module will return an object with an info and possibly an error property. If the error property exists, i want to throw an error and handle it with a response template in API Gateway.

serverless.yml (not sure what goes in the 400 pattern)

functions:
  onboard:
    handler: api/onboard.onboard
    events:
      - http:
          path: /onboard
          method: post
          integration: lambda
          request:
            passThrough: NEVER
            schema:
              application/json: ${file(models/onboard.schema.json)}
            template:
              application/json: $input.body
          response:
            200:
              pattern: ''
            400:
              pattern: '.*"error".*' // WHAT GOES HERE???
              template: $input.path('$.errorMessage')

lambda

const onboard = async (event) => {
  // returns an object with info/error property
  const response = await someModule();
  /* example response
  const response = {
    error: 'some error',
    name: event.name
  };
  */
  
  if(response.error) {
    context.done(JSON.stringify(response));
  }

  return response;
};
CoryDorning
  • 1,854
  • 4
  • 25
  • 36

1 Answers1

2

I was missing the statusCodes property.

functions:
  onboard:
    handler: api/onboard.onboard
    events:
      - http:
          path: /onboard
          method: post
          integration: lambda
          request:
            passThrough: NEVER
            schema:
              application/json: ${file(models/onboard.schema.json)}
            template:
              application/json: $input.body
          response:
            statusCodes:
              200:
                pattern: ''
              400:
                pattern: '.*"error".*'
                template: $input.path('$.errorMessage')
CoryDorning
  • 1,854
  • 4
  • 25
  • 36