Skip to content

Commit

Permalink
feat: handle not found errors from payments storage (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas authored Aug 2, 2023
1 parent 7d94e7a commit 508e566
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion components/payments/internal/app/api/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
26 changes: 19 additions & 7 deletions components/payments/internal/app/api/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions components/payments/internal/app/api/payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 3 additions & 4 deletions components/payments/internal/app/storage/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package storage

import (
"context"
"fmt"
"sort"
"time"

Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions components/payments/internal/app/storage/connectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion components/payments/internal/app/storage/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion components/payments/internal/app/storage/paginate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion components/payments/internal/app/storage/payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions components/payments/internal/app/storage/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package storage
import (
"context"
"encoding/json"
"fmt"
"sort"
"time"

Expand Down Expand Up @@ -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
Expand Down

1 comment on commit 508e566

@vercel
Copy link

@vercel vercel bot commented on 508e566 Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.