From 1e307a5d9226f3634390d2cb55ea989ec16a98be Mon Sep 17 00:00:00 2001 From: Crimson Thompson Date: Fri, 4 Oct 2024 14:07:47 +0200 Subject: [PATCH] feat(payments): wrap Stripe SDK errors so we can detect http client-side issues and assign an appropriate gpc error code --- .../plugins/public/stripe/client/accounts.go | 4 ++-- .../plugins/public/stripe/client/balances.go | 1 + .../plugins/public/stripe/client/client.go | 22 +++++++++++++++++++ .../plugins/public/stripe/client/error.go | 16 -------------- .../public/stripe/client/external_accounts.go | 4 ++-- .../plugins/public/stripe/client/payments.go | 4 ++-- 6 files changed, 29 insertions(+), 22 deletions(-) delete mode 100644 internal/connectors/plugins/public/stripe/client/error.go diff --git a/internal/connectors/plugins/public/stripe/client/accounts.go b/internal/connectors/plugins/public/stripe/client/accounts.go index 75ca3bc2..e386e5d1 100644 --- a/internal/connectors/plugins/public/stripe/client/accounts.go +++ b/internal/connectors/plugins/public/stripe/client/accounts.go @@ -22,7 +22,7 @@ func (c *client) GetAccounts( var oldest interface{} oldest, timeline, hasMore, err = scanForOldest(timeline, pageSize, func(params stripe.ListParams) (stripe.ListContainer, error) { itr := c.accountClient.List(&stripe.AccountListParams{ListParams: params}) - return itr.AccountList(), itr.Err() + return itr.AccountList(), wrapSDKErr(itr.Err()) }) if err != nil { return results, timeline, false, err @@ -43,5 +43,5 @@ func (c *client) GetAccounts( itr := c.accountClient.List(&stripe.AccountListParams{ListParams: filters}) results = append(results, itr.AccountList().Data...) timeline.LatestID = results[len(results)-1].ID - return results, timeline, itr.AccountList().ListMeta.HasMore, itr.Err() + return results, timeline, itr.AccountList().ListMeta.HasMore, wrapSDKErr(itr.Err()) } diff --git a/internal/connectors/plugins/public/stripe/client/balances.go b/internal/connectors/plugins/public/stripe/client/balances.go index 47e7f90d..83c69cdb 100644 --- a/internal/connectors/plugins/public/stripe/client/balances.go +++ b/internal/connectors/plugins/public/stripe/client/balances.go @@ -18,6 +18,7 @@ func (c *client) GetAccountBalances(ctx context.Context, accountID string) (*str } balance, err := c.balanceClient.Get(&stripe.BalanceParams{Params: filters}) + err = wrapSDKErr(err) if err != nil { return nil, fmt.Errorf("failed to get stripe balance: %w", err) } diff --git a/internal/connectors/plugins/public/stripe/client/client.go b/internal/connectors/plugins/public/stripe/client/client.go index 0e3a6c68..44908826 100644 --- a/internal/connectors/plugins/public/stripe/client/client.go +++ b/internal/connectors/plugins/public/stripe/client/client.go @@ -2,7 +2,9 @@ package client import ( "context" + "fmt" + "github.com/formancehq/payments/internal/connectors/httpwrapper" "github.com/stripe/stripe-go/v79" "github.com/stripe/stripe-go/v79/account" "github.com/stripe/stripe-go/v79/balance" @@ -42,3 +44,23 @@ func limit(wanted int64, have int) *int64 { needed := wanted - int64(have) return &needed } + +// wrap a public error for cases that we don't want to retry +// so that activities can classify this error for temporal +func wrapSDKErr(err error) error { + if err == nil { + return nil + } + + stripeErr, ok := err.(*stripe.Error) + if !ok { + return err + } + + switch stripeErr.Type { + case stripe.ErrorTypeInvalidRequest, stripe.ErrorTypeIdempotency: + return fmt.Errorf("%w: %w", httpwrapper.ErrStatusCodeClientError, err) + + } + return err +} diff --git a/internal/connectors/plugins/public/stripe/client/error.go b/internal/connectors/plugins/public/stripe/client/error.go deleted file mode 100644 index 06c710d3..00000000 --- a/internal/connectors/plugins/public/stripe/client/error.go +++ /dev/null @@ -1,16 +0,0 @@ -package client - -import ( - "fmt" - - "github.com/stripe/stripe-go/v79" -) - -func handleError(itr *stripe.Iter) error { - err := itr.Err() - meta := itr.Meta() - if meta == nil { - return fmt.Errorf("failed stripe request: %w", err) - } - return fmt.Errorf("stripe api failed in req to %q: %w", meta.URL, err) -} diff --git a/internal/connectors/plugins/public/stripe/client/external_accounts.go b/internal/connectors/plugins/public/stripe/client/external_accounts.go index ebd9595e..daa31922 100644 --- a/internal/connectors/plugins/public/stripe/client/external_accounts.go +++ b/internal/connectors/plugins/public/stripe/client/external_accounts.go @@ -31,7 +31,7 @@ func (c *client) GetExternalAccounts( Account: &accountID, ListParams: params, }) - return itr.BankAccountList(), itr.Err() + return itr.BankAccountList(), wrapSDKErr(itr.Err()) }) if err != nil { return results, timeline, false, err @@ -51,7 +51,7 @@ func (c *client) GetExternalAccounts( }, }) if err := itr.Err(); err != nil { - return nil, timeline, false, handleError(itr.Iter) + return nil, timeline, false, wrapSDKErr(err) } results = append(results, itr.BankAccountList().Data...) timeline.LatestID = results[len(results)-1].ID diff --git a/internal/connectors/plugins/public/stripe/client/payments.go b/internal/connectors/plugins/public/stripe/client/payments.go index dc31d604..f8022d24 100644 --- a/internal/connectors/plugins/public/stripe/client/payments.go +++ b/internal/connectors/plugins/public/stripe/client/payments.go @@ -39,7 +39,7 @@ func (c *client) GetPayments( transactionParams := &stripe.BalanceTransactionListParams{ListParams: params} expandBalanceTransactionParams(transactionParams) itr := c.balanceTransactionClient.List(transactionParams) - return itr.BalanceTransactionList(), itr.Err() + return itr.BalanceTransactionList(), wrapSDKErr(itr.Err()) }) if err != nil { return results, timeline, false, err @@ -69,7 +69,7 @@ func (c *client) GetPayments( itr := c.balanceTransactionClient.List(params) results = append(results, itr.BalanceTransactionList().Data...) timeline.LatestID = results[len(results)-1].ID - return results, timeline, itr.BalanceTransactionList().ListMeta.HasMore, itr.Err() + return results, timeline, itr.BalanceTransactionList().ListMeta.HasMore, wrapSDKErr(itr.Err()) } func expandBalanceTransactionParams(params *stripe.BalanceTransactionListParams) {