errors.go78 lines, 2.8 KB
| 1 | // Package grpcx is the gRPC plumbing every service shares: interceptors, and |
| 2 | // one consistent way to turn our errors into status codes. It's httpx, for |
| 3 | // gRPC. |
| 4 | package grpcx |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "maps" |
| 10 | "slices" |
| 11 | |
| 12 | "github.com/fiston-user/payments/internal/platform/apperr" |
| 13 | "github.com/fiston-user/payments/internal/platform/idempotency" |
| 14 | "google.golang.org/genproto/googleapis/rpc/errdetails" |
| 15 | "google.golang.org/grpc/codes" |
| 16 | "google.golang.org/grpc/status" |
| 17 | "google.golang.org/protobuf/protoadapt" |
| 18 | ) |
| 19 | |
| 20 | // Domain is the ErrorInfo domain of our errors: with the reason, like |
| 21 | // insufficient_funds, it names an error that clients can branch on. |
| 22 | const Domain = "payments" |
| 23 | |
| 24 | // ToStatus maps an error to a gRPC status. It's the only place that knows how |
| 25 | // our errors become gRPC, like classify does for HTTP. An error that already |
| 26 | // is a status passes through unchanged. |
| 27 | func ToStatus(err error) *status.Status { |
| 28 | if s, ok := status.FromError(err); ok { |
| 29 | return s |
| 30 | } |
| 31 | var valErr *apperr.ValidationError |
| 32 | var ruleErr *apperr.RuleError |
| 33 | switch { |
| 34 | case errors.As(err, &valErr): |
| 35 | return withDetails(codes.InvalidArgument, err.Error(), fieldViolations(valErr)) |
| 36 | case errors.As(err, &ruleErr): |
| 37 | return withDetails(codes.FailedPrecondition, err.Error(), |
| 38 | &errdetails.ErrorInfo{Reason: ruleErr.Code, Domain: Domain}) |
| 39 | case errors.Is(err, apperr.ErrNotFound): |
| 40 | return status.New(codes.NotFound, err.Error()) |
| 41 | case errors.Is(err, apperr.ErrConflict): |
| 42 | return status.New(codes.AlreadyExists, err.Error()) |
| 43 | case errors.Is(err, apperr.ErrUnauthenticated): |
| 44 | return status.New(codes.Unauthenticated, err.Error()) |
| 45 | case errors.Is(err, apperr.ErrForbidden): |
| 46 | return status.New(codes.PermissionDenied, err.Error()) |
| 47 | case errors.Is(err, idempotency.ErrKeyReused): |
| 48 | return status.New(codes.FailedPrecondition, err.Error()) |
| 49 | case errors.Is(err, idempotency.ErrInProgress): |
| 50 | return status.New(codes.Aborted, err.Error()) |
| 51 | case errors.Is(err, context.DeadlineExceeded), errors.Is(err, context.Canceled): |
| 52 | return status.FromContextError(err) |
| 53 | default: |
| 54 | return status.New(codes.Internal, "internal error") |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // withDetails returns a status with machine-readable details attached. |
| 59 | func withDetails(code codes.Code, msg string, details ...protoadapt.MessageV1) *status.Status { |
| 60 | s, err := status.New(code, msg).WithDetails(details...) |
| 61 | if err != nil { |
| 62 | return status.New(code, msg) // only for code OK, which we never use here |
| 63 | } |
| 64 | return s |
| 65 | } |
| 66 | |
| 67 | // fieldViolations lists a validation error's fields, the way gRPC clients |
| 68 | // expect them: a BadRequest, in the error details. |
| 69 | func fieldViolations(v *apperr.ValidationError) *errdetails.BadRequest { |
| 70 | br := &errdetails.BadRequest{} |
| 71 | for _, field := range slices.Sorted(maps.Keys(v.Fields)) { |
| 72 | br.FieldViolations = append(br.FieldViolations, &errdetails.BadRequest_FieldViolation{ |
| 73 | Field: field, |
| 74 | Description: v.Fields[field], |
| 75 | }) |
| 76 | } |
| 77 | return br |
| 78 | } |