From 0b0a456c072f95cbbadc6d71d312d27b64d58ee4 Mon Sep 17 00:00:00 2001 From: izumin5210 Date: Sun, 15 Oct 2017 18:35:44 +0900 Subject: [PATCH] Improve readme --- README.md | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index e9a3687..07d499e 100644 --- a/README.md +++ b/README.md @@ -45,30 +45,27 @@ func main() { 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( - grpcerrors.WithNotWrappedErrorHandler(func(err error) error { - return apperrors.WithStatusCode(err, CodeNotWrapped) - }), - grpcerrors.WithReportableErrorHandler(func(err *apperrors.Error) error { - switch err.StatusCode { - case CodeYourCustomError: - // Report your custom errors - case CodeNotWrapped: - // Report not wrapped errors - default: - // Report errors - } - return err - }), - grpcerrors.WithStatusCodeMapper(grpcCodeByYourCode), - ), + grpcerrors.StreamServerInterceptor(errorHandlers...), ), grpc_middleware.WithUnaryServerChain( - grpcerrors.UnaryServerInterceptor( - // Write your error handlers for an unary server - ), + grpcerrors.UnaryServerInterceptor(errorHandlers...), ), )