Add typed errors and gRPC status code conversion#227
Conversation
rgooch
left a comment
There was a problem hiding this comment.
Please resolve merge conflicts.
addressed. |
|
|
||
| import "google.golang.org/grpc/codes" | ||
|
|
||
| type NotFoundError struct { |
There was a problem hiding this comment.
The general convention in this repository is to put all public types, functions and methods for a package in an api.go file, with implementation in separate files. This makes it easier to quickly see the API for package when browsing the source.
| @@ -0,0 +1,79 @@ | |||
| package errors | |||
There was a problem hiding this comment.
Why not call this file impl.go, following convention?
There was a problem hiding this comment.
renamed the file.
| @@ -0,0 +1,79 @@ | |||
| package grpc | |||
There was a problem hiding this comment.
Why not call this file impl.go, following convention?
There was a problem hiding this comment.
Will have more files which will have the grpc server implementation. Would like to keep this file as is so that it has all the logic related to errors.
Add Typed Errors and gRPC Status Code Conversion
Why
Introduce typed errors so that errors returned with proper types can be handled better by callers. This enables:
errors.As(err, &NotFoundError{}))Existing code can continue using string-based errors -
ErrorToStatus()includes pattern matching as a fallback. New code should gradually adopt typed errors.What
lib/errors/types.go: Typed errors implementingCodedErrorinterface (NotFoundError, PermissionDeniedError, InvalidArgumentError, etc.)lib/srpc/errors.go:ErrorToStatus()function for converting errors to gRPC status codes