forked from appscode/go-hetzner
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror.go
More file actions
47 lines (41 loc) · 1.3 KB
/
error.go
File metadata and controls
47 lines (41 loc) · 1.3 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
package hetzner
import (
"fmt"
"net/http"
"strings"
)
// APIError An ErrorResponse reports the error caused by an API request
type APIError struct {
// HTTP response that caused this error
Response *http.Response `json:"-"`
Status int `json:"status"`
Code string `json:"code"`
Message string `json:"message"`
// Array of missing input parameters or null
Missing []string `json:"missing"`
// Array of invalid input parameters or null
Invalid []string `json:"invalid"`
// Maximum allowed requests
MaxRequest int `json:"max_request"`
// Time interval in seconds
Interval int `json:"interval"`
}
func (r *APIError) Error() string {
if r.Status == 400 && r.Code == "INVALID_INPUT" {
return fmt.Sprintf(
"%v %v: %d %v %v (missing %v) (invalid %v)",
r.Response.Request.Method, r.Response.Request.URL, r.Status, r.Code, r.Message,
strings.Join(r.Missing, ","), strings.Join(r.Invalid, ","),
)
} else if r.Status == 403 && r.Code == "RATE_LIMIT_EXCEEDED" {
return fmt.Sprintf(
"%v %v: %d %v %v (max_request %v) (internal %v sec)",
r.Response.Request.Method, r.Response.Request.URL, r.Status, r.Code, r.Message, r.MaxRequest, r.Interval,
)
} else {
return fmt.Sprintf(
"%v %v: %d %v %v",
r.Response.Request.Method, r.Response.Request.URL, r.Status, r.Code, r.Message,
)
}
}