Categorygithub.com/srvc/grpc-errors
modulepackage
1.2.0
Repository: https://github.com/srvc/grpc-errors.git
Documentation: pkg.go.dev

# README

grpc-errors

Build Status codecov GoDoc Go project version Go Report Card license

grpc-errors is a middleware providing better error handling to resolve errors easily.

Example

package main

import (
	"context"
	"net"

	"github.com/grpc-ecosystem/go-grpc-middleware"
	"github.com/srvc/fail"
	"github.com/srvc/grpc-errors"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
)

const (
	CodeOK uint32 = iota
	CodeInvalidArgument
	CodeNotFound
	CodeYourCustomError
	CodeNotWrapped
	CodeUnknown
)

var grpcCodeByYourCode = grpcerrors.CodeMap{
	CodeOK:              codes.OK,
	CodeInvalidArgument: codes.InvalidArgument,
	CodeNotFound:        codes.NotFound,
}

func main() {
	lis, err := net.Listen("tcp", "api.example.com:80")
	if err != nil {
		panic(err)
	}

	errorHandlers := []grpcerrors.ErrorHandlerFunc{
		grpcerrors.WithNotWrappedErrorHandler(func(c context.Context, err error) error {
			// WithNotWrappedErrorHandler handles an error not wrapped with `*fail.Error`.
			// A handler function should wrap received error with `*fail.Error`.
			return fail.Wrap(err, fail.WithCode(CodeNotWrapped))
		}),
		grpcerrors.WithReportableErrorHandler(func(c context.Context, err *fail.Error) error {
			// WithReportableErrorHandler handles an erorr annotated with the reportability.
			// You reports to an external service if necessary.
			// And you can attach request contexts to error reports.
			return err
		}),
		grpcerrors.WithCodeMap(grpcCodeByYourCode),
	}

	s := grpc.NewServer(
		grpc_middleware.WithStreamServerChain(
			grpcerrors.StreamServerInterceptor(errorHandlers...),
		),
		grpc_middleware.WithUnaryServerChain(
			grpcerrors.UnaryServerInterceptor(errorHandlers...),
		),
	)

	// Register server implementations

	s.Serve(lis)
}

# Packages

Package errorstesting is a generated protocol buffer package.

# Functions

StreamServerInterceptor returns a new streaming server interceptor to handle errors.
UnaryServerInterceptor returns a new unary server interceptor to handle errors.
WithCodeMap returns a new error handler function for mapping status codes to gRPC's one.
WithCodeMapper returns a new error handler function for mapping status codes to gRPC's one with given function.
WithFailHandler returns a new error handler function for handling errors wrapped with fail.Error.
WithGrpcStatusUnwrapper returns unwrapped error if this has a gRPC status.
WithNotWrappedErrorHandler returns a new error handler function for handling not wrapped errors.
WithReportableErrorHandler returns a new error handler function for handling errors annotated with the reportability.
WithStreamServerFailHandler returns a new error handler for stream servers for handling errors wrapped with fail.Error.
WithStreamServerReportableErrorHandler returns a new error handler for stream servers for handling errors annotated with the reportability.
WithUnaryServerFailHandler returns a new error handler for unary servers for handling errors wrapped with fail.Error.
WithUnaryServerReportableErrorHandler returns a new error handler for unary servers for handling errors annotated with the reportability.

# Interfaces

StreamServerErrorHandler is the interface that can handle errors on a gRPC stream server.
UnaryServerErrorHandler is the interface that can handle errors on a gRPC unary server.

# Type aliases

CodeMap maps any status codes to gRPC's `codes.Code`s.
CodeMapFunc returns gRPC's `codes.Code`s from given any codes.
ErrorHandlerFunc is a function that called by interceptors when specified erorrs are detected.
FailHandlerFunc is a function that called by interceptors when specified application erorrs are detected.
StreamServerFailHandlerFunc is a function that called by stream server interceptors when specified application erorrs are detected.
UnaryServerFailHandlerFunc is a function that called by unary server interceptors when specified application erorrs are detected.