-
Notifications
You must be signed in to change notification settings - Fork 89
/
errors.go
65 lines (53 loc) · 1.58 KB
/
errors.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
package http
import (
"fmt"
"net/http"
"strings"
)
// ErrorResponse provides an interface for obtaining
// a single error message from an error response object.
type ErrorResponse interface {
IsNotFound() bool
IsRetryableError() bool
IsUnauthorized(resp *http.Response) bool
IsPaymentRequired(resp *http.Response) bool
IsDeprecated() bool
Error() string
New() ErrorResponse
}
// DefaultErrorResponse represents the default error response from New Relic.
type DefaultErrorResponse struct {
ErrorDetail ErrorDetail `json:"error"`
}
// ErrorDetail represents a New Relic response error detail.
type ErrorDetail struct {
Title string `json:"title"`
Messages []string `json:"messages"`
}
func (e *DefaultErrorResponse) Error() string {
m := e.ErrorDetail.Title
if len(e.ErrorDetail.Messages) > 0 {
m = fmt.Sprintf("%s: %s", m, strings.Join(e.ErrorDetail.Messages, ", "))
}
return m
}
func (e *DefaultErrorResponse) IsNotFound() bool {
return false
}
func (e *DefaultErrorResponse) IsRetryableError() bool {
return false
}
func (e *DefaultErrorResponse) IsDeprecated() bool {
return false
}
func (e *DefaultErrorResponse) IsPaymentRequired(resp *http.Response) bool {
return resp.StatusCode == http.StatusPaymentRequired
}
// IsUnauthorized checks a response for a 401 Unauthorize HTTP status code.
func (e *DefaultErrorResponse) IsUnauthorized(resp *http.Response) bool {
return resp.StatusCode == http.StatusUnauthorized
}
// New creates a new instance of the DefaultErrorResponse struct.
func (e *DefaultErrorResponse) New() ErrorResponse {
return &DefaultErrorResponse{}
}