Return custom error message
See original GitHub issueHi there!
I’d like to know the better way to raise an exception on a Mutation. For example, while trying to create a User with an email address that already exists.
if email
user = User.objects.filter(email=email).first()
if user:
# ?
return CreateUser(username=username, password=password)
I know that I can raise Exception('message')
, but is this the best way? Can we do it differently?
Thanks!
Issue Analytics
- State:
- Created 6 years ago
- Reactions:7
- Comments:14 (2 by maintainers)
Top Results From Across the Web
Custom Error Messages in Spring REST API - amitph
The most basic way of returning an error message from a REST API is to use the @ResponseStatus annotation. We can add the...
Read more >Custom Error Message Handling for REST API - Baeldung
Custom Error Message Handling for REST API · 1. Overview · 2. A Custom Error Message · 3. Handle Bad Request Exceptions ·...
Read more >Get Started with Custom Error Handling in Spring Boot (Java)
First, you have to think about all the HTTP error status codes you want your application to return. Then, you have to define...
Read more >Return custom error message in ASP.NET Core Web Api
I am writing a webapi in ASP.NET CORE 1.1 . I am trying to figure out how to return error message with not...
Read more >Return a custom error message in SQL Server - Tech Funda
To return custom error message in case of error, we pass necessary parameter to the THROW statement. --- SELECT 'ITFunda'/0 BEGIN TRY SELECT...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I usually return a GraphQLError.
It takes a few more arguments:
nodes
,stack
,source
, andpositions
, but I have never used these.I’m going to close this issue since it’s stale. However just to add: I’ve found that modeling expected errors as part of your mutation response is essential. Your client needs to know what to do if your mutation fails. To do that I’ve found that modeling your response types as unions works really well since it allows you explicitly model the errors you’re expecting. So in a
createUser
mutation similar to yours @jonatasbaldin you can do this:Then in your client your mutation query becomes:
Then you just need to check the
__typename
value to figure out if there was an error (and what kind) or if the mutation succeed.