Skip to content

Commit

Permalink
feat(payments): wrap Stripe SDK errors so we can detect http client-s…
Browse files Browse the repository at this point in the history
…ide issues and assign an appropriate gpc error code
  • Loading branch information
laouji committed Oct 4, 2024
1 parent 02c7a56 commit 1e307a5
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 22 deletions.
4 changes: 2 additions & 2 deletions internal/connectors/plugins/public/stripe/client/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
22 changes: 22 additions & 0 deletions internal/connectors/plugins/public/stripe/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
16 changes: 0 additions & 16 deletions internal/connectors/plugins/public/stripe/client/error.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/connectors/plugins/public/stripe/client/payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1e307a5

Please sign in to comment.