github.com/newrelic/newrelic-client-go@v1.1.0/internal/http/errors.go (about) 1 package http 2 3 import ( 4 "fmt" 5 "net/http" 6 "strings" 7 ) 8 9 // ErrorResponse provides an interface for obtaining 10 // a single error message from an error response object. 11 type ErrorResponse interface { 12 IsNotFound() bool 13 IsRetryableError() bool 14 IsUnauthorized(resp *http.Response) bool 15 IsPaymentRequired(resp *http.Response) bool 16 IsDeprecated() bool 17 Error() string 18 New() ErrorResponse 19 } 20 21 // DefaultErrorResponse represents the default error response from New Relic. 22 type DefaultErrorResponse struct { 23 ErrorDetail ErrorDetail `json:"error"` 24 } 25 26 // ErrorDetail represents a New Relic response error detail. 27 type ErrorDetail struct { 28 Title string `json:"title"` 29 Messages []string `json:"messages"` 30 } 31 32 func (e *DefaultErrorResponse) Error() string { 33 m := e.ErrorDetail.Title 34 if len(e.ErrorDetail.Messages) > 0 { 35 m = fmt.Sprintf("%s: %s", m, strings.Join(e.ErrorDetail.Messages, ", ")) 36 } 37 38 return m 39 } 40 41 func (e *DefaultErrorResponse) IsNotFound() bool { 42 return false 43 } 44 45 func (e *DefaultErrorResponse) IsRetryableError() bool { 46 return false 47 } 48 49 func (e *DefaultErrorResponse) IsDeprecated() bool { 50 return false 51 } 52 53 func (e *DefaultErrorResponse) IsPaymentRequired(resp *http.Response) bool { 54 return resp.StatusCode == http.StatusPaymentRequired 55 } 56 57 // IsUnauthorized checks a response for a 401 Unauthorize HTTP status code. 58 func (e *DefaultErrorResponse) IsUnauthorized(resp *http.Response) bool { 59 return resp.StatusCode == http.StatusUnauthorized 60 } 61 62 // New creates a new instance of the DefaultErrorResponse struct. 63 func (e *DefaultErrorResponse) New() ErrorResponse { 64 return &DefaultErrorResponse{} 65 }