Skip to content

Commit ab8d057

Browse files
authored
Merge pull request #18 from srvc/fail
Use srvc/fail
2 parents e5c0848 + 69d4a0d commit ab8d057

File tree

7 files changed

+352
-427
lines changed

7 files changed

+352
-427
lines changed

Gopkg.lock

Lines changed: 110 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ required = [
2626

2727
[[constraint]]
2828
name = "google.golang.org/grpc"
29-
version = "1.6.0"
29+
version = "^1.6.0"
3030

3131
[[constraint]]
32-
name = "github.com/creasty/apperrors"
33-
version = "1.0.0"
32+
name = "github.com/srvc/fail"
33+
version = "^3.0.0"

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@ import (
1717
"context"
1818
"net"
1919

20-
"github.com/creasty/apperrors"
2120
"github.com/grpc-ecosystem/go-grpc-middleware"
21+
"github.com/srvc/fail"
2222
"github.com/srvc/grpc-errors"
2323
"google.golang.org/grpc"
2424
"google.golang.org/grpc/codes"
2525
)
2626

2727
const (
28-
CodeOK int = iota
28+
CodeOK uint32 = iota
2929
CodeInvalidArgument
3030
CodeNotFound
3131
CodeYourCustomError
3232
CodeNotWrapped
3333
CodeUnknown
3434
)
3535

36-
var grpcCodeByYourCode = map[int]codes.Code{
36+
var grpcCodeByYourCode = grpcerrors.CodeMap{
3737
CodeOK: codes.OK,
3838
CodeInvalidArgument: codes.InvalidArgument,
3939
CodeNotFound: codes.NotFound,
@@ -47,17 +47,17 @@ func main() {
4747

4848
errorHandlers := []grpcerrors.ErrorHandlerFunc{
4949
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)
50+
// WithNotWrappedErrorHandler handles an error not wrapped with `*fail.Error`.
51+
// A handler function should wrap received error with `*fail.Error`.
52+
return fail.Wrap(err, fail.WithCode(CodeNotWrapped))
5353
}),
54-
grpcerrors.WithReportableErrorHandler(func(c context.Context, err *apperrors.Error) error {
54+
grpcerrors.WithReportableErrorHandler(func(c context.Context, err *fail.Error) error {
5555
// WithReportableErrorHandler handles an erorr annotated with the reportability.
5656
// You reports to an external service if necessary.
5757
// And you can attach request contexts to error reports.
5858
return err
5959
}),
60-
grpcerrors.WithStatusCodeMap(grpcCodeByYourCode),
60+
grpcerrors.WithCodeMap(grpcCodeByYourCode),
6161
}
6262

6363
s := grpc.NewServer(

handlers.go

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package grpcerrors
22

33
import (
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.
2222
type 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

5555
type notWrappedHandler struct {
@@ -65,8 +65,8 @@ func (h *notWrappedHandler) HandleStreamServerError(c context.Context, req inter
6565
}
6666

6767
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 {
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

Comments
 (0)