Code Monkey home page Code Monkey logo

Comments (11)

mumoshu avatar mumoshu commented on May 23, 2024 1

@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.

iongion avatar iongion commented on May 23, 2024 1

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 the Error 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.

ac360 avatar ac360 commented on May 23, 2024

@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.

tombiscan avatar tombiscan commented on May 23, 2024

@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.

ialmetwally avatar ialmetwally commented on May 23, 2024

is there anyway to put the Lambda Error Regex in the s_function.json?

from serverless.

iongion avatar iongion commented on May 23, 2024

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.

ialmetwally avatar ialmetwally commented on May 23, 2024

@iongion I found out how to do it

"404" : {
"selectionPattern" : "pattern_here",
"statusCode": "404"
}

or

"pattern_here" : {
"statusCode": "404"
}

from serverless.

iongion avatar iongion commented on May 23, 2024

@ialmetwally I do not understand, I see no response template velocity stuff, do you have a complete s-function.json ?

from serverless.

ialmetwally avatar ialmetwally commented on May 23, 2024

@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.

iongion avatar iongion commented on May 23, 2024

@ialmetwally awesome !!!

from serverless.

olotintemitope avatar olotintemitope commented on May 23, 2024

My take on time is to find a way to pass context.fail with a string value

from serverless.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.