The Secret Life of Go: Error Handling (Part 2)
Data-Rich Errors, Custom Structs, and errors.As Eleanor is a senior software engineer. Ethan is her junior colleague. They work in a beautiful beaux arts library in Lower Manhattan — the kind of pl...

Source: DEV Community
Data-Rich Errors, Custom Structs, and errors.As Eleanor is a senior software engineer. Ethan is her junior colleague. They work in a beautiful beaux arts library in Lower Manhattan — the kind of place where coding languages are discussed like poetry. Episode 31 Ethan was building a user registration endpoint. He had learned his lesson from the previous day and was dutifully avoiding string matching. "I have a problem with Sentinel errors," Ethan said, turning his monitor toward Eleanor. "They are great for simple states like ErrNotFound. But what if the error is a validation failure? I need to tell the frontend exactly which field failed and why. I can't write a Sentinel variable for every possible bad email address." He showed her his workaround: // Ethan's attempt to return data func validateUser(u User) (string, string, error) { if !strings.Contains(u.Email, "@") { return "email", "missing @ symbol", errors.New("validation failed") } return "", "", nil } Eleanor winced slightly. "Yo