0

I am using Appsync for graphql server. and use `$util.error(String, String, Object, Object)

in the response mapping template to response error to clients. However, the error messages clients get looks like below json. There are too many extra information there. What clients really care about is the messageI made this error. How can I response a simple json object like {"errorMessage": "I made this error"}` to clients?

{
  "data": {
    "getError": null
  },
  "errors": [
    {
      "path": [
        "getError"
      ],
      "data": null,
      "errorType": "ALWAYS_ERROR",
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "I made this error"
    }
  ]
}
Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523

1 Answers1

0

This is the standard error format for GraphQl per specs - describing code, message and where the error occurred. The primary reason you see this complex format is to due to the nature of GraphQL which can return very deeply nested data. It would be extremely hard to deduce where the issue was with a simple message. In something like REST where the response is standardized its easier to provide that kind of message but in qgl, since the user is building essentially a unique query then we have to be much more thorough with the error response.

That being said, you can, in your client, simplify it by grabbing the first error:

if(data.errors) { 
  console.error(data.errors[0].errorType) 
}
cyberwombat
  • 38,105
  • 35
  • 175
  • 251