forked from puppetlabs-toy-chest/wash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
231 lines (192 loc) · 5.92 KB
/
error.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package api
import (
"encoding/json"
"fmt"
"net/http"
apitypes "github.com/puppetlabs/wash/api/types"
"github.com/puppetlabs/wash/plugin"
)
// This approach was adapted from https://blog.golang.org/error-handling-and-go
// swagger:response
//nolint:deadcode,unused
type errorResp struct {
Body struct {
apitypes.ErrorObj
}
}
func newErrorObj(kind string, message string, fields apitypes.ErrorFields) *apitypes.ErrorObj {
return &apitypes.ErrorObj{
Kind: kind,
Msg: message,
Fields: fields,
}
}
func newUnknownErrorObj(err error) *apitypes.ErrorObj {
return newErrorObj(apitypes.UnknownError, err.Error(), apitypes.ErrorFields{})
}
func newStreamingErrorObj(stream string, reason string) *apitypes.ErrorObj {
return newErrorObj(
apitypes.StreamingError,
fmt.Sprintf("error streaming %v: %v", stream, reason),
apitypes.ErrorFields{"stream": stream},
)
}
// ErrorResponse represents an error response
type errorResponse struct {
statusCode int
body *apitypes.ErrorObj
}
// Used when formatting to write an HTTP response, so serialize as JSON.
func (e *errorResponse) Error() string {
jsonBytes, err := json.Marshal(e.body)
if err != nil {
// We should never hit this code-path, but better safe than sorry
return fmt.Sprintf("Kind: %v, Msg: %v, Fields: %v", e.body.Kind, e.body.Msg, e.body.Fields)
}
return string(jsonBytes)
}
// Below are all of Wash's API error responses
func unknownErrorResponse(err error) *errorResponse {
statusCode := http.StatusInternalServerError
body := newUnknownErrorObj(err)
return &errorResponse{statusCode, body}
}
func entryNotFoundResponse(path string, reason string) *errorResponse {
fields := apitypes.ErrorFields{"path": path}
statusCode := http.StatusNotFound
body := newErrorObj(
apitypes.EntryNotFound,
fmt.Sprintf("Could not find entry %v: %v", path, reason),
fields,
)
return &errorResponse{statusCode, body}
}
func pluginDoesNotExistResponse(plugin string) *errorResponse {
fields := apitypes.ErrorFields{"plugin": plugin}
statusCode := http.StatusNotFound
body := newErrorObj(
apitypes.PluginDoesNotExist,
fmt.Sprintf("Plugin %v does not exist", plugin),
fields,
)
return &errorResponse{statusCode, body}
}
func unsupportedActionResponse(path string, a plugin.Action) *errorResponse {
fields := apitypes.ErrorFields{
"path": path,
"action": a,
}
statusCode := http.StatusNotFound
msg := fmt.Sprintf("Entry %v does not support the %v action: It does not implement the %v protocol", path, a.Name, a.Protocol)
body := newErrorObj(
apitypes.UnsupportedAction,
msg,
fields,
)
return &errorResponse{statusCode, body}
}
func badRequestResponse(reason string) *errorResponse {
fields := apitypes.ErrorFields{}
body := newErrorObj(
apitypes.BadRequest,
fmt.Sprintf("Bad request: %v", reason),
fields,
)
return &errorResponse{http.StatusBadRequest, body}
}
func badActionRequestResponse(path string, a plugin.Action, reason string) *errorResponse {
fields := apitypes.ErrorFields{
"path": path,
"action": a.Name,
}
body := newErrorObj(
apitypes.BadActionRequest,
fmt.Sprintf("Bad request for %v action on %v: %v", a.Name, path, reason),
fields,
)
return &errorResponse{http.StatusBadRequest, body}
}
func journalUnavailableResponse(journalID string, reason string) *errorResponse {
return &errorResponse{http.StatusBadRequest, newErrorObj(
apitypes.JournalUnavailable,
fmt.Sprintf("Journal %v could not be opened: %v", journalID, reason),
apitypes.ErrorFields{
"journal": journalID,
},
)}
}
func outOfBoundsRequest(size int, reason string) *errorResponse {
return &errorResponse{http.StatusBadRequest, newErrorObj(
apitypes.OutOfBounds,
fmt.Sprintf("Invalid index requested: %v", reason),
apitypes.ErrorFields{"size": size},
)}
}
func invalidBoolParam(name, value string) *errorResponse {
return &errorResponse{http.StatusBadRequest, newErrorObj(
apitypes.InvalidBool,
fmt.Sprintf("Invalid boolean value '%v' given for %v parameter", value, name),
apitypes.ErrorFields{"value": value},
)}
}
func invalidIntParam(name, value string) *errorResponse {
return &errorResponse{http.StatusBadRequest, newErrorObj(
apitypes.InvalidInt,
fmt.Sprintf("Invalid int value '%v' given for %v parameter", value, name),
apitypes.ErrorFields{"value": value},
)}
}
func invalidPathsResponse() *errorResponse {
return &errorResponse{http.StatusBadRequest, newErrorObj(
apitypes.InvalidPaths,
"Request must include one 'path' query parameter",
apitypes.ErrorFields{},
)}
}
func erroredActionResponse(path string, a plugin.Action, reason string) *errorResponse {
fields := apitypes.ErrorFields{
"path": path,
"action": a.Name,
}
statusCode := http.StatusInternalServerError
body := newErrorObj(
apitypes.ErroredAction,
fmt.Sprintf("The %v action errored on %v: %v", a.Name, path, reason),
fields,
)
return &errorResponse{statusCode, body}
}
func duplicateCNameResponse(e plugin.DuplicateCNameErr) *errorResponse {
fields := apitypes.ErrorFields{
"parent_id": e.ParentID,
"first_child_name": e.FirstChildName,
"first_child_slash_replacer": e.FirstChildSlashReplacer,
"second_child_name": e.SecondChildName,
"second_child_slash_replacer": e.SecondChildSlashReplacer,
"cname": e.CName,
}
body := newErrorObj(
apitypes.DuplicateCName,
e.Error(),
fields,
)
return &errorResponse{http.StatusInternalServerError, body}
}
func relativePathResponse(path string) *errorResponse {
fields := apitypes.ErrorFields{
"path": path,
}
body := newErrorObj(
apitypes.RelativePath,
fmt.Sprintf("%v is a relative path. The Wash API only accepts absolute paths.", path),
fields,
)
return &errorResponse{http.StatusBadRequest, body}
}
func nonWashPathResponse(path string) *errorResponse {
return &errorResponse{http.StatusBadRequest, newErrorObj(
apitypes.NonWashPath,
"Request path is not managed by wash",
apitypes.ErrorFields{"path": path},
)}
}