Skip to content

Commit 0e7f9b1

Browse files
authored
Merge pull request #10 from izumin5210/izumin5210/readme
Improve readme
2 parents 97063a2 + 0b0a456 commit 0e7f9b1

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

README.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,27 @@ func main() {
4545
panic(err)
4646
}
4747

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+
4863
s := grpc.NewServer(
4964
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...),
6766
),
6867
grpc_middleware.WithUnaryServerChain(
69-
grpcerrors.UnaryServerInterceptor(
70-
// Write your error handlers for an unary server
71-
),
68+
grpcerrors.UnaryServerInterceptor(errorHandlers...),
7269
),
7370
)
7471

0 commit comments

Comments
 (0)