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

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