Handling errors
Up until now, we have not discussed potential errors that could appear within or outside of the business logic. This is obviously not great for a production-ready API, so we are going to see how to solve them. In this section, we are going to concentrate our efforts on the RPC endpoint called AddTask
.
Before starting to code, we need to understand how errors work in gRPC, but this should not be hard because they are pretty similar to what we are used to in REST APIs.
Errors are returned with the help of a wrapper struct called Status
. This struct can be built in multiple ways but the ones we are interested in this section are the following:
func Error(c codes.Code, msg string) error func Errorf(c codes.Code, format string, a ...interface{}) error
They both take a message for the error and an error code. Let us focus on the codes since the messages are just strings describing the error. The status codes are predefined codes that are consistent across the...