github.com/Ingenico-ePayments/connect-sdk-go@v0.0.0-20240318153750-1f8cd329b9c9/errors/ReferenceError.go (about)

     1  package errors
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"github.com/Ingenico-ePayments/connect-sdk-go/domain/errors"
     7  )
     8  
     9  // ReferenceError represents an error response from the Ingenico ePayments platform when a non-existing or removed object is trying to be accessed.
    10  type ReferenceError struct {
    11  	errorMessage string
    12  	statusCode   int
    13  	responseBody string
    14  	errorID      string
    15  	errors       []errors.APIError
    16  }
    17  
    18  // Message returns the error message
    19  func (re *ReferenceError) Message() string {
    20  	return re.errorMessage
    21  }
    22  
    23  // StatusCode returns the status code
    24  func (re *ReferenceError) StatusCode() int {
    25  	return re.statusCode
    26  }
    27  
    28  // ResponseBody returns the response body
    29  func (re *ReferenceError) ResponseBody() string {
    30  	return re.responseBody
    31  }
    32  
    33  // ErrorID returns the error id
    34  func (re *ReferenceError) ErrorID() string {
    35  	return re.errorID
    36  }
    37  
    38  // Errors returns a slice of underlying errors
    39  func (re *ReferenceError) Errors() []errors.APIError {
    40  	// Return a clone instead of the original slice - immutability insurance
    41  	return append([]errors.APIError{}, re.errors...)
    42  }
    43  
    44  // String implements the Stringer interface
    45  // Format: 'errorMessage; statusCode=; responseBody='
    46  func (re *ReferenceError) String() string {
    47  	list := re.errorMessage
    48  
    49  	if re.statusCode > 0 {
    50  		list = list + "; statusCode=" + strconv.Itoa(re.statusCode)
    51  	}
    52  	if len(re.responseBody) != 0 {
    53  		list = list + "; responseBody='" + re.responseBody + "'"
    54  	}
    55  
    56  	return list
    57  }
    58  
    59  // Error implements the Error interface
    60  func (re *ReferenceError) Error() string {
    61  	return re.String()
    62  }
    63  
    64  // NewReferenceError creates a ReferenceError with the given statusCode, responseBody, errorID and errors
    65  func NewReferenceError(statusCode int, responseBody, errorID string, errors []errors.APIError) (*ReferenceError, error) {
    66  	return &ReferenceError{"the Ingenico ePayments platform returned an incorrect request error response", statusCode, responseBody, errorID, errors}, nil
    67  }
    68  
    69  // NewReferenceErrorVerbose creates a ReferenceError with the given message, statusCode, responseBody, errorID and errors
    70  func NewReferenceErrorVerbose(message string, statusCode int, responseBody, errorID string, errors []errors.APIError) (*ReferenceError, error) {
    71  	return &ReferenceError{message, statusCode, responseBody, errorID, errors}, nil
    72  }