1
1
package grpcerrors
2
2
3
3
import (
4
- "github.com/creasty/apperrors "
4
+ "github.com/srvc/fail "
5
5
"golang.org/x/net/context"
6
6
"google.golang.org/grpc"
7
7
"google.golang.org/grpc/codes"
@@ -21,35 +21,35 @@ type StreamServerErrorHandler interface {
21
21
// ErrorHandlerFunc is a function that called by interceptors when specified erorrs are detected.
22
22
type ErrorHandlerFunc func (context.Context , error ) error
23
23
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
26
26
27
- type appErrorHandler struct {
28
- f AppErrorHandlerFunc
27
+ type failHandler struct {
28
+ f FailHandlerFunc
29
29
}
30
30
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 {
32
32
return h .handleError (c , err )
33
33
}
34
34
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 {
36
36
return h .handleError (c , err )
37
37
}
38
38
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 )
43
43
}
44
44
return err
45
45
}
46
46
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 {
49
49
UnaryServerErrorHandler
50
50
StreamServerErrorHandler
51
51
} {
52
- return & appErrorHandler {f : f }
52
+ return & failHandler {f : f }
53
53
}
54
54
55
55
type notWrappedHandler struct {
@@ -65,8 +65,8 @@ func (h *notWrappedHandler) HandleStreamServerError(c context.Context, req inter
65
65
}
66
66
67
67
func (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 {
70
70
return h .f (c , err )
71
71
}
72
72
return err
@@ -81,38 +81,44 @@ func WithNotWrappedErrorHandler(f ErrorHandlerFunc) interface {
81
81
}
82
82
83
83
// 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 {
85
85
UnaryServerErrorHandler
86
86
StreamServerErrorHandler
87
87
} {
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
91
91
}
92
- return err
92
+ return f ( c , err )
93
93
})
94
94
}
95
95
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 {
98
101
UnaryServerErrorHandler
99
102
StreamServerErrorHandler
100
103
} {
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 {
103
106
return status .Error (c , err .Error ())
104
107
}
105
108
return err
106
109
})
107
110
}
108
111
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 {
111
117
UnaryServerErrorHandler
112
118
StreamServerErrorHandler
113
119
} {
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 ())
116
122
})
117
123
}
118
124
@@ -121,7 +127,7 @@ func WithGrpcStatusUnwrapper() interface {
121
127
UnaryServerErrorHandler
122
128
StreamServerErrorHandler
123
129
} {
124
- return WithAppErrorHandler (func (c context.Context , err * apperrors .Error ) error {
130
+ return WithFailHandler (func (c context.Context , err * fail .Error ) error {
125
131
if _ , ok := status .FromError (err .Err ); ok {
126
132
return err .Err
127
133
}
0 commit comments