I am trying to parse MySQL errors in order to return clean error messages to the user from a Go 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 doesn't support parseError.
To give a concrete example, 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 languages and send ready-to-use error messages back to the client.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With