-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_error.go
60 lines (56 loc) · 2.09 KB
/
api_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
package brightbox
import (
"fmt"
"net/url"
"strings"
)
// APIError can be returned when an API request fails. It provides any error
// messages provided by the API, along with other details about the response.
type APIError struct {
// StatusCode will hold the HTTP status code from the request that errored
StatusCode int
// Status will hold the HTTP status line from the request that errored
Status string
// AuthError will hold any available OAuth "error" field contents. See
// https://api.gb1.brightbox.com/1.0/#errors
AuthError string `json:"error"`
// AuthErrorDescription will hold any available OAuth "error_description"
// field contents. See https://api.gb1.brightbox.com/1.0/#errors
AuthErrorDescription string `json:"error_description"`
// ErrorName will hold any available Brightbox API "error_name" field
// contents. See https://api.gb1.brightbox.com/1.0/#request_errors
ErrorName string `json:"error_name"`
// Errors will hold any available Brightbox API "errors" field contents. See
// https://api.gb1.brightbox.com/1.0/#request_errors
Errors []string `json:"errors"`
// ParseError will hold any errors from the JSON parser whilst parsing an
// API response
ParseError error
// RequestURL will hold the full URL used to make the request that errored,
// if available
RequestURL *url.URL
// ResponseBody will hold the raw respose body of the request that errored,
// if available
ResponseBody []byte
}
// Error implements the error interface
func (e *APIError) Error() string {
if e.AuthError != "" {
return fmt.Sprintf("%s, %s", e.AuthError, e.AuthErrorDescription)
}
if e.ErrorName != "" {
if len(e.Errors) == 0 {
return e.ErrorName
}
return fmt.Sprintf("%s: %s", e.ErrorName, strings.Join(e.Errors, ": "))
}
if e.ParseError != nil {
return fmt.Sprintf("ParseError at %s: %s", e.RequestURL, e.ParseError.Error())
}
return fmt.Sprintf("HttpError at %s: %s", e.RequestURL, e.Status)
}
// Unwrap implements the error wrapping interface
// Returns the parse errors from the JSON parser and Unmarshal interface
func (e *APIError) Unwrap() error {
return e.ParseError
}