Should you implement your own error structs or use fmt.Errorf?

Posted on

I’m working on a personal project to learn more Go – it’s an HTTP backend with CRUD endpoints. I’m in the process of writing unit tests and want to write some test cases for server-side errors (5XX errors).

I’ve noticed in my services at this point, I am simply returning errors with fmt.Errorf. These errors are then encoded on the response as a string with http.Error. I’m thinking this isn’t the right approach, since I’ll want to return more client-friendly errors in my handler (and thereby mask any implementation details, the response shouldn’t give away that a Postgres error occurred or that the AWS service I’m using had a 500) as well as assert the right errors were returned in my unit tests. Should I be implementing my own error structs and checking for them in my unit tests with errors.As? What’s the right way to approach this?