I am trying to parse mySQL errors in order to return clean error message to the user from my golang API. I read some article like this one that show what I would like to do but it looks like the module go-mysql-driver that I am using don't support parseError.
To give a concrete example of what I try to achieve, with the error:
Error 1062: Duplicate entry 'John' for key 'name_UNIQUE'
I would like to be able to build a data structure that allow me to organize the information in order to return a user friendly message like
Error with the field 'name': 'John' already exist"
so I can also translate it in different language and directly print it client side.
Thank you!
I found some hints in packets.go and driver_test.go
Example:
me, ok := err.(*mysql.MySQLError)
if !ok {
return err
}
if me.Number == 1062 {
return errors.New("It already exists in a database.")
}
return err
Possible values of me.Number
can be found in mysql documentation
That error comes directly from the server, pretty much you would have to parse the error string (err.Error()
) yourself.