Lambda NodeJS 8.1 and HTTP Status <> 200

0

Trying to go serverless with apigateway but having an issue getting Lambda endpoints to return any HTTP Status Code other than 200. I can of course return JSON with status set to whatever I want, but I need to set the HTTP Status to match.

exports.handler = async (event, context, callback) => {

           return new Promise((resolve,reject) => {
                      findData().then((data) => {
                                // found data
                                resolve({
                                        status: 200,
                                        body: data
                                 });
                      },(err) => {
                               // didn't find anything
                               reject({
                                        status: 404,
                                        error: {
                                                message: 'not found'
                                       }
                                });
                       });
              });
};

I cannot figure out how to get lambda and apigateway to return HTTP Status Code 404 in the error case. I have also tried the documented

             {
                    statusCode: 404,
                    headers: {},
                    body: err
             }

But that does not work either. I have also change the error side of the promise to call resolve (instead of reject)...it of course works but the HTTP Status code is always 200 no matter what I try.

I am open to using Response Mappings, but that isn't so straight-forward and I cannot find any examples.....

Thanks for your time.

Edited by: rjsoph on Feb 10, 2019 1:59 PM

Edited by: rjsoph on Feb 10, 2019 2:01 PM

rjsoph
demandé il y a 5 ans1092 vues
1 réponse
1

Found a reference that worked......

You must not select "Lambda proxy integration" on apigateway for this to work.....

Node 8 you call resolve and reject.....then using the below reference you setup the mappings and templates....worked perfectly for me.

https://kennbrodhagen.net/2016/03/09/how-to-return-a-custom-error-object-and-status-code-from-api-gateway-with-lambda/

rjsoph
répondu il y a 5 ans

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions