grpcerrors

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2020 License: MIT Imports: 5 Imported by: 0

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)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StreamServerInterceptor

func StreamServerInterceptor(handlers ...StreamServerErrorHandler) grpc.StreamServerInterceptor

StreamServerInterceptor returns a new streaming server interceptor to handle errors

func UnaryServerInterceptor

func UnaryServerInterceptor(handlers ...UnaryServerErrorHandler) grpc.UnaryServerInterceptor

UnaryServerInterceptor returns a new unary server interceptor to handle errors

func WithCodeMap added in v1.0.0

func WithCodeMap(m CodeMap) interface {
	UnaryServerErrorHandler
	StreamServerErrorHandler
}

WithCodeMap returns a new error handler function for mapping status codes to gRPC's one.

func WithCodeMapper added in v1.0.0

func WithCodeMapper(mapFn CodeMapFunc) interface {
	UnaryServerErrorHandler
	StreamServerErrorHandler
}

WithCodeMapper returns a new error handler function for mapping status codes to gRPC's one with given function.

func WithFailHandler added in v1.0.0

func WithFailHandler(f FailHandlerFunc) interface {
	UnaryServerErrorHandler
	StreamServerErrorHandler
}

WithFailHandler returns a new error handler function for handling errors wrapped with fail.Error.

func WithGrpcStatusUnwrapper added in v1.0.0

func WithGrpcStatusUnwrapper() interface {
	UnaryServerErrorHandler
	StreamServerErrorHandler
}

WithGrpcStatusUnwrapper returns unwrapped error if this has a gRPC status.

func WithNotWrappedErrorHandler

func WithNotWrappedErrorHandler(f ErrorHandlerFunc) interface {
	UnaryServerErrorHandler
	StreamServerErrorHandler
}

WithNotWrappedErrorHandler returns a new error handler function for handling not wrapped errors.

func WithReportableErrorHandler

func WithReportableErrorHandler(f FailHandlerFunc) interface {
	UnaryServerErrorHandler
	StreamServerErrorHandler
}

WithReportableErrorHandler returns a new error handler function for handling errors annotated with the reportability.

Types

type CodeMap added in v1.0.0

type CodeMap map[interface{}]codes.Code

CodeMap maps any status codes to gRPC's `codes.Code`s.

type CodeMapFunc added in v1.0.0

type CodeMapFunc func(code interface{}) codes.Code

CodeMapFunc returns gRPC's `codes.Code`s from given any codes.

type ErrorHandlerFunc

type ErrorHandlerFunc func(context.Context, error) error

ErrorHandlerFunc is a function that called by interceptors when specified erorrs are detected.

type FailHandlerFunc added in v1.0.0

type FailHandlerFunc func(context.Context, *fail.Error) error

FailHandlerFunc is a function that called by interceptors when specified application erorrs are detected.

type StreamServerErrorHandler added in v1.0.0

type StreamServerErrorHandler interface {
	HandleStreamServerError(context.Context, interface{}, interface{}, *grpc.StreamServerInfo, error) error
}

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

func WithStreamServerFailHandler added in v1.0.0

func WithStreamServerFailHandler(f StreamServerFailHandlerFunc) StreamServerErrorHandler

WithStreamServerFailHandler returns a new error handler for stream servers for handling errors wrapped with fail.Error.

func WithStreamServerReportableErrorHandler added in v1.0.0

func WithStreamServerReportableErrorHandler(f StreamServerFailHandlerFunc) StreamServerErrorHandler

WithStreamServerReportableErrorHandler returns a new error handler for stream servers for handling errors annotated with the reportability.

type StreamServerFailHandlerFunc added in v1.0.0

type StreamServerFailHandlerFunc func(context.Context, interface{}, interface{}, *grpc.StreamServerInfo, *fail.Error) error

StreamServerFailHandlerFunc is a function that called by stream server interceptors when specified application erorrs are detected.

type UnaryServerErrorHandler added in v1.0.0

type UnaryServerErrorHandler interface {
	HandleUnaryServerError(context.Context, interface{}, *grpc.UnaryServerInfo, error) error
}

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

func WithUnaryServerFailHandler added in v1.0.0

func WithUnaryServerFailHandler(f UnaryServerFailHandlerFunc) UnaryServerErrorHandler

WithUnaryServerFailHandler returns a new error handler for unary servers for handling errors wrapped with fail.Error.

func WithUnaryServerReportableErrorHandler added in v1.0.0

func WithUnaryServerReportableErrorHandler(f UnaryServerFailHandlerFunc) UnaryServerErrorHandler

WithUnaryServerReportableErrorHandler returns a new error handler for unary servers for handling errors annotated with the reportability.

type UnaryServerFailHandlerFunc added in v1.0.0

type UnaryServerFailHandlerFunc func(context.Context, interface{}, *grpc.UnaryServerInfo, *fail.Error) error

UnaryServerFailHandlerFunc is a function that called by unary server interceptors when specified application erorrs are detected.

Directories

Path Synopsis
Package errorstesting is a generated protocol buffer package.
Package errorstesting is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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