Replies: 1 comment 1 reply
-
Hi, the error format is specified in the GraphQL spec with a mandatory error
If you want to work with an If you need to act differently depending on the actual error, then you need to check how your server responds in different error conditions and build your error handling specifically for your use case and your server. If you control the server it might be possible to extend the errors with custom content, so you can "recreate" an exception which has ocurred on the server from the contents of a |
Beta Was this translation helpful? Give feedback.
-
Hello,
What is the expected way to raise my own errors?
Consider I have a GraphQL query that returns
Person
. My C# resolver is defined like this:public static async Task<Person> GetPerson(int personId)
If I want to raise an error, let's say personId is not within a range, I could
throw new Exception("personId not in expected range")
, however the GraphQL response.Errors
(of typeGraphQLError[]
) mix a simple custom error message with a lot of technical stuff (locations, path, extensions, etc.):{"errors":[{"message":"Error trying to resolve field \u0027getPerson\u0027.","locations":[{"line":3,"column":9}],"path":["getPerson"],"extensions":{"code":"","codes":[""],"details":"GraphQL.Execution.UnhandledError: Error trying to resolve field \u0027getPerson\u0027.\r\n ---\u003E System.Exception: personId not in expected range...
I also tried to throw new GraphQLError but the constructor is empty (would still have same issue as above anyway).
I'm thinking that the GraphQLError is not really suitable for handled/custom errors. Am I right?
What I know that worked well is to wrap Person with a custom class like
ResultCall
(Task<ResultCall<Person>>
) where I can have my own error property (along with the data, of course). Is this an acceptable solution to send my own error messages, or should I always use the.Errors
(GraphQLError[]
) property?Having a wrapper means the client have first to check GraphQL's
.Errors
, then check my ownResultCall
.Errors
.If I should use the GraphQL
.Errors
, then what is the recommended approach as throwing anException
does not seem appropriate.Thank you.
Beta Was this translation helpful? Give feedback.
All reactions