Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
152 lines (142 loc) · 4.99 KB
/
Copy patherrors.go
File metadata and controls
152 lines (142 loc) · 4.99 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package cinc
import (
"encoding/json"
"errors"
"fmt"
"strings"
"unicode/utf8"
)
// Sentinel errors for use with errors.Is.
var (
// ErrNotFound is a 404: the object, or the organization, does not exist.
ErrNotFound = errors.New("cinc: not found")
// ErrConflict is a 409: an object of that name already exists, or (for a
// cookbook version) it is frozen; see ErrCookbookFrozen.
ErrConflict = errors.New("cinc: conflict")
// ErrForbidden is a 403: the requestor lacks the permission, or the
// server refused the change outright (see ErrAdminsRequiredOnGrant).
ErrForbidden = errors.New("cinc: forbidden")
// ErrUnauthorized is a 401: the signature did not verify, which is
// nearly always the wrong key or a skewed clock.
ErrUnauthorized = errors.New("cinc: unauthorized")
// ErrBadRequest is a 400: the server refused the request as malformed or
// naming something that does not exist, such as an unknown actor or group
// in an ACL write.
ErrBadRequest = errors.New("cinc: bad request")
// ErrAdminsRequiredOnGrant is erchef refusing to take the admins group
// off an object's grant permission, which only the superuser may do. The
// server answers 403, so the error also matches ErrForbidden, but it says
// nothing about the caller's own grant permission: erchef checks this
// rule before it checks authorization. ACLs.Revoke reports it.
ErrAdminsRequiredOnGrant = errors.New("cinc: the admins group cannot be removed from the grant permission")
// ErrCookbookFrozen is Cookbooks.Upload refused because the version is
// frozen; upload WithForce to replace it. It also matches ErrConflict.
ErrCookbookFrozen = errors.New("cinc: cookbook version is frozen")
)
// ErrorResponse describes a non-2xx response from the Chef server.
type ErrorResponse struct {
Method string // request method
Path string // request path
StatusCode int // HTTP status code
Messages []string // server-reported error messages
}
// ServerMessage returns what the server said, its Messages joined with "; ",
// without Error's "cinc: METHOD PATH: CODE:" prefix or the 401 hint. It is ""
// when the server sent no message (or e is nil), so a caller can fall back to
// its own wording.
func (e *ErrorResponse) ServerMessage() string {
if e == nil {
return ""
}
return strings.Join(e.Messages, "; ")
}
func (e *ErrorResponse) Error() string {
msg := e.ServerMessage()
if msg == "" {
msg = "(no message)"
}
// A 401 is nearly always a bad key or a skewed clock, and the server's own
// message rarely says so. The hint belongs here rather than in Messages,
// which reports what the server said.
if e.StatusCode == 401 {
msg += " (check the client key and that the local clock is in sync)"
}
return fmt.Sprintf("cinc: %s %s: %d: %s", e.Method, e.Path, e.StatusCode, msg)
}
// Unwrap maps the status code to a sentinel error for errors.Is.
func (e *ErrorResponse) Unwrap() error {
switch e.StatusCode {
case 400:
return ErrBadRequest
case 401:
return ErrUnauthorized
case 403:
return ErrForbidden
case 404:
return ErrNotFound
case 409:
return ErrConflict
default:
return nil
}
}
// newErrorResponse builds an *ErrorResponse, decoding the Chef error body.
func newErrorResponse(method, path string, code int, body []byte) *ErrorResponse {
er := &ErrorResponse{Method: method, Path: path, StatusCode: code}
var parsed struct {
Error json.RawMessage `json:"error"`
Message string `json:"message"`
}
if json.Unmarshal(body, &parsed) == nil && parsed.Error != nil {
// "error" is a bare string, or a list whose elements are strings or,
// for depsolver failures, objects carrying a "message".
var list []json.RawMessage
if json.Unmarshal(parsed.Error, &list) == nil {
for _, item := range list {
if m := errorText(item); m != "" {
er.Messages = append(er.Messages, m)
}
}
} else if m := errorText(parsed.Error); m != "" {
er.Messages = []string{m}
}
// An invalid API version answers with a slug in "error" and the
// explanation in a top-level "message".
if parsed.Message != "" {
er.Messages = append(er.Messages, parsed.Message)
}
}
if len(er.Messages) == 0 && len(body) > 0 {
er.Messages = []string{truncate(strings.TrimSpace(string(body)), maxRawErrorBody)}
}
return er
}
// maxRawErrorBody caps an error body that is not a Chef error document (a
// proxy's HTML page, say), so it cannot bloat an error message or a log.
const maxRawErrorBody = 512
// truncate returns s cut to at most n bytes, on a rune boundary, with an
// ellipsis when anything was cut.
func truncate(s string, n int) string {
if len(s) <= n {
return s
}
for n > 0 && !utf8.RuneStart(s[n]) {
n--
}
return s[:n] + "…"
}
// errorText returns the text of one element of an erchef "error" value: a
// string, or an object's "message".
func errorText(raw json.RawMessage) string {
var s string
if json.Unmarshal(raw, &s) == nil {
return s
}
var obj struct {
Message string `json:"message"`
}
if json.Unmarshal(raw, &obj) == nil {
return obj.Message
}
return ""
}