在我的项目中,我使用的是无服务器。我正在尝试更改默认的statusCodes和响应。我已经尝试了以下几种方法。
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');尽管如此,我仍然无法获得具有指定statusCode的响应。我不确定确切的错误在哪里。以下是我得到的回复。

请帮我解决这个问题。谢谢..。
发布于 2018-10-13 17:00:02
试试下面的方法。
# Instead of "return callback('Bad Request');"
callback(new Error('Bad Request')); 虽然我不是Node用户,但我在Node中见过使用Error对象的示例代码。
对于Python,如果在response块上使用serverless配置,我会对其进行测试。
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
$ 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 Requesthttps://stackoverflow.com/questions/52780026
复制相似问题