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