-
Notifications
You must be signed in to change notification settings - Fork 33
/
error.go
51 lines (41 loc) · 1.01 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
package ovh
import (
"fmt"
"strings"
)
// APIError represents an error that can occurred while calling the API.
type APIError struct {
// Error class
Class string `json:"class,omitempty"`
// Error message.
Message string `json:"message"`
// Error details
Details map[string]string `json:"details,omitempty"`
// HTTP code.
Code int
// ID of the request
QueryID string
}
// Let's make sure that APIError always satisfies the fmt.Stringer and error interfaces
var _ fmt.Stringer = APIError{}
var _ error = APIError{}
func (err APIError) Error() string {
var sb strings.Builder
sb.Grow(128)
// Base message
fmt.Fprint(&sb, "OVHcloud API error (status code ", err.Code, "): ")
// Append class if any
if err.Class != "" {
fmt.Fprint(&sb, err.Class, ": ")
}
// Real error message, quoted
fmt.Fprintf(&sb, "%q", err.Message)
// QueryID if any
if err.QueryID != "" {
fmt.Fprint(&sb, " (X-OVH-Query-Id: ", err.QueryID, ")")
}
return sb.String()
}
func (err APIError) String() string {
return err.Error()
}