Unable to map AWS AppSync Custom Error from API Gateway

0

I am using API Gateway and AppSync for the development purpose.

In API Gateway we are mapping most of the http status code and when we execute those apis from postman we get the correct response with statuscode (for errors also).

API Gateway mapping template for error response be like this

#set ($errorMessageObj = $input.path('$')) { "type" : "$errorMessageObj.errorType", "message" : "$errorMessageObj.details.message", "errorMessage" : "$errorMessageObj.errorMessage", "statusCode" : "$errorMessageObj.statusCode", "errorCode" : "$errorMessageObj.errorCode", "trace" : "$errorMessageObj.details.stacktrace" }

We get the 200 response for all the success request. But we are unable to map error response of API in Grapql Response Mapping Template .Graphql always throw 500 statuscode with "Template transformation yielded an empty response" message and errortype of 'MappingTemplate'.

Graphql Response Mapping template be like this

#if($ctx.error) $util.error($ctx.error.message, $ctx.error.type) #end

#if($ctx.result.statusCode == 200) #set($body = $util.parseJson($ctx.result.body))
$util.toJson($body) #else $utils.appendError($ctx.result.body, $ctx.result.statusCode) #end

How can I map the API Gateway custom error response in AppSync?

1 Answer
0

Hi!

From the docs:

$util.appendError(String).
Appends a custom error. This can be used in request or response mapping templates if the template detects an error with the request or with the invocation result. Unlike $util.error(String), the template evaluation will not be interrupted, so that data can be returned to the caller.

In your case the else statement isn't actually returning a response, i think your response mapping template should use $utils.error helper instead:

#if($ctx.error) $util.error($ctx.error.message, $ctx.error.type) #end

#if($ctx.result.statusCode == 200) #set($body = $util.parseJson($ctx.result.body))
$util.toJson($body) #else $utils.error($ctx.result.body, $ctx.result.statusCode) #end
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions