Back to articles
The Secret Life of Go: Error Handling

The Secret Life of Go: Error Handling

via Dev.toAaron Rose

Sentinel Errors, Wrapping, and The String Trap 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 30 Ethan was reviewing an HTTP handler he had just written. He wanted to return a 404 Not Found if a database query failed, and a 500 Internal Server Error for anything else. "How does this look?" he asked, pointing to his screen. user , err := db . GetUser ( id ) if err != nil { // If the error message contains the words "not found", it's a 404 if strings . Contains ( err . Error (), "not found" ) { return respondWithError ( w , 404 , "User not found" ) } // Otherwise, it's a real server error return respondWithError ( w , 500 , "Internal server error" ) } Eleanor leaned in, her eyes scanning the strings.Contains line. "That," she said softly, "is a ticking time bomb." "Why?" Ethan asked. "It works perfectly in my tests." "It works

Continue reading on Dev.to

Opens in a new tab

Read Full Article
2 views

Related Articles