Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make ErrorType a valid error and implement Unwrap on Error #170

Merged
merged 1 commit into from
Jun 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ const (
ErrForbidden
)

func (e ErrorType) Error() string {
return e.String()
}

func (e ErrorType) String() string {
switch e {
case ErrNormal:
return "command failed"
case ErrClient:
return "invalid argument"
case ErrImplementation:
return "internal error"
case ErrRateLimited:
return "rate limited"
case ErrForbidden:
return "request forbidden"
default:
return "unknown error code"
}
}

// Error is a struct for marshalling errors
type Error struct {
Message string
Expand All @@ -42,6 +63,12 @@ func (e Error) Error() string {
return e.Message
}

// Unwrap returns the base error (an ErrorType). Works with go 1.13 error
// helpers.
func (e Error) Unwrap() error {
return e.Code
}

func (e Error) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Message string
Expand Down