Unable to map AWS AppSync Custom Error from API Gateway
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?
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
Relevant questions
I am having an issue in API Gateway that says " No integration defined for method" when I attempt to deploy
asked a month agocan we attach the custom domain to lambda function urls ?
Accepted Answerasked 23 days agoAPI Gateway API Mappings deployed through CDK do not work until modified via console
asked a month agoAPI Gateway 524 Response
asked 3 days agoUnable to map AWS AppSync Custom Error from API Gateway
asked a month agoI'm getting {"message":"Forbidden"} when trying to access my lambda from custom domain in api Gateway
asked 5 months agoIs it possible to modify the statusCode returned by API Gateway when access is denied by a lambda authorizer?
Accepted Answerasked 4 years agoAPI Gateway Mapping Template missing toJson
asked 2 months agoAWS Api gateway for REST API does not work with multi-level base path mapping
asked 2 months agoAPI Gateway - Gateway response - HTTP API
Accepted Answerasked a year ago