Skip to content

Commit

Permalink
feat(payments): use go-libs/errorsutils for mangopay client errors
Browse files Browse the repository at this point in the history
  • Loading branch information
laouji committed Oct 2, 2024
1 parent 16546fb commit 5dd55f0
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"
"net/http"
"strconv"

"github.com/formancehq/go-libs/errorsutils"
)

type OwnerAddress struct {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion internal/connectors/plugins/public/mangopay/client/payin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"net/http"

"github.com/formancehq/go-libs/errorsutils"
)

type PayinResponse struct {
Expand Down Expand Up @@ -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
}
6 changes: 4 additions & 2 deletions internal/connectors/plugins/public/mangopay/client/payout.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"encoding/json"
"fmt"
"net/http"

"github.com/formancehq/go-libs/errorsutils"
)

type PayoutRequest struct {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
4 changes: 3 additions & 1 deletion internal/connectors/plugins/public/mangopay/client/refund.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"net/http"

"github.com/formancehq/go-libs/errorsutils"
)

type Refund struct {
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/http"
"strconv"
"time"

"github.com/formancehq/go-libs/errorsutils"
)

type Payment struct {
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"fmt"
"net/http"

"github.com/formancehq/go-libs/errorsutils"
)

type Funds struct {
Expand Down Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion internal/connectors/plugins/public/mangopay/client/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"net/http"
"strconv"

"github.com/formancehq/go-libs/errorsutils"
)

type User struct {
Expand Down Expand Up @@ -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
}
6 changes: 4 additions & 2 deletions internal/connectors/plugins/public/mangopay/client/wallets.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"net/http"
"strconv"

"github.com/formancehq/go-libs/errorsutils"
)

type Wallet struct {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"encoding/json"
"fmt"
"net/http"

"github.com/formancehq/go-libs/errorsutils"
)

type EventType string
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

0 comments on commit 5dd55f0

Please sign in to comment.