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 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南