From 5dd55f0fbcaf601454fb33addc323f32fbb43d97 Mon Sep 17 00:00:00 2001 From: Crimson Thompson Date: Tue, 1 Oct 2024 20:21:13 +0200 Subject: [PATCH] feat(payments): use go-libs/errorsutils for mangopay client errors --- .../plugins/public/mangopay/client/bank_accounts.go | 6 ++++-- .../connectors/plugins/public/mangopay/client/payin.go | 4 +++- .../connectors/plugins/public/mangopay/client/payout.go | 6 ++++-- .../connectors/plugins/public/mangopay/client/refund.go | 4 +++- .../plugins/public/mangopay/client/transactions.go | 4 +++- .../connectors/plugins/public/mangopay/client/transfer.go | 7 ++++++- .../connectors/plugins/public/mangopay/client/users.go | 4 +++- .../connectors/plugins/public/mangopay/client/wallets.go | 6 ++++-- .../connectors/plugins/public/mangopay/client/webhooks.go | 8 +++++--- 9 files changed, 35 insertions(+), 14 deletions(-) diff --git a/internal/connectors/plugins/public/mangopay/client/bank_accounts.go b/internal/connectors/plugins/public/mangopay/client/bank_accounts.go index da92831c..a8a8540f 100644 --- a/internal/connectors/plugins/public/mangopay/client/bank_accounts.go +++ b/internal/connectors/plugins/public/mangopay/client/bank_accounts.go @@ -7,6 +7,8 @@ import ( "fmt" "net/http" "strconv" + + "github.com/formancehq/go-libs/errorsutils" ) type OwnerAddress struct { @@ -130,7 +132,7 @@ func (c *Client) createBankAccount(ctx context.Context, endpoint string, req any var bankAccount BankAccount statusCode, err := c.httpClient.Do(httpReq, &bankAccount, nil) if err != nil { - return nil, fmt.Errorf("failed to create bank account, got code %d: %w", statusCode, err) + return nil, errorsutils.NewErrorWithExitCode(fmt.Errorf("failed to create bank account: %w", err), statusCode) } return &bankAccount, nil } @@ -162,7 +164,7 @@ func (c *Client) GetBankAccounts(ctx context.Context, userID string, page, pageS var bankAccounts []BankAccount statusCode, err := c.httpClient.Do(req, &bankAccounts, nil) if err != nil { - return nil, fmt.Errorf("failed to get bank accounts, got code %d: %w", statusCode, err) + return nil, errorsutils.NewErrorWithExitCode(fmt.Errorf("failed to get bank accounts: %w", err), statusCode) } return bankAccounts, nil } diff --git a/internal/connectors/plugins/public/mangopay/client/payin.go b/internal/connectors/plugins/public/mangopay/client/payin.go index c07c0885..bed570e5 100644 --- a/internal/connectors/plugins/public/mangopay/client/payin.go +++ b/internal/connectors/plugins/public/mangopay/client/payin.go @@ -4,6 +4,8 @@ import ( "context" "fmt" "net/http" + + "github.com/formancehq/go-libs/errorsutils" ) type PayinResponse struct { @@ -41,7 +43,7 @@ func (c *Client) GetPayin(ctx context.Context, payinID string) (*PayinResponse, var payinResponse PayinResponse statusCode, err := c.httpClient.Do(req, &payinResponse, nil) if err != nil { - return nil, fmt.Errorf("failed to get payin response got code %d: %w", statusCode, err) + return nil, errorsutils.NewErrorWithExitCode(fmt.Errorf("failed to get payin: %w", err), statusCode) } return &payinResponse, nil } diff --git a/internal/connectors/plugins/public/mangopay/client/payout.go b/internal/connectors/plugins/public/mangopay/client/payout.go index 2aa70d58..ec390439 100644 --- a/internal/connectors/plugins/public/mangopay/client/payout.go +++ b/internal/connectors/plugins/public/mangopay/client/payout.go @@ -6,6 +6,8 @@ import ( "encoding/json" "fmt" "net/http" + + "github.com/formancehq/go-libs/errorsutils" ) type PayoutRequest struct { @@ -62,7 +64,7 @@ func (c *Client) InitiatePayout(ctx context.Context, payoutRequest *PayoutReques var payoutResponse PayoutResponse statusCode, err := c.httpClient.Do(req, &payoutResponse, nil) if err != nil { - return nil, fmt.Errorf("failed to initiate payout, got code %d: %w", statusCode, err) + return nil, errorsutils.NewErrorWithExitCode(fmt.Errorf("failed to initiate payout: %w", err), statusCode) } return &payoutResponse, nil } @@ -83,7 +85,7 @@ func (c *Client) GetPayout(ctx context.Context, payoutID string) (*PayoutRespons var payoutResponse PayoutResponse statusCode, err := c.httpClient.Do(req, &payoutResponse, nil) if err != nil { - return nil, fmt.Errorf("failed to get payout response got code %d: %w", statusCode, err) + return nil, errorsutils.NewErrorWithExitCode(fmt.Errorf("failed to get payout: %w", err), statusCode) } return &payoutResponse, nil } diff --git a/internal/connectors/plugins/public/mangopay/client/refund.go b/internal/connectors/plugins/public/mangopay/client/refund.go index 7e343495..3f9134de 100644 --- a/internal/connectors/plugins/public/mangopay/client/refund.go +++ b/internal/connectors/plugins/public/mangopay/client/refund.go @@ -4,6 +4,8 @@ import ( "context" "fmt" "net/http" + + "github.com/formancehq/go-libs/errorsutils" ) type Refund struct { @@ -42,7 +44,7 @@ func (c *Client) GetRefund(ctx context.Context, refundID string) (*Refund, error var refund Refund statusCode, err := c.httpClient.Do(req, &refund, nil) if err != nil { - return nil, fmt.Errorf("failed to get refund, got code %d: %w", statusCode, err) + return nil, errorsutils.NewErrorWithExitCode(fmt.Errorf("failed to get refund: %w", err), statusCode) } return &refund, nil } diff --git a/internal/connectors/plugins/public/mangopay/client/transactions.go b/internal/connectors/plugins/public/mangopay/client/transactions.go index 35379ba2..e8f91eea 100644 --- a/internal/connectors/plugins/public/mangopay/client/transactions.go +++ b/internal/connectors/plugins/public/mangopay/client/transactions.go @@ -7,6 +7,8 @@ import ( "net/http" "strconv" "time" + + "github.com/formancehq/go-libs/errorsutils" ) type Payment struct { @@ -61,7 +63,7 @@ func (c *Client) GetTransactions(ctx context.Context, walletsID string, page, pa var payments []Payment statusCode, err := c.httpClient.Do(req, &payments, nil) if err != nil { - return nil, fmt.Errorf("failed to get transactions, got code %d: %w", statusCode, err) + return nil, errorsutils.NewErrorWithExitCode(fmt.Errorf("failed to get transactions: %w", err), statusCode) } return payments, nil } diff --git a/internal/connectors/plugins/public/mangopay/client/transfer.go b/internal/connectors/plugins/public/mangopay/client/transfer.go index 01f83b05..5a5dc22a 100644 --- a/internal/connectors/plugins/public/mangopay/client/transfer.go +++ b/internal/connectors/plugins/public/mangopay/client/transfer.go @@ -5,6 +5,8 @@ import ( "encoding/json" "fmt" "net/http" + + "github.com/formancehq/go-libs/errorsutils" ) type Funds struct { @@ -54,7 +56,10 @@ func (c *Client) GetWalletTransfer(ctx context.Context, transferID string) (Tran var transfer TransferResponse statusCode, err := c.httpClient.Do(req, &transfer, nil) if err != nil { - return transfer, fmt.Errorf("failed to get transfer response, got code %d: %w", statusCode, err) + return transfer, errorsutils.NewErrorWithExitCode( + fmt.Errorf("failed to get transfer response: %w", err), + statusCode, + ) } return transfer, nil } diff --git a/internal/connectors/plugins/public/mangopay/client/users.go b/internal/connectors/plugins/public/mangopay/client/users.go index fca08c73..a6322749 100644 --- a/internal/connectors/plugins/public/mangopay/client/users.go +++ b/internal/connectors/plugins/public/mangopay/client/users.go @@ -5,6 +5,8 @@ import ( "fmt" "net/http" "strconv" + + "github.com/formancehq/go-libs/errorsutils" ) type User struct { @@ -33,7 +35,7 @@ func (c *Client) GetUsers(ctx context.Context, page int, pageSize int) ([]User, var users []User statusCode, err := c.httpClient.Do(req, &users, nil) if err != nil { - return nil, fmt.Errorf("failed to get user response, got code %d: %w", statusCode, err) + return nil, errorsutils.NewErrorWithExitCode(fmt.Errorf("failed to get user response: %w", err), statusCode) } return users, nil } diff --git a/internal/connectors/plugins/public/mangopay/client/wallets.go b/internal/connectors/plugins/public/mangopay/client/wallets.go index 3dedcc49..deaa307b 100644 --- a/internal/connectors/plugins/public/mangopay/client/wallets.go +++ b/internal/connectors/plugins/public/mangopay/client/wallets.go @@ -6,6 +6,8 @@ import ( "fmt" "net/http" "strconv" + + "github.com/formancehq/go-libs/errorsutils" ) type Wallet struct { @@ -42,7 +44,7 @@ func (c *Client) GetWallets(ctx context.Context, userID string, page, pageSize i var errRes mangopayError statusCode, err := c.httpClient.Do(req, &wallets, &errRes) if err != nil { - return nil, fmt.Errorf("failed to get wallets, got code %d: %w: %w", statusCode, err, errRes.Error()) + return nil, errorsutils.NewErrorWithExitCode(fmt.Errorf("failed to get wallets: %w %w", err, errRes.Error()), statusCode) } return wallets, nil } @@ -63,7 +65,7 @@ func (c *Client) GetWallet(ctx context.Context, walletID string) (*Wallet, error var errRes mangopayError statusCode, err := c.httpClient.Do(req, &wallet, &errRes) if err != nil { - return nil, fmt.Errorf("failed to get wallet, got code %d: %w: %w", statusCode, err, errRes.Error()) + return nil, errorsutils.NewErrorWithExitCode(fmt.Errorf("failed to get wallet: %w %w", err, errRes.Error()), statusCode) } return &wallet, nil } diff --git a/internal/connectors/plugins/public/mangopay/client/webhooks.go b/internal/connectors/plugins/public/mangopay/client/webhooks.go index bbdd5127..bca8e7c8 100644 --- a/internal/connectors/plugins/public/mangopay/client/webhooks.go +++ b/internal/connectors/plugins/public/mangopay/client/webhooks.go @@ -6,6 +6,8 @@ import ( "encoding/json" "fmt" "net/http" + + "github.com/formancehq/go-libs/errorsutils" ) type EventType string @@ -100,7 +102,7 @@ func (c *Client) ListAllHooks(ctx context.Context) ([]*Hook, error) { var errRes mangopayError statusCode, err := c.httpClient.Do(req, &hooks, &errRes) if err != nil { - return nil, fmt.Errorf("failed to list hooks, got code %d: %w: %w", statusCode, err, errRes.Error()) + return nil, errorsutils.NewErrorWithExitCode(fmt.Errorf("failed to list hooks: %w %w", err, errRes.Error()), statusCode) } return hooks, nil } @@ -134,7 +136,7 @@ func (c *Client) CreateHook(ctx context.Context, eventType EventType, URL string var errRes mangopayError statusCode, err := c.httpClient.Do(req, nil, &errRes) if err != nil { - return fmt.Errorf("failed to create hook, got code %d: %w: %w", statusCode, err, errRes.Error()) + return errorsutils.NewErrorWithExitCode(fmt.Errorf("failed to create hook: %w %w", err, errRes.Error()), statusCode) } return nil } @@ -168,7 +170,7 @@ func (c *Client) UpdateHook(ctx context.Context, hookID string, URL string) erro var errRes mangopayError statusCode, err := c.httpClient.Do(req, nil, &errRes) if err != nil { - return fmt.Errorf("failed to update hook, got code %d: %w: %w", statusCode, err, errRes.Error()) + return errorsutils.NewErrorWithExitCode(fmt.Errorf("failed to update hook: %w %w", err, errRes.Error()), statusCode) } return nil }