Skip to content

Add typed errors and gRPC status code conversion#227

Open
Vikramarjuna wants to merge 3 commits into
Cloud-Foundations:masterfrom
Vikramarjuna:grpc-errors
Open

Add typed errors and gRPC status code conversion#227
Vikramarjuna wants to merge 3 commits into
Cloud-Foundations:masterfrom
Vikramarjuna:grpc-errors

Conversation

@Vikramarjuna

Copy link
Copy Markdown
Contributor

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:

  • Programmatic error handling (e.g., errors.As(err, &NotFoundError{}))
  • Automatic gRPC status code mapping without string parsing
  • Clearer error messages with structured context (resource, ID, reason)

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 implementing CodedError interface (NotFoundError, PermissionDeniedError, InvalidArgumentError, etc.)
  • lib/srpc/errors.go: ErrorToStatus() function for converting errors to gRPC status codes

@rgooch rgooch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please resolve merge conflicts.

@Vikramarjuna

Copy link
Copy Markdown
Contributor Author

Please resolve merge conflicts.

addressed.

Comment thread lib/errors/types.go Outdated

import "google.golang.org/grpc/codes"

type NotFoundError struct {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

Comment thread lib/errors/impl.go
@@ -0,0 +1,79 @@
package errors

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not call this file impl.go, following convention?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed the file.

Comment thread lib/grpc/errors.go
@@ -0,0 +1,79 @@
package grpc

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not call this file impl.go, following convention?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants