Handle controller error with GraphQL

System Information
  • Strapi Version: 3.6.5
  • Operating System:
  • Database:
  • Node Version:
  • NPM Version:
  • Yarn Version:

Hi everyone :slight_smile:

I am currently working on a side project in order to try the great cms Strapi.

I have a question concerning error handling in Strapi with graphql. I was playing with the graphql playground and tried some queries/mutations. There is one thing I can’t figure out.

Let’s say I want to register a new user. I use the “register” mutation


It works perfectly.

Now if I omit the password I got this response


which is nice, because I can handle this message on my front-end app. I checked on the strapi’s github to see how it is managed strapi/Auth.js at master · strapi/strapi · GitHub. We can see in the controller the action “register” which returns a BadRequest if the password is empty :
Capture d’écran 2021-09-08 à 12.36.56
That’s perfect.

Next, I tried the “updateUser” mutation which works pretty well


However, if I let the username empty, I got this error

I checked the “update” action strapi/api.js at master · strapi/strapi · GitHub and we can see a badrequest is returned if the username is empty
Capture d’écran 2021-09-08 à 12.45.07

I was wondering why I don’t get this error message in the graphql response whereas for the register action I can handle it ? Instead I got “Cannot return null for non-nullable field UsersPermissionsUser.id.” (which I understand, if no user is returned, I can’t access user ID)

If someone can help me :slight_smile:
Thanks !

1 Like