From 6e80284a45b58fcc040c2af92229e9f638525bb0 Mon Sep 17 00:00:00 2001 From: Martin Hansen Date: Mon, 16 Sep 2024 19:18:48 +0200 Subject: [PATCH] fix(nordigen): skip account on rate limit exceeded Crude way to fail gracefully when the rate limit is exceeded. Ref: #76 commit-id:25e9d0c8 --- reader/nordigen/nordigen.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/reader/nordigen/nordigen.go b/reader/nordigen/nordigen.go index 9562e53..224b467 100644 --- a/reader/nordigen/nordigen.go +++ b/reader/nordigen/nordigen.go @@ -1,6 +1,7 @@ package nordigen import ( + "errors" "fmt" "log/slog" "regexp" @@ -10,6 +11,8 @@ import ( "github.com/martinohansen/ynabber" ) +const rateLimitExceededStatusCode = 429 + type Reader struct { Config *ynabber.Config Client *nordigen.Client @@ -75,13 +78,13 @@ func (r Reader) Bulk() (t []ynabber.Transaction, err error) { return nil, fmt.Errorf("failed to authorize: %w", err) } - r.logger.Info("bulk reading", "accounts", len(req.Accounts)) + r.logger.Info("", "accounts", len(req.Accounts)) for _, account := range req.Accounts { - logger := r.logger.With("account", account) accountMetadata, err := r.Client.GetAccountMetadata(account) if err != nil { return nil, fmt.Errorf("failed to get account metadata: %w", err) } + logger := r.logger.With("iban", accountMetadata.Iban) // Handle expired, or suspended accounts by recreating the // requisition. @@ -100,6 +103,11 @@ func (r Reader) Bulk() (t []ynabber.Transaction, err error) { logger.Info("reading transactions") transactions, err := r.Client.GetAccountTransactions(string(account.ID)) if err != nil { + var apiErr *nordigen.APIError + if errors.As(err, &apiErr) && apiErr.StatusCode == rateLimitExceededStatusCode { + logger.Warn("rate limit exceeded, skipping account") + continue + } return nil, fmt.Errorf("failed to get transactions: %w", err) }