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

     1  // Package errors provides error types for specific error scenarios.
     2  package errors
     3  
     4  import (
     5  	"fmt"
     6  	"net/http"
     7  )
     8  
     9  // NewNotFound returns a new instance of NotFound with an optional custom error message.
    10  func NewNotFound(err string) *NotFound {
    11  	e := NotFound{
    12  		err: err,
    13  	}
    14  
    15  	return &e
    16  }
    17  
    18  // NewNotFoundf returns a new instance of NotFound
    19  // with an optional formatted custom error message.
    20  func NewNotFoundf(format string, args ...interface{}) *NotFound {
    21  	return NewNotFound(fmt.Sprintf(format, args...))
    22  }
    23  
    24  // NotFound is returned when the target resource cannot be located.
    25  type NotFound struct {
    26  	err string
    27  }
    28  
    29  func (e *NotFound) Error() string {
    30  	if e.err == "" {
    31  		return "resource not found"
    32  	}
    33  
    34  	return e.err
    35  }
    36  
    37  // NewUnexpectedStatusCode returns a new instance of UnexpectedStatusCode
    38  // with an optional custom message.
    39  func NewUnexpectedStatusCode(statusCode int, err string) *UnexpectedStatusCode {
    40  	return &UnexpectedStatusCode{
    41  		err:        err,
    42  		statusCode: statusCode,
    43  	}
    44  }
    45  
    46  // NewUnexpectedStatusCodef returns a new instance of UnexpectedStatusCode
    47  // with an optional formatted custom message.
    48  func NewUnexpectedStatusCodef(statusCode int, format string, args ...interface{}) *UnexpectedStatusCode {
    49  	return NewUnexpectedStatusCode(statusCode, fmt.Sprintf(format, args...))
    50  }
    51  
    52  // UnexpectedStatusCode is returned when an unexpected status code is returned
    53  // from New Relic's APIs.
    54  type UnexpectedStatusCode struct {
    55  	err        string
    56  	statusCode int
    57  }
    58  
    59  func (e *UnexpectedStatusCode) Error() string {
    60  	msg := fmt.Sprintf("%d response returned", e.statusCode)
    61  
    62  	if e.err != "" {
    63  		msg += fmt.Sprintf(": %s", e.err)
    64  	}
    65  
    66  	return msg
    67  }
    68  
    69  // NewUnauthorizedError returns a new instance of UnauthorizedError
    70  // with an optional custom message.
    71  func NewUnauthorizedError() *UnauthorizedError {
    72  	return &UnauthorizedError{
    73  		err:        "Invalid credentials provided. Missing API key or an invalid API key was provided.",
    74  		statusCode: http.StatusUnauthorized,
    75  	}
    76  }
    77  
    78  // UnauthorizedError is returned when a 401 HTTP status code is returned
    79  // from New Relic's APIs.
    80  type UnauthorizedError struct {
    81  	err        string
    82  	statusCode int
    83  }
    84  
    85  func (e *UnauthorizedError) Error() string {
    86  	msg := fmt.Sprintf("%d response returned", e.statusCode)
    87  
    88  	if e.err != "" {
    89  		msg += fmt.Sprintf(": %s", e.err)
    90  	}
    91  
    92  	return msg
    93  }
    94  
    95  // NewMaxRetriesReached returns a new instance of MaxRetriesReached with an optional custom error message.
    96  func NewMaxRetriesReached(err string) *MaxRetriesReached {
    97  	e := MaxRetriesReached{
    98  		err: err,
    99  	}
   100  
   101  	return &e
   102  }
   103  
   104  // NewMaxRetriesReachedf returns a new instance of MaxRetriesReached
   105  // with an optional formatted custom error message.
   106  func NewMaxRetriesReachedf(format string, args ...interface{}) *MaxRetriesReached {
   107  	return NewMaxRetriesReached(fmt.Sprintf(format, args...))
   108  }
   109  
   110  // MaxRetriesReached is returned when the target resource cannot be located.
   111  type MaxRetriesReached struct {
   112  	err string
   113  }
   114  
   115  func (e *MaxRetriesReached) Error() string {
   116  	return fmt.Sprintf("maximum retries reached: %s", e.err)
   117  }
   118  
   119  // NewInvalidInput returns a new instance of InvalidInput with an optional custom error message.
   120  func NewInvalidInput(err string) *InvalidInput {
   121  	e := InvalidInput{
   122  		err: err,
   123  	}
   124  
   125  	return &e
   126  }
   127  
   128  // NewInvalidInputf returns a new instance of InvalidInput
   129  // with an optional formatted custom error message.
   130  func NewInvalidInputf(format string, args ...interface{}) *InvalidInput {
   131  	return NewInvalidInput(fmt.Sprintf(format, args...))
   132  }
   133  
   134  // InvalidInput is returned when the user input is invalid.
   135  type InvalidInput struct {
   136  	err string
   137  }
   138  
   139  func (e *InvalidInput) Error() string {
   140  	if e.err == "" {
   141  		return "invalid input error"
   142  	}
   143  
   144  	return e.err
   145  }
   146  
   147  // PaymentRequiredError is returned when a 402 HTTP status code is returned
   148  // from New Relic's APIs.
   149  type PaymentRequiredError struct {
   150  	err        string
   151  	statusCode int
   152  }
   153  
   154  func (e *PaymentRequiredError) Error() string {
   155  	return e.err
   156  }
   157  
   158  // NewPaymentRequiredError returns a new instance of PaymentRequiredError
   159  // with an optional custom message.
   160  func NewPaymentRequiredError() *PaymentRequiredError {
   161  	return &PaymentRequiredError{
   162  		err:        http.StatusText(http.StatusPaymentRequired),
   163  		statusCode: http.StatusPaymentRequired,
   164  	}
   165  }