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

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ