我希望有不同的HTTP状态码,但是看起来API网关总是返回200状态码,即使lambda函数返回错误。
更新于2016年9月20日
通过使用Lambda Proxy集成,亚马逊终于让这变得更加容易。这使得您的Lambda函数能够返回正确的HTTP代码和标头:
let response = {
statusCode: '400',
body: JSON.stringify({ error: 'you messed up!' }),
headers: {
'Content-Type': 'application/json',
}
};
context.succeed(response);
告别API Gateway中的请求/响应映射!
选项2
使用aws-serverless-express将现有的Express应用程序集成到Lambda/API Gateway。
callback(null, {statusCode: 200, body: 'whatever'})
即可。 - Widdershinreturn { "isBase64Encoded": True, "statusCode": 200, "headers": { }, "body": "" }
- Jithu R Jacob这是返回自定义HTTP状态码和自定义错误消息的最快方法:
在API Gateway仪表板中,按照以下步骤操作:
为您先前创建的每个HTTP状态码添加一个集成响应。确保选中输入透传。使用lambda error regex来识别当您从lambda函数返回错误消息时应使用哪个状态码。例如:
// Return An Error Message String In Your Lambda Function
return context.fail('Bad Request: You submitted invalid input');
// Here is what a Lambda Error Regex should look like.
// Be sure to include the period and the asterisk so any text
// after your regex is mapped to that specific HTTP Status Code
Bad Request: .*
您的API网关路由应返回以下内容:
HTTP Status Code: 400
JSON Error Response:
{
errorMessage: "Bad Request: You submitted invalid input"
}
我看不到任何方法可以复制这些设置并在不同的方法中重复使用它,因此我们需要进行许多烦人的冗余手动输入!
我的整合响应看起来像这样:
return context.fail(new Error('bad one'))
。 - MonkeyBonkey200 OK
或201 CREATED
的响应。 - Ben Davisexports.handler = function(event, context) {
var response = {
status: 400,
errors: [
{
code: "123",
source: "/data/attributes/first-name",
message: "Value is too short",
detail: "First name must contain at least three characters."
},
{
code: "225",
source: "/data/attributes/password",
message: "Passwords must contain a letter, number, and punctuation character.",
detail: "The password provided is missing a punctuation character."
},
{
code: "226",
source: "/data/attributes/password",
message: "Password and password confirmation do not match."
}
]
}
context.fail(JSON.stringify(response));
};
接下来,您需要为希望返回的每个状态码设置正则表达式映射。使用我上面定义的对象,您可以为400设置这个正则表达式:
.*"status":400.*
最后,您需要设置一个映射模板,从Lambda返回的errorMessage属性中提取JSON响应。映射模板如下所示:
$input.path('$.errorMessage')
我在这篇文章中写了更详细的内容,并解释了从Lambda到API Gateway的响应流程:http://kennbrodhagen.net/2016/03/09/how-to-return-a-custom-error-object-and-status-code-from-api-gateway-with-lambda/
1) 配置API Gateway资源,通过在API Gateway资源定义的"Integration Request"屏幕上选中标有"使用Lambda代理集成"的复选框来使用Lambda代理集成。(或在您的cloudformation/terraform/serverless/etc配置文件中定义)
2) 更改您的Lambda代码两个方面:
event
(第一个函数参数)。它不再仅是裸载荷,而是表示包括标头、查询字符串和正文在内的整个HTTP请求。如下所示。关键点是JSON正文将是需要显式调用JSON.parse(event.body)
的字符串(不要忘记在其周围加上try/catch
)。示例如下。statusCode
, body
和headers
。
body
应该是一个字符串,所以根据需要执行JSON.stringify(payload)
statusCode
可以是数字headers
是一个包含标头名称和值的对象{
"resource": "/example-path",
"path": "/example-path",
"httpMethod": "POST",
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "US",
"Content-Type": "application/json",
"Host": "exampleapiid.execute-api.us-west-2.amazonaws.com",
"User-Agent": "insomnia/4.0.12",
"Via": "1.1 9438b4fa578cbce283b48cf092373802.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "oCflC0BzaPQpTF9qVddpN_-v0X57Dnu6oXTbzObgV-uU-PKP5egkFQ==",
"X-Forwarded-For": "73.217.16.234, 216.137.42.129",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"queryStringParameters": {
"bar": "BarValue",
"foo": "FooValue"
},
"pathParameters": null,
"stageVariables": null,
"requestContext": {
"accountId": "666",
"resourceId": "xyz",
"stage": "dev",
"requestId": "5944789f-ce00-11e6-b2a2-dfdbdba4a4ee",
"identity": {
"cognitoIdentityPoolId": null,
"accountId": null,
"cognitoIdentityId": null,
"caller": null,
"apiKey": null,
"sourceIp": "73.217.16.234",
"accessKey": null,
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": null,
"userAgent": "insomnia/4.0.12",
"user": null
},
"resourcePath": "/example-path",
"httpMethod": "POST",
"apiId": "exampleapiid"
},
"body": "{\n \"foo\": \"FOO\",\n \"bar\": \"BAR\",\n \"baz\": \"BAZ\"\n}\n",
"isBase64Encoded": false
}
callback(null, {
statusCode: 409,
body: JSON.stringify(bodyObject),
headers: {
'Content-Type': 'application/json'
}
})
注意事项
- 我认为像context.succeed()
这样的context
方法已经被弃用了。虽然它们似乎仍然可以工作,但它们不再有文档记录。我认为编写回调API是正确的做法。
我希望Lambda返回的错误能够是一个合适的500错误,经过多次研究,我想出了以下方法:
在Lambda中
为了得到良好的响应,我将返回结果如下:
exports.handler = (event, context, callback) => {
// ..
var someData1 = {
data: {
httpStatusCode: 200,
details: [
{
prodId: "123",
prodName: "Product 1"
},
{
"more": "213",
"moreDetails": "Product 2"
}
]
}
};
return callback(null, someData1);
}
对于一个坏的响应,返回如下
exports.handler = (event, context, callback) => {
// ..
var someError1 = {
error: {
httpStatusCode: 500,
details: [
{
code: "ProductNotFound",
message: "Product not found in Cart",
description: "Product should be present after checkout, but not found in Cart",
source: "/data/attributes/product"
},
{
code: "PasswordConfirmPasswordDoesntMatch",
message: "Password and password confirmation do not match.",
description: "Password and password confirmation must match for registration to succeed.",
source: "/data/attributes/password",
}
]
}
};
return callback(new Error(JSON.stringify(someError1)));
}
关于API网关
对于GET方法,例如获取/res1/service1:
Through Method Response > Add Response, added 3 responses:
- 200
- 300
- 400
然后,
Through 'Integration Response' > 'Add integration response', create a Regex for 400 errors (client error):
Lambda Error Regex .*"httpStatusCode":.*4.*
'Body Mapping Templates' > Add mapping template as:
Content-Type application/json
Template text box* $input.path('$.errorMessage')
Similarly, create a Regex for 500 errors (server error):
Lambda Error Regex .*"httpStatusCode":.*5.*
'Body Mapping Templates' > Add mapping template as:
Content-Type application/json
Template text box* $input.path('$.errorMessage')
现在,发布 /res1/service1 ,访问已发布的 URL,该 URL 连接到上面提到的 Lambda。
使用高级 REST 客户端(或 Postman)Chrome 插件,您将看到正确的 HTTP 状态码,例如服务器错误(500)或 400,而不是所有请求都给出“httpStatusCode”中的 200 HTTP 响应代码。
从 API Gateway 的“仪表板”中,我们可以查看以下 HTTP 状态码:
最简单的方法是使用LAMBDA_PROXY集成。使用这种方法,您不需要设置任何特殊的转换到API Gateway管道中。
您的返回对象必须类似于下面的片段:
module.exports.lambdaHandler = (event, context, done) => {
// ...
let response = {
statusCode: 200, // or any other HTTP code
headers: { // optional
"any-http-header" : "my custom header value"
},
body: JSON.stringify(payload) // data returned by the API Gateway endpoint
};
done(null, response); // always return as a success
};
对于那些尝试了所有方法但仍然无法解决这个问题的人(例如我),请查看此帖子中的thedevkit评论(拯救了我的一天):
https://forums.aws.amazon.com/thread.jspa?threadID=192918
以下是完全复制的内容:
我自己也遇到了这个问题,我认为换行符是罪魁祸首。
foo.*将匹配跟在“foo”后面的任何字符,但不包括换行符。通常通过添加'/s'标志来解决这个问题,即“foo.*/s”,但Lambda错误正则表达式似乎不尊重这一点。
作为替代方案,您可以使用类似于:foo(.|\n)*
#set($context.responseOverride.status = $input.path('$.statusCode'))
var myErrorObj = {
errorType : "InternalServerError",
httpStatus : 500,
requestId : context.awsRequestId,
message : "An unknown error has occurred. Please try again."
}
callback(JSON.stringify(myErrorObj));
s-function.json:
{
"name": "temp-err-test",
"description": "Deployed",
"runtime": "nodejs4.3",
"handler": "path/to/handler.handler",
"timeout": 6,
"memorySize": 1024,
"endpoints": [
{
"path": "test-error-handling",
"method": "GET",
"type": "AWS_PROXY",
"responses": {
"default": {
"statusCode": "200"
}
}
}
]
}
handler.js:
'use strict';
function serveRequest(event, context, cb) {
let response = {
statusCode: '400',
body: JSON.stringify({ event, context }),
headers: {
'Content-Type': 'application/json',
}
};
cb(null, response);
}
module.exports.handler = serveRequest;