Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
100 lines (94 loc) · 3.14 KB
/
Copy patherrors.go
File metadata and controls
100 lines (94 loc) · 3.14 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package supermarket
import (
"encoding/json"
"errors"
"fmt"
"strings"
)
// Sentinel errors for use with errors.Is. Supermarket reports a missing
// resource as 404 with error_code NOT_FOUND; Unwrap maps the error_code
// (and, failing that, the status) to ErrNotFound, so match on the sentinel
// rather than the status code.
var (
ErrNotFound = errors.New("supermarket: not found")
ErrUnauthorized = errors.New("supermarket: unauthorized")
ErrForbidden = errors.New("supermarket: forbidden")
ErrInvalidData = errors.New("supermarket: invalid data")
ErrUnauthenticatedWrite = errors.New("supermarket: write requires Username and Key")
// ErrInvalidArgument is returned locally, without contacting the server,
// when a caller passes a blank value for a required path segment such as
// a cookbook name or version.
ErrInvalidArgument = errors.New("supermarket: invalid argument")
// ErrConflict matches a 409, which Supermarket returns when deleting the
// only remaining version of a cookbook.
ErrConflict = errors.New("supermarket: conflict")
// ErrRateLimited matches a 429 that outlasted the client's retries.
ErrRateLimited = errors.New("supermarket: rate limited")
)
// ErrorResponse describes a non-2xx response from Supermarket.
type ErrorResponse struct {
Method string // request method
Path string // request path
StatusCode int // HTTP status code
Code string // server-supplied error_code (e.g. NOT_FOUND)
Messages []string // server-supplied error_messages
}
func (e *ErrorResponse) Error() string {
msg := strings.Join(e.Messages, "; ")
if msg == "" {
msg = "(no message)"
}
if e.Code != "" {
return fmt.Sprintf("supermarket: %s %s: %d %s: %s",
e.Method, e.Path, e.StatusCode, e.Code, msg)
}
return fmt.Sprintf("supermarket: %s %s: %d: %s",
e.Method, e.Path, e.StatusCode, msg)
}
// Unwrap maps the error_code (preferred) or HTTP status to a sentinel
// error for errors.Is.
func (e *ErrorResponse) Unwrap() error {
switch e.Code {
case "NOT_FOUND":
return ErrNotFound
case "UNAUTHORIZED", "AUTHENTICATION_FAILED":
return ErrUnauthorized
case "FORBIDDEN":
return ErrForbidden
case "INVALID_DATA", "MISSING_REQUIRED_DATA":
return ErrInvalidData
case "CONFLICT":
return ErrConflict
}
switch e.StatusCode {
case 401:
return ErrUnauthorized
case 403:
return ErrForbidden
case 404:
return ErrNotFound
case 409:
return ErrConflict
case 429:
return ErrRateLimited
}
return nil
}
// newErrorResponse decodes Supermarket's `{error_messages,error_code}`
// envelope. When the body is not the expected shape it falls back to
// the raw body as a single message.
func newErrorResponse(method, path string, status int, body []byte) *ErrorResponse {
er := &ErrorResponse{Method: method, Path: path, StatusCode: status}
var parsed struct {
ErrorMessages []string `json:"error_messages"`
ErrorCode string `json:"error_code"`
}
if json.Unmarshal(body, &parsed) == nil {
er.Messages = parsed.ErrorMessages
er.Code = parsed.ErrorCode
}
if len(er.Messages) == 0 && len(body) > 0 {
er.Messages = []string{strings.TrimSpace(string(body))}
}
return er
}