Skip to content

Commit

Permalink
Merge pull request #170 from ipfs/feat/better-errors
Browse files Browse the repository at this point in the history
make ErrorType a valid error and implement Unwrap on Error
  • Loading branch information
Stebalien committed Jun 13, 2019
2 parents 6069424 + 59cf45c commit 1adb804
Showing 1 changed file with 27 additions and 0 deletions.
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

0 comments on commit 1adb804

Please sign in to comment.