简体   繁体   中英

Return Error response in AWS API Lambda Proxy Integration

I am using aws lambda proxy integration.

I hit the API in response if Success means 200 response is coming if error means 500 error is coming correct.

        {
    try:
          return {
                "statusCode": str(200),
                "body": json.dumps("Hello"),
                "headers": headers,
            }

    except Exception as e:
         return {
                "statusCode": str(500),
                "body": json.dumps("Error"),
                "headers": headers,
            }

}

But when I am trying to return the exception from another method inside the lambda in a postman error response is coming, but statuscode is 200 response is there. Below is the code.

    def getMethod(val):
   try:
        a= val
        return {
            "statusCode": str(200),
            "body": json.dumps("Success"),
            "headers": headers,
        }
    except Exception as e:
         return {
            "statusCode": str(500),
            "body": json.dumps("Error in getMethod"),
            "headers": headers,
        }

handler request(event,context):
    try:
      response = getMethod(
      return {
            "statusCode": str(200),
            "body": json.dumps(response),
            "headers": headers,
        }

    except Exception as e:
      return {
            "statusCode": str(500),
            "body": json.dumps("Error"),
            "headers": headers,
        }

I am sorry I didn't understand your code. You have called the "getMethod" with a return value :

return { "statusCode": str(200), "body": json.dumps(response), "headers": headers, }

in which you have specified "response" which is the return value of getMethod. Most probably you're getting the 200 response because the getMethod is evaluating to return the try part and not the except part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM