Debian Codegraph @

fastly/errors.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "bytes"
5 "errors"
6 "fmt"
7 "net/http"
8
9 "github.com/google/jsonapi"
10 )
11
12 // ErrMissingService is an error that is returned when an input struct requires
13 // a "Service" key, but one was not set.
14 var ErrMissingService = errors.New("Missing required field 'Service'")
15
16 // ErrMissingStatus is an error that is returned when an input struct requires
17 // a "Status" key, but one was not set.
18 var ErrMissingStatus = errors.New("Missing required field 'Status'")
19
20 // ErrMissingTag is an error that is returned when an input struct requires
21 // a "Tag" key, but one was not set.
22 var ErrMissingTag = errors.New("Missing required field 'Tag'")
23
24 // ErrMissingVersion is an error that is returned when an input struct requires
25 // a "Version" key, but one was not set.
26 var ErrMissingVersion = errors.New("Missing required field 'Version'")
27
28 // ErrMissingContent is an error that is returned when an input struct requires a
29 // "Content" key, but one was not set.
30 var ErrMissingContent = errors.New("Missing required field 'Content'")
31
32 // ErrMissingName is an error that is returned when an input struct requires a
33 // "Name" key, but one was not set.
34 var ErrMissingName = errors.New("Missing required field 'Name'")
35
36 // ErrMissingKey is an error that is returned when an input struct requires a
37 // "Key" key, but one was not set.
38 var ErrMissingKey = errors.New("Missing required field 'Key'")
39
40 // ErrMissingURL is an error that is returned when an input struct requires a
41 // "URL" key, but one was not set.
42 var ErrMissingURL = errors.New("Missing required field 'URL'")
43
44 // ErrMissingID is an error that is returned when an input struct requires an
45 // "ID" key, but one was not set.
46 var ErrMissingID = errors.New("Missing required field 'ID'")
47
48 // ErrMissingDictionary is an error that is returned when an input struct
49 // requires a "Dictionary" key, but one was not set.
50 var ErrMissingDictionary = errors.New("Missing required field 'Dictionary'")
51
52 // ErrMissingItemKey is an error that is returned when an input struct
53 // requires a "ItemKey" key, but one was not set.
54 var ErrMissingItemKey = errors.New("Missing required field 'ItemKey'")
55
56 // ErrMissingFrom is an error that is returned when an input struct
57 // requires a "From" key, but one was not set.
58 var ErrMissingFrom = errors.New("Missing required field 'From'")
59
60 // ErrMissingTo is an error that is returned when an input struct
61 // requires a "To" key, but one was not set.
62 var ErrMissingTo = errors.New("Missing required field 'To'")
63
64 // ErrMissingDirector is an error that is returned when an input struct
65 // requires a "From" key, but one was not set.
66 var ErrMissingDirector = errors.New("Missing required field 'Director'")
67
68 // ErrMissingBackend is an error that is returned when an input struct
69 // requires a "Backend" key, but one was not set.
70 var ErrMissingBackend = errors.New("Missing required field 'Backend'")
71
72 // ErrMissingYear is an error that is returned when an input struct
73 // requires a "Year" key, but one was not set.
74 var ErrMissingYear = errors.New("Missing required field 'Year'")
75
76 // ErrMissingMonth is an error that is returned when an input struct
77 // requires a "Month" key, but one was not set.
78 var ErrMissingMonth = errors.New("Missing required field 'Month'")
79
80 // ErrMissingNewName is an erorr that is returned when an input struct
81 // requires a "NewName" key, but one was not set
82 var ErrMissingNewName = errors.New("Missing required field 'NewName'")
83
84 // ErrMissingAcl is an error that is returned when an unout struct
85 // required an "Acl" key, but one is not set
86 var ErrMissingACL = errors.New("Missing required field 'ACL'")
87
88 // ErrMissingIP is an error that is returned when an unout struct
89 // required an "IP" key, but one is not set
90 var ErrMissingIP = errors.New("Missing requried field 'IP'")
91
92 // ErrMissingEventID is an error that is returned was an input struct
93 // requires a "EventID" key, but one was not set
94 var ErrMissingEventID = errors.New("Missing required field 'EventID'")
95
96 // ErrMissingWafID is an error that is returned was an input struct
97 // requires a "WafID" key, but one was not set
98 var ErrMissingWAFID = errors.New("Missing required field 'WAFID'")
99
100 // ErrMissingOWASPID is an error that is returned was an input struct
101 // requires a "OWASPID" key, but one was not set
102 var ErrMissingOWASPID = errors.New("Missing required field 'OWASPID'")
103
104 // ErrMissingRuleID is an error that is returned was an input struct
105 // requires a "RuleID" key, but one was not set
106 var ErrMissingRuleID = errors.New("Missing required field 'RuleID'")
107
108 // ErrMissingConfigSetID is an error that is returned was an input struct
109 // requires a "ConfigSetID" key, but one was not set
110 var ErrMissingConfigSetID = errors.New("Missing required field 'ConfigSetID'")
111
112 // ErrMissingWAFList is an error that is returned was an input struct
113 // requires a list of WAF id's, but it is empty
114 var ErrMissingWAFList = errors.New("WAF slice is empty")
115
116 // ErrBatchUpdateMaximumItemsExceeded is an error that indicates that too many batch operations are being executed.
117 // The Fastly API specifies an maximum limit.
118 var ErrBatchUpdateMaximumOperationsExceeded = errors.New("batch modify maximum operations exceeded")
119
120 // Ensure HTTPError is, in fact, an error.
121 var _ error = (*HTTPError)(nil)
122
123 // HTTPError is a custom error type that wraps an HTTP status code with some
124 // helper functions.
125 type HTTPError struct {
126 // StatusCode is the HTTP status code (2xx-5xx).
127 StatusCode int
128
129 Errors []*ErrorObject `mapstructure:"errors"`
130 }
131
132 // ErrorObject is a single error.
133 type ErrorObject struct {
134 ID string `mapstructure:"id"`
135 Title string `mapstructure:"title"`
136 Detail string `mapstructure:"detail"`
137 Status string `mapstructure:"status"`
138 Code string `mapstructure:"code"`
139
140 Meta *map[string]interface{} `mapstructure:"meta"`
141 }
142
143 // legacyError represents the older-style errors from Fastly. It is private
144 // because it is automatically converted to a jsonapi error.
145 type legacyError struct {
146 Message string `mapstructure:"msg"`
147 Detail string `mapstructure:"detail"`
148 }
149
150 // NewHTTPError creates a new HTTP error from the given code.
151 func NewHTTPError(resp *http.Response) *HTTPError {
152 var e HTTPError
153 e.StatusCode = resp.StatusCode
154
155 if resp.Body == nil {
156 return &e
157 }
158
159 // If this is a jsonapi response, decode it accordingly
160 if resp.Header.Get("Content-Type") == jsonapi.MediaType {
161 if err := decodeJSON(&e, resp.Body); err != nil {
162 panic(err)
163 }
164 } else {
165 var lerr *legacyError
166 decodeJSON(&lerr, resp.Body)
167 if lerr != nil {
168 e.Errors = append(e.Errors, &ErrorObject{
169 Title: lerr.Message,
170 Detail: lerr.Detail,
171 })
172 }
173 }
174
175 return &e
176 }
177
178 // Error implements the error interface and returns the string representing the
179 // error text that includes the status code and the corresponding status text.
180 func (e *HTTPError) Error() string {
181 var b bytes.Buffer
182
183 fmt.Fprintf(&b, "%d - %s:", e.StatusCode, http.StatusText(e.StatusCode))
184
185 for _, e := range e.Errors {
186 fmt.Fprintf(&b, "\n")
187
188 if e.ID != "" {
189 fmt.Fprintf(&b, "\n ID: %s", e.ID)
190 }
191
192 if e.Title != "" {
193 fmt.Fprintf(&b, "\n Title: %s", e.Title)
194 }
195
196 if e.Detail != "" {
197 fmt.Fprintf(&b, "\n Detail: %s", e.Detail)
198 }
199
200 if e.Code != "" {
201 fmt.Fprintf(&b, "\n Code: %s", e.Code)
202 }
203
204 if e.Meta != nil {
205 fmt.Fprintf(&b, "\n Meta: %v", *e.Meta)
206 }
207 }
208
209 return b.String()
210 }
211
212 // String implements the stringer interface and returns the string representing
213 // the string text that includes the status code and corresponding status text.
214 func (e *HTTPError) String() string {
215 return e.Error()
216 }
217
218 // IsNotFound returns true if the HTTP error code is a 404, false otherwise.
219 func (e *HTTPError) IsNotFound() bool {
220 return e.StatusCode == 404
221 }