github.com/newrelic/newrelic-client-go@v1.1.0/pkg/infrastructure/infrastructure.go (about)

     1  // Package infrastructure provides metadata about the underlying Infrastructure API.
     2  package infrastructure
     3  
     4  import (
     5  	"net/http"
     6  
     7  	nrhttp "github.com/newrelic/newrelic-client-go/internal/http"
     8  )
     9  
    10  // ErrorResponse represents an error response from New Relic Infrastructure.
    11  type ErrorResponse struct {
    12  	Errors  []*ErrorDetail `json:"errors,omitempty"`
    13  	Message string         `json:"description,omitempty"`
    14  }
    15  
    16  // ErrorDetail represents the details of an error response from New Relic Infrastructure.
    17  type ErrorDetail struct {
    18  	Status string `json:"status,omitempty"`
    19  	Detail string `json:"detail,omitempty"`
    20  }
    21  
    22  // Error surfaces an error message from the Infrastructure error response.
    23  func (e *ErrorResponse) Error() string {
    24  	if e.Message != "" {
    25  		return e.Message
    26  	}
    27  
    28  	if len(e.Errors) > 0 && e.Errors[0].Detail != "" {
    29  		return e.Errors[0].Detail
    30  	}
    31  
    32  	return ""
    33  }
    34  
    35  // New creates a new instance of ErrorResponse.
    36  func (e *ErrorResponse) New() nrhttp.ErrorResponse {
    37  	return &ErrorResponse{}
    38  }
    39  
    40  func (e *ErrorResponse) IsNotFound() bool {
    41  	return false
    42  }
    43  
    44  func (e *ErrorResponse) IsRetryableError() bool {
    45  	return false
    46  }
    47  
    48  func (e *ErrorResponse) IsDeprecated() bool {
    49  	return false
    50  }
    51  
    52  // IsUnauthorized checks a response for a 401 Unauthorize HTTP status code.
    53  func (e *ErrorResponse) IsUnauthorized(resp *http.Response) bool {
    54  	return resp.StatusCode == http.StatusUnauthorized
    55  }
    56  
    57  func (e *ErrorResponse) IsPaymentRequired(resp *http.Response) bool {
    58  	return resp.StatusCode == http.StatusPaymentRequired
    59  }