-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
37 lines (32 loc) · 842 Bytes
/
Copy patherrors.go
File metadata and controls
37 lines (32 loc) · 842 Bytes
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
package fipe
import (
"errors"
"fmt"
"net/http"
)
// Sentinel errors for common API failures. APIError unwraps to these, so
// callers can match with errors.Is regardless of which form they prefer.
var (
ErrNotFound = errors.New("fipe: not found")
ErrTooManyRequests = errors.New("fipe: too many requests")
)
// APIError is returned when the API responds with a non-2xx status.
type APIError struct {
StatusCode int
Body string
}
func (e *APIError) Error() string {
if e.Body != "" {
return fmt.Sprintf("fipe: unexpected status %d: %s", e.StatusCode, e.Body)
}
return fmt.Sprintf("fipe: unexpected status %d", e.StatusCode)
}
func (e *APIError) Unwrap() error {
switch e.StatusCode {
case http.StatusNotFound:
return ErrNotFound
case http.StatusTooManyRequests:
return ErrTooManyRequests
}
return nil
}