Standalone standalone standalone error from API gateway with Lambda

Is there a way to return a custom error object and status code from the API gateway? I get status 200.

 var response = {
    status: 400,
    errors: [
       {
          code:   "226",
          message: "Password and password confirmation do not match."
        }
    ]
 }

context.done(JSON.stringify(response));

      

+3


source to share


2 answers


This way I can change the Gatway API. I can manage the API response using s-templates.json to add this codebase.

ValidationError":{
  "selectionPattern": ".*ValidationError.*",
  "statusCode": "400",
  "responseParameters": {
    "method.response.header.Access-Control-Allow-Headers": "'Content-
    Type,X-Amz-Date,Authorization,X-Api-Key,Cache-Control,Token'",
    "method.response.header.Access-Control-Allow-Methods": "'*'",
    "method.response.header.Access-Control-Allow-Origin": "'*'"
  },
  "responseModels": {},
  "responseTemplates": {
    "application/json": "$input.path('$.errorMessage')"
  }
}

      



So I am returning my response with 400 statusCode and a valid message.

module.exports.handler = function(event, context) {
  const validationError={
    statsCode:"ValidationError",
    section:"Login",
    validationType:"emailConfirmation",
    message:"Email is not confirmed",
    otherInfo:"Would you like to get the email again?",
    client:"web|ios|android"
  }
  context.done(null, response);
};

      

+1


source


If you want to respond with an error, you must use the success callback with an error response construct.

If you use the context.fail () callback, AWS will assume the Lambda is technically not working and responds with the default mapping present in your API gateway.



An example of an error response:

'use strict';

module.exports.hello = (event, context, callback) => {
  const response = {
    statusCode: 400,
    body: JSON.stringify({
      errors:[{
        code: "226",
        message:"Password confirmation do not match"
      }]
    }),
  };
  context.done(null, response);
};

      

+3


source







All Articles