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

     1  package errors
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"github.com/Ingenico-ePayments/connect-sdk-go/communicator/communication"
     7  )
     8  
     9  // ResponseError is returned when a response was received from the Ingenico ePayments platform which indicates an error.
    10  type ResponseError struct {
    11  	statusCode int
    12  	body       string
    13  	headers    []communication.Header
    14  }
    15  
    16  // StatusCode gets the HTTP status code that was returned by the Ingenico ePayments platform.
    17  func (e *ResponseError) StatusCode() int {
    18  	return e.statusCode
    19  }
    20  
    21  // Body gets the raw response body that was returned by the Ingenico ePayments platform.
    22  func (e *ResponseError) Body() string {
    23  	return e.body
    24  }
    25  
    26  // Headers gets the headers that were returned by the Ingenico ePayments platform.
    27  func (e *ResponseError) Headers() []communication.Header {
    28  	return e.headers
    29  }
    30  
    31  // GetHeader returns the header with the given name, or nil if there was no such header.
    32  func (e *ResponseError) GetHeader(headerName string) *communication.Header {
    33  	return communication.Headers(e.headers).GetHeader(headerName)
    34  }
    35  
    36  // String implements the Stringer interface
    37  // Format: 'errorMessage; statusCode=; responseBody='
    38  func (e *ResponseError) String() string {
    39  	retString := "the Ingenico ePayments platform returned an error response"
    40  
    41  	statusCode := e.statusCode
    42  	if statusCode > 0 {
    43  		retString += ";  statusCode=" + strconv.Itoa(e.statusCode)
    44  	}
    45  
    46  	responseBody := e.body
    47  	if len(responseBody) > 0 {
    48  		retString += "; responseBody='" + e.body + "'"
    49  	}
    50  
    51  	return retString
    52  }
    53  
    54  // Error implements the Error interface
    55  func (e *ResponseError) Error() string {
    56  	return e.String()
    57  }
    58  
    59  // NewResponseError creates a new ResponseError with the specified response
    60  func NewResponseError(statusCode int, body string, headers []communication.Header) *ResponseError {
    61  	return &ResponseError{statusCode: statusCode, body: body, headers: headers}
    62  }