Comments (11)
@B-Scan @ac360 Hi. I've had the same issue and here's the workaround I use.
In your lambda function:, use:
context.fail(JSON.stringify(error));
# Instead of:
# context.fail(error);
For your integration response's Lambda Error Regex and Method response status, use:
\{"status":4.*
=> 400\{"status":5.*
=> 500- and so on...
It works as far as the status
is the first property in the error object.
Finally, in your integration response's mapping template, use:
$input.path('$.errorMessage')
Very ugly and incomplete, but now you can pass your error object as-is from lambda's output to API gateway's HTTP response body.
from serverless.
To be able to have my own custom error responses with at least a 400 error status code I do the following:
- in case of error, from the lambda I return an expected Error object
var fault = { type: 'api', message: 'could not continue', detail: 'not-allowed', code: 4332 };
var error = new Error(JSON.stringify({ status: 'failure', fault: fault }));
context.fail(error);
- then my s-function.json I have the responses section as following:
...
"responses": {
"400": {
"selectionPattern": "(.|\\n)*\\\"status\\\"\\:\\s*\\n*\\s*\\\"failure\\\"(.|\\n)*",
"statusCode": "400",
"responseParameters": {},
"responseModels": {},
"responseTemplates": {
"application/json": "#set($inputRoot = $input.path('$.errorMessage'))\n$inputRoot"
}
},
"default": {
"statusCode": "200",
"responseParameters": {},
"responseModels": {},
"responseTemplates": {
}
}
}
...
- This means that whenever a complete match of
errorMessage
value is found against this regex:
(.|\n)*\"status\"\:\s*\n*\s*\"failure\"(.|\n)*
- Then the response should be the
$input.path('$.errorMessage')
, but the$.errorMessage
coresponds to the unserialized JSON string that was passed to theError
constructor - This should result in an error object that is returned to the client which looks like this:
{
status: 'failure',
fault: {
type: 'api',
message: 'could not continue',
detail: 'not-allowed',
code: 4332
}
}
which is awesome.
Now, the problem is that the s-function.json
configuration is not loaded at API Gateway configuration, the integration response has no 400 handling defined, how did you get that loaded by API Gateway ?
from serverless.
@B-Scan This is something I need to research as well, but have been too busy working on other components for JAWS v1.
With your mapping template approach, are you able to get API Gateway to return the same HTTP response code? I believe you must return an error string to trigger the lambda error regex and render the right status code, correct? It's been about a month since I've looked into this issue, so I'm a bit rusty.
@B-Scan If you find a good solution for this, please put it in here. 👍
from serverless.
@ac360 yes, with setting custom integration response that was triggered with lambda error regex, I was able to get the right status code.
I posted a thread on AWS forum to see is there any other way to return an object instead of a string for errors. If not, I can make some changes and submit a PR with string only version. If you are happy with that.
from serverless.
is there anyway to put the Lambda Error Regex in the s_function.json?
from serverless.
I have the same question as @ialmetwally - where is this to be placed ? what do you place in there ?
...
"responses": {
"400": {
"statusCode": "400"
},
"default": {
"statusCode": "200",
"responseParameters": {},
"responseModels": {},
"responseTemplates": {
"application/json": "IT GOES HERE"
}
}
}
....
from serverless.
@iongion I found out how to do it
"404" : {
"selectionPattern" : "pattern_here",
"statusCode": "404"
}
or
"pattern_here" : {
"statusCode": "404"
}
from serverless.
@ialmetwally I do not understand, I see no response template velocity stuff, do you have a complete s-function.json ?
from serverless.
@iongion I just put the statusCode, you can add the rest of params inside the same object
"404" : {
"selectionPattern" : "pattern_here",
"statusCode": "404"
"responseParameters": {},
"responseModels": {},
"responseTemplates": {
"application/json": "IT GOES HERE"
}
}
or
"pattern_here" : {
"statusCode": "404"
"responseParameters": {},
"responseModels": {},
"responseTemplates": {
"application/json": "IT GOES HERE"
}
}
from serverless.
@ialmetwally awesome !!!
from serverless.
My take on time is to find a way to pass context.fail
with a string value
from serverless.
Related Issues (20)
- Old API-GW stages aren't cleaned up during deployment.
- add support to provided.al2/provided.al2023 runtimes on AWS lambdas and allow arm64 architecture use on them
- DLQ for EventBridge Schedules
- CF functions for fileSystemConfig missing in Typescript
- NotificationConfiguration Error "Unable to validate the following destination configurations"
- decompress-tar vulnerability HOT 1
- Expose actual error from spawned process HOT 2
- AWSLambdaVPCAccessExecutionRole is added in VPC configured lambdas even if it was manually added
- Add support for .NET 8
- Is there any plan to upgrade aws sdk to v3?
- Deploy individual function, role arn normalisation fails on imported role ARN HOT 2
- Cannot use multiple --docker-arg options with invoke local
- Unable to Change Kafka Topic Name in Serverless Trigger
- devDependencies are not excluded all of a sudden... HOT 1
- Lambda with kafka trigger onFailure destination
- Xray Tracing for SNS and SQS in Serverless framework
- Function URLs get deleted (or won't get created) when provisioned capacity is set on Lambdas for AWS
- Serverless `support` command doesn't support multiple orgs or read the org in serverless.yml
- Compose CLI Command Schema HOT 2
- Advanced Lambda Logging - Undo Uppercase in YAML
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from serverless.