github.com/Ingenico-ePayments/connect-sdk-go@v0.0.0-20240318153750-1f8cd329b9c9/errors/DeclinedPayoutError.go (about) 1 package errors 2 3 import ( 4 goerr "errors" 5 "fmt" 6 "strconv" 7 8 "github.com/Ingenico-ePayments/connect-sdk-go/domain/errors" 9 "github.com/Ingenico-ePayments/connect-sdk-go/domain/payout" 10 ) 11 12 // DeclinedPayoutError represents an error response from a create payout call 13 type DeclinedPayoutError struct { 14 errorMessage string 15 statusCode int 16 responseBody string 17 errorResponse *payout.ErrorResponse 18 } 19 20 // Message returns the error message 21 func (dpe *DeclinedPayoutError) Message() string { 22 return dpe.errorMessage 23 } 24 25 // StatusCode returns the status code 26 func (dpe *DeclinedPayoutError) StatusCode() int { 27 return dpe.statusCode 28 } 29 30 // ResponseBody returns the response body 31 func (dpe *DeclinedPayoutError) ResponseBody() string { 32 return dpe.responseBody 33 } 34 35 // ErrorID returns the error id 36 func (dpe *DeclinedPayoutError) ErrorID() string { 37 if dpe.errorResponse.ErrorID == nil { 38 return "" 39 } 40 return *dpe.errorResponse.ErrorID 41 } 42 43 // Errors returns a slice of underlying errors 44 func (dpe *DeclinedPayoutError) Errors() []errors.APIError { 45 // Return a clone instead of the original slice - immutability insurance 46 if dpe.errorResponse.Errors == nil { 47 return []errors.APIError{} 48 } 49 return append([]errors.APIError{}, *dpe.errorResponse.Errors...) 50 } 51 52 // PayoutResult returns the payout result 53 func (dpe *DeclinedPayoutError) PayoutResult() *payout.Result { 54 return dpe.errorResponse.PayoutResult 55 } 56 57 // String implements the Stringer interface 58 // Format: 'errorMessage; statusCode=; responseBody=' 59 func (dpe *DeclinedPayoutError) String() string { 60 list := dpe.errorMessage 61 62 if dpe.statusCode > 0 { 63 list = list + "; statusCode=" + strconv.Itoa(dpe.statusCode) 64 } 65 if len(dpe.responseBody) != 0 { 66 list = list + "; responseBody='" + dpe.responseBody + "'" 67 } 68 69 return list 70 } 71 72 // Error implements the Error interface 73 func (dpe *DeclinedPayoutError) Error() string { 74 return dpe.String() 75 } 76 77 // NewDeclinedPayoutError creates a DeclinedPayoutError with the given statusCode, responseBody and errorResponse 78 func NewDeclinedPayoutError(statusCode int, responseBody string, errorResponse *payout.ErrorResponse) (*DeclinedPayoutError, error) { 79 if errorResponse.PayoutResult.ID == nil || errorResponse.PayoutResult.Status == nil { 80 return nil, goerr.New("Cannot get payout") 81 } 82 errorMessage := fmt.Sprintf("declined payout '%s' with status '%s'", 83 *errorResponse.PayoutResult.ID, 84 *errorResponse.PayoutResult.Status) 85 86 return &DeclinedPayoutError{errorMessage, statusCode, responseBody, errorResponse}, nil 87 }