grpcerrors

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2017 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/creasty/apperrors"
	"github.com/grpc-ecosystem/go-grpc-middleware"
	"github.com/izumin5210/grpc-errors"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
)

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

var grpcCodeByYourCode = map[int]codes.Code{
	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 `*apperror.Error`.
			// A handler function should wrap received error with `*apperror.Error`.
			return apperrors.WithStatusCode(err, CodeNotWrapped)
		}),
		grpcerrors.WithReportableErrorHandler(func(c context.Context, err *apperrors.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.WithStatusCodeMap(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(funcs ...ErrorHandlerFunc) grpc.StreamServerInterceptor

StreamServerInterceptor returns a new streaming server interceptor to handle errors

func UnaryServerInterceptor

func UnaryServerInterceptor(funcs ...ErrorHandlerFunc) grpc.UnaryServerInterceptor

UnaryServerInterceptor returns a new unary server interceptor to handle errors

Types

type AppErrorHandlerFunc

type AppErrorHandlerFunc func(context.Context, *apperrors.Error) error

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

type ErrorHandlerFunc

type ErrorHandlerFunc func(context.Context, error) error

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

func WithAppErrorHandler added in v0.2.0

func WithAppErrorHandler(f AppErrorHandlerFunc) ErrorHandlerFunc

WithAppErrorHandler returns a new error handler function for handling errors wrapped with apperrors.

func WithNotWrappedErrorHandler

func WithNotWrappedErrorHandler(f ErrorHandlerFunc) ErrorHandlerFunc

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

func WithReportableErrorHandler

func WithReportableErrorHandler(f AppErrorHandlerFunc) ErrorHandlerFunc

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

func WithStatusCodeMap added in v0.2.0

func WithStatusCodeMap(m map[int]codes.Code) ErrorHandlerFunc

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

func WithStatusCodeMapper

func WithStatusCodeMapper(mapFn func(code int) codes.Code) ErrorHandlerFunc

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

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