connectrpc / connect-go

The Go implementation of Connect: Protobuf RPC that works.
https://connectrpc.com
Apache License 2.0
2.8k stars 94 forks source link

Translate errors grom google.golang.org/error to connect-go/error #763

Closed franchb closed 6 days ago

franchb commented 3 weeks ago

Feature Request: Error Translation from gRPC to connect-go

Is your feature request related to a problem? Please describe.

I'm frequently encountering situations where I need to translate google.golang.org/grpc errors to connect-go errors. Currently, there's no straightforward way to do this, which leads to additional development time and potential inconsistencies in error handling across projects that are migrating from gRPC to connect-go.

Describe the solution you'd like

I propose adding a utility function or package within connect-go that can convert gRPC errors to their connect-go equivalents. This could be implemented as:

  1. A new function in the existing connect package:
func FromGRPCError(err error) error
  1. Or a new subpackage, e.g., connect/grpccompat:
package grpccompat

func FromGRPCError(err error) error   

This function would take a gRPC error as input and return the corresponding connect-go error, maintaining the original error's status code, message, and any additional details.

Describe alternatives you've considered

  1. Creating a separate, third-party package for this conversion. However, this approach may lead to fragmentation and inconsistency across projects using connect-go.

  2. Manually mapping errors in each project that needs this functionality. This is currently the default approach but is time-consuming and error-prone.

  3. Extending the existing connect.CodeOf function to handle gRPC errors directly. While this could work, it might clutter the primary API and could be confusing for users who don't need gRPC compatibility.

The proposed solution of adding a dedicated conversion function or package within connect-go seems to be the most clean and maintainable approach.

Additional context

This feature would greatly benefit projects that are in the process of migrating from gRPC to connect-go, as well as those that need to interact with both gRPC and connect-go services. It would ensure consistent error handling and reduce the cognitive load on developers working with both ecosystems.

jhump commented 3 weeks ago

@franchb, this would require adding a dependency from connect-go to the grpc-go module. That would then pull in lots of gRPC-related packages into every program that uses connect-go.

While one big objective of Connect is to providing support for HTTP 1.1 for web and mobile RPC clients, another is to provide libraries that are lightweight & simple and that use standard (or widely used) libraries and idioms for the target language. So pulling the behemoth that is grpc-go into connect-go's dependency graph is a non-starter.

The recommended way to do this is, in your own codebase (where I assume you are migrating a gRPC codebase to Connect and still have handlers and helpers that produce the gRPC error type), use an interceptor that examines all errors and translates them to Connect errors whenever it sees a gRPC error. This translation should be very straight-forward to do with the existing APIs.