diff --git a/components/payments/internal/app/api/accounts.go b/components/payments/internal/app/api/accounts.go index 72877b79ce..2b4a8b3113 100644 --- a/components/payments/internal/app/api/accounts.go +++ b/components/payments/internal/app/api/accounts.go @@ -76,7 +76,7 @@ func listAccountsHandler(repo accountsRepository) http.HandlerFunc { ret, paginationDetails, err := repo.ListAccounts(r.Context(), pagination) if err != nil { - handleServerError(w, r, err) + handleStorageErrors(w, r, err) return } diff --git a/components/payments/internal/app/api/module.go b/components/payments/internal/app/api/module.go index d2ae0fd371..7bc8ed27d9 100644 --- a/components/payments/internal/app/api/module.go +++ b/components/payments/internal/app/api/module.go @@ -3,23 +3,23 @@ package api import ( "context" "encoding/json" + "errors" "net/http" "runtime/debug" "strconv" - "github.com/formancehq/stack/libs/go-libs/api" - "github.com/formancehq/stack/libs/go-libs/httpserver" - "github.com/formancehq/stack/libs/go-libs/logging" - "github.com/formancehq/payments/internal/app/connectors/bankingcircle" "github.com/formancehq/payments/internal/app/connectors/currencycloud" - "github.com/formancehq/payments/internal/app/connectors/mangopay" - "github.com/formancehq/payments/internal/app/connectors/moneycorp" - "github.com/formancehq/payments/internal/app/connectors/dummypay" + "github.com/formancehq/payments/internal/app/connectors/mangopay" "github.com/formancehq/payments/internal/app/connectors/modulr" + "github.com/formancehq/payments/internal/app/connectors/moneycorp" "github.com/formancehq/payments/internal/app/connectors/stripe" "github.com/formancehq/payments/internal/app/connectors/wise" + "github.com/formancehq/payments/internal/app/storage" + "github.com/formancehq/stack/libs/go-libs/api" + "github.com/formancehq/stack/libs/go-libs/httpserver" + "github.com/formancehq/stack/libs/go-libs/logging" "github.com/formancehq/stack/libs/go-libs/otlp" "github.com/gorilla/mux" "github.com/rs/cors" @@ -75,6 +75,14 @@ func httpServeFunc(handler http.Handler) http.Handler { }) } +func handleStorageErrors(w http.ResponseWriter, r *http.Request, err error) { + if errors.Is(err, storage.ErrNotFound) { + handleNotFoundError(w, r, err) + } else { + handleServerError(w, r, err) + } +} + func handleServerError(w http.ResponseWriter, r *http.Request, err error) { w.WriteHeader(http.StatusInternalServerError) logging.FromContext(r.Context()).Error(err) @@ -88,6 +96,10 @@ func handleServerError(w http.ResponseWriter, r *http.Request, err error) { } } +func handleNotFoundError(w http.ResponseWriter, r *http.Request, err error) { + api.NotFound(w) +} + func handleValidationError(w http.ResponseWriter, r *http.Request, err error) { w.WriteHeader(http.StatusBadRequest) logging.FromContext(r.Context()).Error(err) diff --git a/components/payments/internal/app/api/payments.go b/components/payments/internal/app/api/payments.go index bc1e2c2f65..c3600d096e 100644 --- a/components/payments/internal/app/api/payments.go +++ b/components/payments/internal/app/api/payments.go @@ -92,7 +92,7 @@ func listPaymentsHandler(repo listPaymentsRepository) http.HandlerFunc { ret, paginationDetails, err := repo.ListPayments(r.Context(), pagination) if err != nil { - handleServerError(w, r, err) + handleStorageErrors(w, r, err) return } @@ -170,7 +170,7 @@ func readPaymentHandler(repo readPaymentRepository) http.HandlerFunc { payment, err := repo.GetPayment(r.Context(), paymentID) if err != nil { - handleServerError(w, r, err) + handleStorageErrors(w, r, err) return } diff --git a/components/payments/internal/app/storage/accounts.go b/components/payments/internal/app/storage/accounts.go index 6445a5383f..6908d265f7 100644 --- a/components/payments/internal/app/storage/accounts.go +++ b/components/payments/internal/app/storage/accounts.go @@ -2,7 +2,6 @@ package storage import ( "context" - "fmt" "sort" "time" @@ -49,7 +48,7 @@ func (s *Storage) ListAccounts(ctx context.Context, pagination Paginator) ([]*mo err := query.Scan(ctx) if err != nil { - return nil, PaginationDetails{}, fmt.Errorf("failed to list payments: %w", err) + return nil, PaginationDetails{}, e("failed to list payments", err) } var ( @@ -78,13 +77,13 @@ func (s *Storage) ListAccounts(ctx context.Context, pagination Paginator) ([]*mo hasPrevious, err = pagination.hasPrevious(ctx, query, "account.created_at", firstReference) if err != nil { - return nil, PaginationDetails{}, fmt.Errorf("failed to check if there is a previous page: %w", err) + return nil, PaginationDetails{}, e("failed to check if there is a previous page", err) } } paginationDetails, err := pagination.paginationDetails(hasMore, hasPrevious, firstReference, lastReference) if err != nil { - return nil, PaginationDetails{}, fmt.Errorf("failed to get pagination details: %w", err) + return nil, PaginationDetails{}, e("failed to get pagination details", err) } return accounts, paginationDetails, nil diff --git a/components/payments/internal/app/storage/connectors.go b/components/payments/internal/app/storage/connectors.go index 1ce82a9433..b76adecbb4 100644 --- a/components/payments/internal/app/storage/connectors.go +++ b/components/payments/internal/app/storage/connectors.go @@ -31,12 +31,12 @@ func (s *Storage) GetConfig(ctx context.Context, connectorProvider models.Connec Where("provider = ?", connectorProvider). Scan(ctx) if err != nil { - return fmt.Errorf("failed to get config for connector %s: %w", connectorProvider, err) + return e(fmt.Sprintf("failed to get config for connector %s", connectorProvider), err) } err = json.Unmarshal(connector.Config, destination) if err != nil { - return fmt.Errorf("failed to unmarshal config for connector %s: %w", connectorProvider, err) + return e(fmt.Sprintf("failed to unmarshal config for connector %s", connectorProvider), err) } return nil diff --git a/components/payments/internal/app/storage/error.go b/components/payments/internal/app/storage/error.go index 20de1f25e2..10d8b5d19d 100644 --- a/components/payments/internal/app/storage/error.go +++ b/components/payments/internal/app/storage/error.go @@ -15,7 +15,7 @@ func e(msg string, err error) error { } if errors.Is(err, sql.ErrNoRows) { - return fmt.Errorf("%s: %w", msg, ErrNotFound) + return ErrNotFound } return fmt.Errorf("%s: %w", msg, err) diff --git a/components/payments/internal/app/storage/paginate.go b/components/payments/internal/app/storage/paginate.go index 202de7fab1..2337fbb2d6 100644 --- a/components/payments/internal/app/storage/paginate.go +++ b/components/payments/internal/app/storage/paginate.go @@ -109,7 +109,7 @@ func (p Paginator) hasPrevious(ctx context.Context, query *bun.SelectQuery, colu exists, err := query.Exists(ctx) if err != nil { - return false, fmt.Errorf("error checking if previous page exists: %w", err) + return false, e("error checking if previous page exists", err) } return exists, nil diff --git a/components/payments/internal/app/storage/payments.go b/components/payments/internal/app/storage/payments.go index bd34cd17c4..c49fe2f4de 100644 --- a/components/payments/internal/app/storage/payments.go +++ b/components/payments/internal/app/storage/payments.go @@ -74,7 +74,7 @@ func (s *Storage) GetPayment(ctx context.Context, id string) (*models.Payment, e Where("payment.id = ?", id). Scan(ctx) if err != nil { - return nil, fmt.Errorf("failed to get payment %s: %w", id, err) + return nil, e(fmt.Sprintf("failed to get payment %s", id), err) } return &payment, nil diff --git a/components/payments/internal/app/storage/task.go b/components/payments/internal/app/storage/task.go index 5df5475548..89e983d62e 100644 --- a/components/payments/internal/app/storage/task.go +++ b/components/payments/internal/app/storage/task.go @@ -3,7 +3,6 @@ package storage import ( "context" "encoding/json" - "fmt" "sort" "time" @@ -162,13 +161,13 @@ func (s *Storage) ListTasks(ctx context.Context, provider models.ConnectorProvid hasPrevious, err = pagination.hasPrevious(ctx, query, "task.created_at", firstReference) if err != nil { - return nil, PaginationDetails{}, fmt.Errorf("failed to check if there is a previous page: %w", err) + return nil, PaginationDetails{}, e("failed to check if there is a previous page", err) } } paginationDetails, err := pagination.paginationDetails(hasMore, hasPrevious, firstReference, lastReference) if err != nil { - return nil, PaginationDetails{}, fmt.Errorf("failed to get pagination details: %w", err) + return nil, PaginationDetails{}, e("failed to get pagination details", err) } return tasks, paginationDetails, nil