|
|
|
|
|
by klodolph
867 days ago
|
|
I mean, you may end up just wanting something like, type UsernameError struct {
name string
reason string
}
func (e *UsernameError) Error() string {
return fmt.Errorf("invalid username %q: %s", e.name, e.reason)
}
And reason can be "username cannot be empty" or "username may not contain '<'" or something like that.This is fine for lots of different cases, because it’s likely that your code wants to know how to handle “username is invalid”, but only humans care about why. I have personally never seen a Go codebase where you parse error strings. I know that people keep complaining about it so it must be happening out there—but every codebase I’ve worked with either has error constants (an exported var set to some errors.New() value) or some kind of custom error type you can check. Or if it doesn’t have those things, I had no interest in parsing the errors. |
|