@@ -45,30 +45,27 @@ func main() {
45
45
panic (err)
46
46
}
47
47
48
+ errorHandlers := []grpcerrors.ErrorHandlerFunc {
49
+ grpcerrors.WithNotWrappedErrorHandler (func (c context.Context , err error ) error {
50
+ // WithNotWrappedErrorHandler handles an error not wrapped with `*apperror.Error`.
51
+ // A handler function should wrap received error with `*apperror.Error`.
52
+ return apperrors.WithStatusCode (err, CodeNotWrapped)
53
+ }),
54
+ grpcerrors.WithReportableErrorHandler (func (c context.Context , err *apperrors.Error ) error {
55
+ // WithReportableErrorHandler handles an erorr annotated with the reportability.
56
+ // You reports to an external service if necessary.
57
+ // And you can attach request contexts to error reports.
58
+ return err
59
+ }),
60
+ grpcerrors.WithStatusCodeMap (grpcCodeByYourCode),
61
+ }
62
+
48
63
s := grpc.NewServer (
49
64
grpc_middleware.WithStreamServerChain (
50
- grpcerrors.StreamServerInterceptor (
51
- grpcerrors.WithNotWrappedErrorHandler (func (err error ) error {
52
- return apperrors.WithStatusCode (err, CodeNotWrapped)
53
- }),
54
- grpcerrors.WithReportableErrorHandler (func (err *apperrors.Error ) error {
55
- switch err.StatusCode {
56
- case CodeYourCustomError:
57
- // Report your custom errors
58
- case CodeNotWrapped:
59
- // Report not wrapped errors
60
- default :
61
- // Report errors
62
- }
63
- return err
64
- }),
65
- grpcerrors.WithStatusCodeMapper (grpcCodeByYourCode),
66
- ),
65
+ grpcerrors.StreamServerInterceptor (errorHandlers...),
67
66
),
68
67
grpc_middleware.WithUnaryServerChain (
69
- grpcerrors.UnaryServerInterceptor (
70
- // Write your error handlers for an unary server
71
- ),
68
+ grpcerrors.UnaryServerInterceptor (errorHandlers...),
72
69
),
73
70
)
74
71
0 commit comments