amazon-web-services - 使用模式匹配的自定义错误状态代码不起作用
问题描述
在我的项目中,我使用的是无服务器。我正在尝试更改默认状态代码和响应。我尝试了以下方法。
dashboard:
handler: src/common/dashboard.dashboard
role: CommonServicesFullAccessRole
timeout: 30
events:
- http:
integration: lambda
path: ui/dashboard/
method: get
request:
parameters:
paths:
id: true
response:
headers:
Content-Type: "'text/html'"
template: $input.path('$')
statusCodes:
400:
pattern: '[\s\S]*Bad Request[\s\S]*'
template: $input.path('$.errorMessage')
headers:
Content-Type: "'text/plain'"
在我的 lambda 中,我将错误回调返回为
return callback('Bad Request');
尽管如此,我还是无法获得具有指定状态代码的响应。我不确定确切的错误在哪里。以下是我得到的回应。
请帮我解决这个问题。谢谢...
解决方案
试试下面的。
# Instead of "return callback('Bad Request');"
callback(new Error('Bad Request'));
虽然我不是Node
用户,但我已经看到这些示例代码Error
使用Node
.
对于Python
,如果在块上使用您的serverless
配置,我会对其进行测试。response
raise Exception('Bad Request') # in case of Python
- 编辑 -
我认为我serverless.yml
的与您的没有区别,因为我只是复制了您的部分。
但是,我附上了我的测试代码,希望对您有所帮助。
# serverless.yml
service: "lambda"
provider:
name: aws
runtime: nodejs6.10
region: ap-northeast-2
stage: test
package:
exclude:
- "*/**"
include:
- "handler.js"
functions:
api-test:
handler: handler.functionOne
events:
- http:
method: get
path: fire
integration: lambda
response:
headers:
Content-Type: "'text/html'"
template: $input.path('$')
statusCodes:
400:
pattern: '[\s\S]*Bad Request[\s\S]*'
template: $input.path('$.errorMessage')
headers:
Content-Type: "'text/plain'"
# handler.js
module.exports.functionOne = function(event, context, callback) {
callback(new Error('Bad Request'));
}
# 卷曲
$ curl -X GET https://xxxxxxxx.execute-api.ap-northeast-2.amazonaws.com/test/fire -v
.
.
.
< HTTP/2 400
< content-type: text/plain
< content-length: 11
< date: Mon, 15 Oct 2018 12:40:34 GMT
.
.
.
Bad Request
推荐阅读
- authentication - 使用 CloudFormation 在机器启动期间添加用户
- deployment - UWP:AppPackages 中的文件夹与包系列名称不同
- apache-kafka - 消费者“group_name”组正在永远重新平衡
- oracle - Oracle 使用拒绝无限条款
- swift - 可跨越的日期
- javascript - 使用危险的SetInnerHtml时无法在反应nextjs中显示图像
- c# - 将 C# 转换为 VB.Net 以获取 Salesforce REST 调用示例
- c# - 连接字符串 ASP.NET
- jmeter - 如何在 MQTT 请求中使用来自 JSR223 的变量
- php - laravel sql查询if和if条件