I'm using Hot chocolate in ASP.NET Core. If userExist != null is true, I return the GraphQLException to the client to process and display to the user (I'm using Angular and apollo client). But I can't catch the return error and get internal server error in the console screen.
Below is my mutation.
public async Task<UserType?> CreateUser([UseFluentValidation, UseValidator<RegisterInputTypeValidator>()]RegisterInputType model, [Service]IUserRepository userRepository)
{
var userExist = await userRepository.GetUserByEmail(model.Email);
if (userExist != null)
{
throw new GraphQLException(new List<Error>()
{
new Error("Email already exists"),
});
}
AppUser user = new AppUser
{
Email = model.Email,
FullName = model.FullName,
UserName = model.Email,
AvatarUrl = model.AvatarUrl,
PhoneNumber = ""
};
var r = await userRepository.CreateUserAsync(user, model.Password);
if (r.Succeeded == false)
{
var err = r.Errors.Select(e => new Error(e.Description)).ToList();
throw new GraphQLException(err);
}
return new UserType
{
Email = user.Email,
FullName = user.FullName,
AvatarUrl = user.AvatarUrl,
PhoneNumber = user.PhoneNumber!,
FriendIds = user.FriendIds,
GroupIds = user.GroupIds
};
}
This is my Angular code:
this.apollo
.mutate({
mutation: CREATE_USER,
variables: {
email: this.registerForm.controls['email'].value,
fullName: this.registerForm.controls['fullName'].value,
password: this.registerForm.controls['password'].value,
reEnterPassword: this.registerForm.controls['reEnterPassword'].value,
avatarUrl: '',
},
})
.subscribe({
next: (result) => {
console.log(result);
},
error: (err) => {
// not working
console.log(err.message);
}
});
I tried everything and nothing worked. How to return the errors to the client?