Skip to content

Commit 59cf45c

Browse files
committed
make ErrorType a valid error and implement Unwrap on Error
That way, go 1.13's errors library can check if an error "is" one of these error types.
1 parent 6069424 commit 59cf45c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

error.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,27 @@ const (
2424
ErrForbidden
2525
)
2626

27+
func (e ErrorType) Error() string {
28+
return e.String()
29+
}
30+
31+
func (e ErrorType) String() string {
32+
switch e {
33+
case ErrNormal:
34+
return "command failed"
35+
case ErrClient:
36+
return "invalid argument"
37+
case ErrImplementation:
38+
return "internal error"
39+
case ErrRateLimited:
40+
return "rate limited"
41+
case ErrForbidden:
42+
return "request forbidden"
43+
default:
44+
return "unknown error code"
45+
}
46+
}
47+
2748
// Error is a struct for marshalling errors
2849
type Error struct {
2950
Message string
@@ -42,6 +63,12 @@ func (e Error) Error() string {
4263
return e.Message
4364
}
4465

66+
// Unwrap returns the base error (an ErrorType). Works with go 1.13 error
67+
// helpers.
68+
func (e Error) Unwrap() error {
69+
return e.Code
70+
}
71+
4572
func (e Error) MarshalJSON() ([]byte, error) {
4673
return json.Marshal(struct {
4774
Message string

0 commit comments

Comments
 (0)