Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Response Templates not Uploading to AWS (Bug?) #2170

Closed
eecdc opened this issue Sep 20, 2016 · 6 comments
Closed

Custom Response Templates not Uploading to AWS (Bug?) #2170

eecdc opened this issue Sep 20, 2016 · 6 comments

Comments

@eecdc
Copy link

eecdc commented Sep 20, 2016

This is a Bug Report

Description

I am trying to create a custom response template that gets uploaded in to AWS as integration responses. Currently I want the response to expose my error object as a content-type of application/json. What I've created doesn't seem to get uploaded into AWS, however writing the template directly in to AWS works.

config:

functions:
  signin:
    handler: credentials/authentication/signin/handler.signin
    package:
      include:
        - lib\auth
    events:
      - http:
          path: authentication/signin/{provider}
          method: post
          private: true
          response:
            headers:
              Content-Type: "'application/json'"
            template: ${file(response-template.txt)}

response-template.txt contains the following:

#set ($errorMessageObj = $util.parseJson($input.path('$.errorMessage')))
{
   "status" : "$errorMessageObj.status",
    "error" : {
    "type": "$errorMessageObj.error.type",
    "message": "$errorMessageObj.error.message",
    "detail": "$errorMessageObj.error.detail",
    "code": $errorMessageObj.error.code
    }
}

After a successful sls deploy and making the post request that fails I get the following which is my error object as a string

{
    "errorMessage": "{\"status\":\"[500]\",\"error\":{\"type\":\"API\",\"message\":\"Unexpected error has occurred.\",\"detail\":null,\"code\":6}}"
}

If I paste the template directly in to AWS I get the response formatted as I'd expect. It would appear, unless I am doing something wrong that the template is not being uploading into AWS.

@eecdc
Copy link
Author

eecdc commented Sep 20, 2016

By way of an update I have found that my template is applied to the default mapping.

Is there a way to apply it to the following?

.[400].
.[401].
.[403].
.[404].
.[422].
.(Process\s?exited\s?before\s?completing\s?request|[500]).
.[502].
.[504].

@rowanu
Copy link
Member

rowanu commented Sep 20, 2016

Can you please put your snippets in code blocks (i.e. surrounded by three back ticks "```") so that we can see your exact configuration - whitespace is significant in YAML, and might be why you're having problems.

For more info, see the Github markdown page.

@eecdc
Copy link
Author

eecdc commented Sep 20, 2016

Sure thing, that's done.

@flomotlik
Copy link
Contributor

@chaitanya11
Copy link

Hi,
I am throwing runtime exception in java as below

public class LambdaInfoHandler implements RequestHandler<String, String> {
 @Override
   public String handleRequest(String input, Context context) {

       Map<String, Object> errorPayload = new HashMap();
       errorPayload.put("errorType", "BadRequest");
       errorPayload.put("httpStatus", 400);
       errorPayload.put("requestId", context.getAwsRequestId());
       errorPayload.put("message", "An unknown error has occurred. Please try again.");
       String message = new ObjectMapper().writeValueAsString(errorPayload);
       
       throw new RuntimeException(message);
   }
}

and my serverless configuration for this lambda function is

lambdaInfo:
    handler: LambdaInfoHandler
    events:
      - http:
          path: lambda
          method: post
          response:
            headers:
              Content-Type: "'application/json'"
            template: ${file(response-template.txt)}

This is my response-template.txt file content

#set ($errorMessageObj = $util.parseJson($input.path('$.errorMessage')))
{
  "type" : "$errorMessageObj.errorType",
  "message" : "$errorMessageObj.message",
  "httpStatus" : "$errorMessageObj.httpStatus"
}

So, when i hitting lambda function i am getting
502 error with body as

{
    "message": "Internal server error"
}

So, the mapping template is not working as expected ...
can any one suggest the actual way of implementing it if i am wrong...
I followed this this.

@HyperBrain
Copy link
Member

HyperBrain commented Nov 13, 2017

Hi @chaitanya11 ,

the default integration type for lambda endpoints in Serverless is Lambda-Proxy. You are about to use the lambda integration type that uses templates, so you have to specify that in your serverless.yml:

...
events:
      - http:
          integration: lambda    <--- HERE!
          path: lambda
          method: post
          ...

For some info see here: https://serverless.com/framework/docs/providers/aws/events/apigateway/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants