11package grpcerrors
22
33import (
4- "github.com/creasty/apperrors "
4+ "github.com/srvc/fail "
55 "golang.org/x/net/context"
66 "google.golang.org/grpc"
77 "google.golang.org/grpc/codes"
@@ -21,35 +21,35 @@ type StreamServerErrorHandler interface {
2121// ErrorHandlerFunc is a function that called by interceptors when specified erorrs are detected.
2222type ErrorHandlerFunc func (context.Context , error ) error
2323
24- // AppErrorHandlerFunc is a function that called by interceptors when specified application erorrs are detected.
25- type AppErrorHandlerFunc func (context.Context , * apperrors .Error ) error
24+ // FailHandlerFunc is a function that called by interceptors when specified application erorrs are detected.
25+ type FailHandlerFunc func (context.Context , * fail .Error ) error
2626
27- type appErrorHandler struct {
28- f AppErrorHandlerFunc
27+ type failHandler struct {
28+ f FailHandlerFunc
2929}
3030
31- func (h * appErrorHandler ) HandleUnaryServerError (c context.Context , req interface {}, info * grpc.UnaryServerInfo , err error ) error {
31+ func (h * failHandler ) HandleUnaryServerError (c context.Context , req interface {}, info * grpc.UnaryServerInfo , err error ) error {
3232 return h .handleError (c , err )
3333}
3434
35- func (h * appErrorHandler ) HandleStreamServerError (c context.Context , req interface {}, resp interface {}, info * grpc.StreamServerInfo , err error ) error {
35+ func (h * failHandler ) HandleStreamServerError (c context.Context , req interface {}, resp interface {}, info * grpc.StreamServerInfo , err error ) error {
3636 return h .handleError (c , err )
3737}
3838
39- func (h * appErrorHandler ) handleError (c context.Context , err error ) error {
40- appErr := apperrors .Unwrap (err )
41- if appErr != nil {
42- return h .f (c , appErr )
39+ func (h * failHandler ) handleError (c context.Context , err error ) error {
40+ fErr := fail .Unwrap (err )
41+ if fErr != nil {
42+ return h .f (c , fErr )
4343 }
4444 return err
4545}
4646
47- // WithAppErrorHandler returns a new error handler function for handling errors wrapped with apperrors .
48- func WithAppErrorHandler (f AppErrorHandlerFunc ) interface {
47+ // WithFailHandler returns a new error handler function for handling errors wrapped with fail.Error .
48+ func WithFailHandler (f FailHandlerFunc ) interface {
4949 UnaryServerErrorHandler
5050 StreamServerErrorHandler
5151} {
52- return & appErrorHandler {f : f }
52+ return & failHandler {f : f }
5353}
5454
5555type notWrappedHandler struct {
@@ -65,8 +65,8 @@ func (h *notWrappedHandler) HandleStreamServerError(c context.Context, req inter
6565}
6666
6767func (h * notWrappedHandler ) handleError (c context.Context , err error ) error {
68- appErr := apperrors .Unwrap (err )
69- if appErr == nil {
68+ fErr := fail .Unwrap (err )
69+ if fErr == nil {
7070 return h .f (c , err )
7171 }
7272 return err
@@ -81,38 +81,44 @@ func WithNotWrappedErrorHandler(f ErrorHandlerFunc) interface {
8181}
8282
8383// WithReportableErrorHandler returns a new error handler function for handling errors annotated with the reportability.
84- func WithReportableErrorHandler (f AppErrorHandlerFunc ) interface {
84+ func WithReportableErrorHandler (f FailHandlerFunc ) interface {
8585 UnaryServerErrorHandler
8686 StreamServerErrorHandler
8787} {
88- return WithAppErrorHandler (func (c context.Context , err * apperrors .Error ) error {
89- if err .Report {
90- return f ( c , err )
88+ return WithFailHandler (func (c context.Context , err * fail .Error ) error {
89+ if err .Ignorable {
90+ return err
9191 }
92- return err
92+ return f ( c , err )
9393 })
9494}
9595
96- // WithStatusCodeMap returns a new error handler function for mapping status codes to gRPC's one.
97- func WithStatusCodeMap (m map [int ]codes.Code ) interface {
96+ // CodeMap maps any status codes to gRPC's `codes.Code`s.
97+ type CodeMap map [interface {}]codes.Code
98+
99+ // WithCodeMap returns a new error handler function for mapping status codes to gRPC's one.
100+ func WithCodeMap (m CodeMap ) interface {
98101 UnaryServerErrorHandler
99102 StreamServerErrorHandler
100103} {
101- return WithAppErrorHandler (func (c context.Context , err * apperrors .Error ) error {
102- if c , ok := m [err .StatusCode ]; ok {
104+ return WithFailHandler (func (c context.Context , err * fail .Error ) error {
105+ if c , ok := m [err .Code ]; ok {
103106 return status .Error (c , err .Error ())
104107 }
105108 return err
106109 })
107110}
108111
109- // WithStatusCodeMapper returns a new error handler function for mapping status codes to gRPC's one with given function.
110- func WithStatusCodeMapper (mapFn func (code int ) codes.Code ) interface {
112+ // CodeMapFunc returns gRPC's `codes.Code`s from given any codes.
113+ type CodeMapFunc func (code interface {}) codes.Code
114+
115+ // WithCodeMapper returns a new error handler function for mapping status codes to gRPC's one with given function.
116+ func WithCodeMapper (mapFn CodeMapFunc ) interface {
111117 UnaryServerErrorHandler
112118 StreamServerErrorHandler
113119} {
114- return WithAppErrorHandler (func (c context.Context , err * apperrors .Error ) error {
115- return status .Error (mapFn (err .StatusCode ), err .Error ())
120+ return WithFailHandler (func (c context.Context , err * fail .Error ) error {
121+ return status .Error (mapFn (err .Code ), err .Error ())
116122 })
117123}
118124
@@ -121,7 +127,7 @@ func WithGrpcStatusUnwrapper() interface {
121127 UnaryServerErrorHandler
122128 StreamServerErrorHandler
123129} {
124- return WithAppErrorHandler (func (c context.Context , err * apperrors .Error ) error {
130+ return WithFailHandler (func (c context.Context , err * fail .Error ) error {
125131 if _ , ok := status .FromError (err .Err ); ok {
126132 return err .Err
127133 }
0 commit comments