dsa1234569 2018-01-25 21:13
浏览 252

转到API网关后面的AWS Lambda:自定义错误消息

I'm trying to handle an error in a Lambda function written in Go.
The Lambda is triggered by API Gateway.
When I respond with a 200, I get the correct response.
But when I respond with a 500 code, I always receive {"message": "Internal server error"}

Here is part of the code:

func newErrReponse(message string) (events.APIGatewayProxyResponse, error) {
    return events.APIGatewayProxyResponse{
        Body:       message,
        StatusCode: 500,
    }, errors.New(message)
}

func handleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    return newErrReponse("some error")
}

func main() {
    lambda.Start(handleRequest)
}

I was expecting "some error", but I always get the internal server error. I tried JSON in the body but that didn't help. The integration request is of type LAMBDA_PROXY. That was the default.

How can I control the error response?

  • 写回答

1条回答 默认 最新

  • dougu7546 2018-01-26 13:52
    关注

    The lambda handler can return 2 values. interface{} and error:
    https://godoc.org/github.com/aws/aws-lambda-go/lambda

    Because I use API Gateway, The interface is of type APIGatewayProxyResponse:
    https://godoc.org/github.com/aws/aws-lambda-go/events#APIGatewayProxyResponse

    If the Lambda succeeds, the API Gateway will return the values from APIGatewayProxyResponse.

    But if the Lambda don't succeeds, then you will get an internal server error.

    If you return an error other then nil, then the Lambda failed.

    When there is a panic() or os.Exit() then the Lambda also failed. This means a log.Fatal also fails a Lambda.

    Here is more information about errors:
    https://docs.aws.amazon.com/lambda/latest/dg/go-programming-model-errors.html

    Lambda logs a panic (and other output) in CloudWatch

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题