-
Notifications
You must be signed in to change notification settings - Fork 6
/
errors.go
38 lines (31 loc) · 1003 Bytes
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package itchio
import (
"fmt"
"strings"
"github.com/pkg/errors"
)
// APIError represents an itch.io API error. Some errors
// are just HTTP status codes, others have more detailed messages.
type APIError struct {
Messages []string `json:"messages"`
StatusCode int `json:"statusCode"`
Path string `json:"path"`
}
var _ error = (*APIError)(nil)
func (ae *APIError) Error() string {
return fmt.Sprintf("itch.io API error (%d): %s: %s", ae.StatusCode, ae.Path, strings.Join(ae.Messages, ", "))
}
// IsAPIError returns true if an error is an itch.io API error,
// even if it's wrapped with github.com/pkg/errors
func IsAPIError(err error) bool {
_, ok := AsAPIError(err)
return ok
}
// AsAPIError returns an *APIError and true if the
// passed error (no matter how deeply wrapped it is)
// is an *APIError. Otherwise it returns nil, false.
func AsAPIError(err error) (*APIError, bool) {
rootErr := errors.Cause(err)
apiError, ok := rootErr.(*APIError)
return apiError, ok
}