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개 답변
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
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인