connectgrpcerr

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

README

grpc-connect-go-errors

Keep a Changelog Go Reference GitHub go.mod Go version GitHub License GitHub Actions Workflow Status Go Report Card Codecov

Description

This small Go package converts google.golang.org/grpc error codes to connectrpc.com/connect-go error codes.

Motivation

During the migration process from gRPC to connect-go, developers often face the challenge of translating google.golang.org/grpc errors to their connect-go equivalents. This package aims to address these issues by offering a streamlined conversion process.

Specific Use Case: Migrating from grpc-gateway to connect-go

A common scenario where this package proves particularly useful is when migrating from grpc-gateway to connect-go:

  1. In existing grpc-gateway implementations,
  2. the status.Error with appropriate codes.Code is automatically translated to the corresponding HTTP status.
  3. When wrapping a gRPC service implementation with a connect-go handler, errors returned by the gRPC service are passed to the connect-go handler. However, connect-go expects explicit connect.Code and connect.Error.
  4. Without proper error conversion, all errors from the gRPC service will result in HTTP 500 status codes when handled by connect-go.

This package solves this issue by allowing developers to wrap errors returned by the gRPC service before passing them to the connect-go handler, ensuring correct HTTP status codes are maintained during the migration process.

I've created an issue in the connect-go repo, and @jhump responded:

One big objective of Connect is 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.

https://github.com/connectrpc/connect-go/issues/763

@jhump's insightful feedback was instrumental in shaping the direction of this project. After considering his response, I decided to address this specific need by implementing this small Go package. It provides a focused solution for converting gRPC error codes to connect-go error codes without introducing heavy dependencies, aligning with connect-go's philosophy of lightweight and simple libraries.

Usage

package controller

import (
	"context"
	"github.com/franchb/grpc-connect-go-errors"
)

// ...

func (ps *PingServer) Ping(
        ctx context.Context,
        req *connect.Request[pingv1.PingRequest],
) (*connect.Response[pingv1.PingResponse], error) {
	response, err := ps.grpccontroller.Ping(ctx, ...)
	if err != nil {
		return nil, connectgrpcerr.FromGRPCError(err)
	}

	// ...other code here
}

Status: Alpha

This package is in its alpha stage and should be considered unstable.

License

Offered under the Apache 2 license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromGRPCError

func FromGRPCError(err error) error

FromGRPCError maps the gRPC error to the corresponding connect-go error code. If the error is nil, it returns nil. If the error cannot be converted to a gRPC error, it returns a connect-go internal error. Otherwise, it returns the connect-go error code based on the gRPC status code.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL