From bcc61b1693cc6f7108a989f4c29ad2125272ae21 Mon Sep 17 00:00:00 2001 From: Geoffrey Ragot Date: Thu, 10 Oct 2024 21:28:23 +0200 Subject: [PATCH] feat: factorize error handling on api --- internal/api/common/errors.go | 17 +++++++++++++++++ .../api/v1/controllers_accounts_add_metadata.go | 8 +------- internal/api/v1/controllers_accounts_count.go | 5 +---- .../v1/controllers_accounts_delete_metadata.go | 9 +-------- internal/api/v1/controllers_accounts_list.go | 5 +---- internal/api/v1/controllers_accounts_read.go | 5 +---- .../api/v1/controllers_balances_aggregates.go | 9 +-------- internal/api/v1/controllers_balances_list.go | 9 +-------- internal/api/v1/controllers_config.go | 3 ++- internal/api/v1/controllers_info.go | 9 +-------- internal/api/v1/controllers_logs_list.go | 9 +-------- internal/api/v1/controllers_stats.go | 9 +-------- .../v1/controllers_transactions_add_metadata.go | 5 +---- .../api/v1/controllers_transactions_count.go | 9 +-------- .../api/v1/controllers_transactions_create.go | 9 ++------- .../controllers_transactions_delete_metadata.go | 5 +---- .../api/v1/controllers_transactions_list.go | 9 +-------- .../api/v1/controllers_transactions_read.go | 5 +---- .../api/v1/controllers_transactions_revert.go | 5 +---- .../api/v2/controllers_accounts_add_metadata.go | 8 +------- internal/api/v2/controllers_accounts_count.go | 5 +---- .../v2/controllers_accounts_delete_metadata.go | 9 +-------- internal/api/v2/controllers_accounts_list.go | 5 +---- internal/api/v2/controllers_accounts_read.go | 5 +---- internal/api/v2/controllers_balances.go | 5 +---- internal/api/v2/controllers_ledgers_create.go | 6 ++---- .../v2/controllers_ledgers_delete_metadata.go | 10 ++-------- internal/api/v2/controllers_ledgers_info.go | 9 +-------- internal/api/v2/controllers_ledgers_list.go | 6 ++---- internal/api/v2/controllers_ledgers_read.go | 6 ++---- .../v2/controllers_ledgers_update_metadata.go | 9 ++------- internal/api/v2/controllers_logs_export.go | 10 +--------- internal/api/v2/controllers_logs_import.go | 5 +---- internal/api/v2/controllers_logs_list.go | 5 +---- internal/api/v2/controllers_stats.go | 9 +-------- .../v2/controllers_transactions_add_metadata.go | 5 +---- .../api/v2/controllers_transactions_count.go | 5 +---- .../api/v2/controllers_transactions_create.go | 5 +---- .../controllers_transactions_delete_metadata.go | 5 +---- .../api/v2/controllers_transactions_list.go | 5 +---- .../api/v2/controllers_transactions_read.go | 5 +---- .../api/v2/controllers_transactions_revert.go | 5 +---- internal/api/v2/controllers_volumes.go | 5 +---- 43 files changed, 66 insertions(+), 230 deletions(-) create mode 100644 internal/api/common/errors.go diff --git a/internal/api/common/errors.go b/internal/api/common/errors.go new file mode 100644 index 000000000..2f5bbae43 --- /dev/null +++ b/internal/api/common/errors.go @@ -0,0 +1,17 @@ +package common + +import ( + "errors" + "github.com/formancehq/go-libs/api" + "github.com/formancehq/go-libs/platform/postgres" + "net/http" +) + +func HandleCommonErrors(w http.ResponseWriter, r *http.Request, err error) { + switch { + case errors.Is(err, postgres.ErrTooManyClient{}): + api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) + default: + api.InternalServerError(w, r, err) + } +} diff --git a/internal/api/v1/controllers_accounts_add_metadata.go b/internal/api/v1/controllers_accounts_add_metadata.go index 50a3687c3..5c68acaca 100644 --- a/internal/api/v1/controllers_accounts_add_metadata.go +++ b/internal/api/v1/controllers_accounts_add_metadata.go @@ -2,7 +2,6 @@ package v1 import ( "encoding/json" - "github.com/formancehq/go-libs/platform/postgres" "github.com/formancehq/ledger/internal/controller/ledger" "github.com/formancehq/ledger/pkg/accounts" "net/http" @@ -39,12 +38,7 @@ func addAccountMetadata(w http.ResponseWriter, r *http.Request) { Metadata: m, })) if err != nil { - switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) - default: - api.InternalServerError(w, r, err) - } + common.HandleCommonErrors(w, r, err) return } diff --git a/internal/api/v1/controllers_accounts_count.go b/internal/api/v1/controllers_accounts_count.go index 991b00fc1..c232083f0 100644 --- a/internal/api/v1/controllers_accounts_count.go +++ b/internal/api/v1/controllers_accounts_count.go @@ -2,7 +2,6 @@ package v1 import ( "fmt" - "github.com/formancehq/go-libs/platform/postgres" "net/http" "errors" @@ -35,12 +34,10 @@ func countAccounts(w http.ResponseWriter, r *http.Request) { count, err := l.CountAccounts(r.Context(), *query) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, ledgercontroller.ErrInvalidQuery{}) || errors.Is(err, ledgercontroller.ErrMissingFeature{}): api.BadRequest(w, ErrValidation, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v1/controllers_accounts_delete_metadata.go b/internal/api/v1/controllers_accounts_delete_metadata.go index 9f7fccc06..4f801bb14 100644 --- a/internal/api/v1/controllers_accounts_delete_metadata.go +++ b/internal/api/v1/controllers_accounts_delete_metadata.go @@ -1,8 +1,6 @@ package v1 import ( - "errors" - "github.com/formancehq/go-libs/platform/postgres" "github.com/formancehq/ledger/internal/controller/ledger" "net/http" "net/url" @@ -27,12 +25,7 @@ func deleteAccountMetadata(w http.ResponseWriter, r *http.Request) { Key: chi.URLParam(r, "key"), }), ); err != nil { - switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) - default: - api.InternalServerError(w, r, err) - } + common.HandleCommonErrors(w, r, err) return } diff --git a/internal/api/v1/controllers_accounts_list.go b/internal/api/v1/controllers_accounts_list.go index 495986e70..990daa3de 100644 --- a/internal/api/v1/controllers_accounts_list.go +++ b/internal/api/v1/controllers_accounts_list.go @@ -1,7 +1,6 @@ package v1 import ( - "github.com/formancehq/go-libs/platform/postgres" "net/http" "errors" @@ -34,12 +33,10 @@ func listAccounts(w http.ResponseWriter, r *http.Request) { cursor, err := l.ListAccounts(r.Context(), *query) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, ledgercontroller.ErrMissingFeature{}): api.BadRequest(w, ErrValidation, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v1/controllers_accounts_read.go b/internal/api/v1/controllers_accounts_read.go index 644bd51c0..4b450bc66 100644 --- a/internal/api/v1/controllers_accounts_read.go +++ b/internal/api/v1/controllers_accounts_read.go @@ -1,7 +1,6 @@ package v1 import ( - "errors" "net/http" "net/url" @@ -29,8 +28,6 @@ func getAccount(w http.ResponseWriter, r *http.Request) { acc, err := l.GetAccount(r.Context(), query) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case postgres.IsNotFoundError(err): acc = &ledger.Account{ Address: address, @@ -39,7 +36,7 @@ func getAccount(w http.ResponseWriter, r *http.Request) { EffectiveVolumes: ledger.VolumesByAssets{}, } default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) return } } diff --git a/internal/api/v1/controllers_balances_aggregates.go b/internal/api/v1/controllers_balances_aggregates.go index ce451b5cf..8649d5021 100644 --- a/internal/api/v1/controllers_balances_aggregates.go +++ b/internal/api/v1/controllers_balances_aggregates.go @@ -1,8 +1,6 @@ package v1 import ( - "errors" - "github.com/formancehq/go-libs/platform/postgres" "net/http" "github.com/formancehq/go-libs/api" @@ -39,12 +37,7 @@ func getBalancesAggregated(w http.ResponseWriter, r *http.Request) { balances, err := common.LedgerFromContext(r.Context()).GetAggregatedBalances(r.Context(), query) if err != nil { - switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) - default: - api.InternalServerError(w, r, err) - } + common.HandleCommonErrors(w, r, err) return } diff --git a/internal/api/v1/controllers_balances_list.go b/internal/api/v1/controllers_balances_list.go index 8597b75f9..272c5f7e7 100644 --- a/internal/api/v1/controllers_balances_list.go +++ b/internal/api/v1/controllers_balances_list.go @@ -1,8 +1,6 @@ package v1 import ( - "errors" - "github.com/formancehq/go-libs/platform/postgres" "math/big" "net/http" @@ -34,12 +32,7 @@ func getBalances(w http.ResponseWriter, r *http.Request) { cursor, err := l.ListAccounts(r.Context(), q.WithExpandVolumes()) if err != nil { - switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) - default: - api.InternalServerError(w, r, err) - } + common.HandleCommonErrors(w, r, err) return } diff --git a/internal/api/v1/controllers_config.go b/internal/api/v1/controllers_config.go index caff56771..572b7f037 100644 --- a/internal/api/v1/controllers_config.go +++ b/internal/api/v1/controllers_config.go @@ -3,6 +3,7 @@ package v1 import ( "context" _ "embed" + "github.com/formancehq/ledger/internal/api/common" "net/http" ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger" @@ -46,7 +47,7 @@ func getInfo(systemController system.Controller, version string) func(w http.Res return nil }, ); err != nil { - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) return } diff --git a/internal/api/v1/controllers_info.go b/internal/api/v1/controllers_info.go index 8d6efef73..09c53d0c6 100644 --- a/internal/api/v1/controllers_info.go +++ b/internal/api/v1/controllers_info.go @@ -1,8 +1,6 @@ package v1 import ( - "errors" - "github.com/formancehq/go-libs/platform/postgres" "net/http" "github.com/go-chi/chi/v5" @@ -31,12 +29,7 @@ func getLedgerInfo(w http.ResponseWriter, r *http.Request) { } res.Storage.Migrations, err = ledger.GetMigrationsInfo(r.Context()) if err != nil { - switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) - default: - api.InternalServerError(w, r, err) - } + common.HandleCommonErrors(w, r, err) return } diff --git a/internal/api/v1/controllers_logs_list.go b/internal/api/v1/controllers_logs_list.go index 12e344e21..bed579997 100644 --- a/internal/api/v1/controllers_logs_list.go +++ b/internal/api/v1/controllers_logs_list.go @@ -1,9 +1,7 @@ package v1 import ( - "errors" "fmt" - "github.com/formancehq/go-libs/platform/postgres" "net/http" "github.com/formancehq/go-libs/api" @@ -75,12 +73,7 @@ func getLogs(w http.ResponseWriter, r *http.Request) { cursor, err := l.ListLogs(r.Context(), query) if err != nil { - switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) - default: - api.InternalServerError(w, r, err) - } + common.HandleCommonErrors(w, r, err) return } diff --git a/internal/api/v1/controllers_stats.go b/internal/api/v1/controllers_stats.go index 162cbe832..e83a6209b 100644 --- a/internal/api/v1/controllers_stats.go +++ b/internal/api/v1/controllers_stats.go @@ -1,8 +1,6 @@ package v1 import ( - "errors" - "github.com/formancehq/go-libs/platform/postgres" "net/http" "github.com/formancehq/go-libs/api" @@ -14,12 +12,7 @@ func getStats(w http.ResponseWriter, r *http.Request) { stats, err := l.GetStats(r.Context()) if err != nil { - switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) - default: - api.InternalServerError(w, r, err) - } + common.HandleCommonErrors(w, r, err) return } diff --git a/internal/api/v1/controllers_transactions_add_metadata.go b/internal/api/v1/controllers_transactions_add_metadata.go index 46aee088f..be8489c7f 100644 --- a/internal/api/v1/controllers_transactions_add_metadata.go +++ b/internal/api/v1/controllers_transactions_add_metadata.go @@ -2,7 +2,6 @@ package v1 import ( "encoding/json" - "github.com/formancehq/go-libs/platform/postgres" "net/http" "strconv" @@ -35,12 +34,10 @@ func addTransactionMetadata(w http.ResponseWriter, r *http.Request) { Metadata: m, })); err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, ledgercontroller.ErrNotFound): api.NotFound(w, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v1/controllers_transactions_count.go b/internal/api/v1/controllers_transactions_count.go index 2740b1c84..d9d562929 100644 --- a/internal/api/v1/controllers_transactions_count.go +++ b/internal/api/v1/controllers_transactions_count.go @@ -1,9 +1,7 @@ package v1 import ( - "errors" "fmt" - "github.com/formancehq/go-libs/platform/postgres" "net/http" "github.com/formancehq/go-libs/api" @@ -27,12 +25,7 @@ func countTransactions(w http.ResponseWriter, r *http.Request) { count, err := common.LedgerFromContext(r.Context()). CountTransactions(r.Context(), ledgercontroller.NewListTransactionsQuery(*options)) if err != nil { - switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) - default: - api.InternalServerError(w, r, err) - } + common.HandleCommonErrors(w, r, err) return } diff --git a/internal/api/v1/controllers_transactions_create.go b/internal/api/v1/controllers_transactions_create.go index 95e6c5974..bc11dd945 100644 --- a/internal/api/v1/controllers_transactions_create.go +++ b/internal/api/v1/controllers_transactions_create.go @@ -3,7 +3,6 @@ package v1 import ( "encoding/json" "fmt" - "github.com/formancehq/go-libs/platform/postgres" "math/big" "net/http" @@ -87,8 +86,6 @@ func createTransaction(w http.ResponseWriter, r *http.Request) { res, err := l.CreateTransaction(r.Context(), getCommandParameters(r, common.TxToScriptData(txData, false))) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, &ledgercontroller.ErrInsufficientFunds{}): api.BadRequest(w, ErrInsufficientFund, err) case errors.Is(err, &ledgercontroller.ErrInvalidVars{}) || errors.Is(err, ledgercontroller.ErrCompilationFailed{}): @@ -101,7 +98,7 @@ func createTransaction(w http.ResponseWriter, r *http.Request) { case errors.Is(err, ledgercontroller.ErrTransactionReferenceConflict{}): api.WriteErrorResponse(w, http.StatusConflict, ErrConflict, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } @@ -125,8 +122,6 @@ func createTransaction(w http.ResponseWriter, r *http.Request) { res, err := l.CreateTransaction(r.Context(), getCommandParameters(r, runScript)) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, &ledgercontroller.ErrInsufficientFunds{}): api.BadRequest(w, ErrInsufficientFund, err) case errors.Is(err, &ledgercontroller.ErrInvalidVars{}) || @@ -138,7 +133,7 @@ func createTransaction(w http.ResponseWriter, r *http.Request) { case errors.Is(err, ledgercontroller.ErrTransactionReferenceConflict{}): api.WriteErrorResponse(w, http.StatusConflict, ErrConflict, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v1/controllers_transactions_delete_metadata.go b/internal/api/v1/controllers_transactions_delete_metadata.go index cb89a528d..7babff500 100644 --- a/internal/api/v1/controllers_transactions_delete_metadata.go +++ b/internal/api/v1/controllers_transactions_delete_metadata.go @@ -1,7 +1,6 @@ package v1 import ( - "github.com/formancehq/go-libs/platform/postgres" "net/http" "strconv" @@ -29,12 +28,10 @@ func deleteTransactionMetadata(w http.ResponseWriter, r *http.Request) { Key: metadataKey, })); err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, ledgercontroller.ErrNotFound): api.NotFound(w, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v1/controllers_transactions_list.go b/internal/api/v1/controllers_transactions_list.go index 818720a7e..c5a55002d 100644 --- a/internal/api/v1/controllers_transactions_list.go +++ b/internal/api/v1/controllers_transactions_list.go @@ -1,8 +1,6 @@ package v1 import ( - "errors" - "github.com/formancehq/go-libs/platform/postgres" "net/http" "github.com/formancehq/go-libs/api" @@ -33,12 +31,7 @@ func listTransactions(w http.ResponseWriter, r *http.Request) { cursor, err := l.ListTransactions(r.Context(), *query) if err != nil { - switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) - default: - api.InternalServerError(w, r, err) - } + common.HandleCommonErrors(w, r, err) return } diff --git a/internal/api/v1/controllers_transactions_read.go b/internal/api/v1/controllers_transactions_read.go index 807e9bcd9..23cf93f09 100644 --- a/internal/api/v1/controllers_transactions_read.go +++ b/internal/api/v1/controllers_transactions_read.go @@ -1,7 +1,6 @@ package v1 import ( - "errors" "net/http" "strconv" @@ -33,12 +32,10 @@ func readTransaction(w http.ResponseWriter, r *http.Request) { tx, err := l.GetTransaction(r.Context(), query) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case postgres.IsNotFoundError(err): api.NotFound(w, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v1/controllers_transactions_revert.go b/internal/api/v1/controllers_transactions_revert.go index 4150856ba..8fcb47f95 100644 --- a/internal/api/v1/controllers_transactions_revert.go +++ b/internal/api/v1/controllers_transactions_revert.go @@ -1,7 +1,6 @@ package v1 import ( - "github.com/formancehq/go-libs/platform/postgres" "net/http" "strconv" @@ -32,8 +31,6 @@ func revertTransaction(w http.ResponseWriter, r *http.Request) { ) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, &ledgercontroller.ErrInsufficientFunds{}): api.BadRequest(w, ErrInsufficientFund, err) case errors.Is(err, ledgercontroller.ErrAlreadyReverted{}): @@ -41,7 +38,7 @@ func revertTransaction(w http.ResponseWriter, r *http.Request) { case errors.Is(err, ledgercontroller.ErrNotFound): api.NotFound(w, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v2/controllers_accounts_add_metadata.go b/internal/api/v2/controllers_accounts_add_metadata.go index 0d05b3ae5..69456abf7 100644 --- a/internal/api/v2/controllers_accounts_add_metadata.go +++ b/internal/api/v2/controllers_accounts_add_metadata.go @@ -2,7 +2,6 @@ package v2 import ( "encoding/json" - "github.com/formancehq/go-libs/platform/postgres" "github.com/formancehq/ledger/internal/controller/ledger" "net/http" "net/url" @@ -34,12 +33,7 @@ func addAccountMetadata(w http.ResponseWriter, r *http.Request) { Metadata: m, })) if err != nil { - switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) - default: - api.InternalServerError(w, r, err) - } + common.HandleCommonErrors(w, r, err) return } diff --git a/internal/api/v2/controllers_accounts_count.go b/internal/api/v2/controllers_accounts_count.go index 3404e664c..0c2a34424 100644 --- a/internal/api/v2/controllers_accounts_count.go +++ b/internal/api/v2/controllers_accounts_count.go @@ -2,7 +2,6 @@ package v2 import ( "fmt" - "github.com/formancehq/go-libs/platform/postgres" "net/http" "errors" @@ -23,12 +22,10 @@ func countAccounts(w http.ResponseWriter, r *http.Request) { count, err := l.CountAccounts(r.Context(), ledgercontroller.NewListAccountsQuery(*options)) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, ledgercontroller.ErrInvalidQuery{}) || errors.Is(err, ledgercontroller.ErrMissingFeature{}): api.BadRequest(w, ErrValidation, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v2/controllers_accounts_delete_metadata.go b/internal/api/v2/controllers_accounts_delete_metadata.go index c0971bb21..6e59d2cfe 100644 --- a/internal/api/v2/controllers_accounts_delete_metadata.go +++ b/internal/api/v2/controllers_accounts_delete_metadata.go @@ -1,8 +1,6 @@ package v2 import ( - "errors" - "github.com/formancehq/go-libs/platform/postgres" "github.com/formancehq/ledger/internal/controller/ledger" "net/http" "net/url" @@ -28,12 +26,7 @@ func deleteAccountMetadata(w http.ResponseWriter, r *http.Request) { Key: chi.URLParam(r, "key"), }), ); err != nil { - switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) - default: - api.InternalServerError(w, r, err) - } + common.HandleCommonErrors(w, r, err) return } diff --git a/internal/api/v2/controllers_accounts_list.go b/internal/api/v2/controllers_accounts_list.go index 7e3c40733..002417c96 100644 --- a/internal/api/v2/controllers_accounts_list.go +++ b/internal/api/v2/controllers_accounts_list.go @@ -1,7 +1,6 @@ package v2 import ( - "github.com/formancehq/go-libs/platform/postgres" "net/http" "errors" @@ -30,12 +29,10 @@ func listAccounts(w http.ResponseWriter, r *http.Request) { cursor, err := l.ListAccounts(r.Context(), *query) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, ledgercontroller.ErrInvalidQuery{}) || errors.Is(err, ledgercontroller.ErrMissingFeature{}): api.BadRequest(w, ErrValidation, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v2/controllers_accounts_read.go b/internal/api/v2/controllers_accounts_read.go index a8bc58b72..09c20e86f 100644 --- a/internal/api/v2/controllers_accounts_read.go +++ b/internal/api/v2/controllers_accounts_read.go @@ -1,7 +1,6 @@ package v2 import ( - "errors" "net/http" "net/url" @@ -38,12 +37,10 @@ func readAccount(w http.ResponseWriter, r *http.Request) { acc, err := l.GetAccount(r.Context(), query) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case postgres.IsNotFoundError(err): api.NotFound(w, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v2/controllers_balances.go b/internal/api/v2/controllers_balances.go index 740e2820a..ed42bea86 100644 --- a/internal/api/v2/controllers_balances.go +++ b/internal/api/v2/controllers_balances.go @@ -1,7 +1,6 @@ package v2 import ( - "github.com/formancehq/go-libs/platform/postgres" "net/http" "errors" @@ -30,12 +29,10 @@ func readBalancesAggregated(w http.ResponseWriter, r *http.Request) { *pitFilter, queryBuilder, api.QueryParamBool(r, "use_insertion_date") || api.QueryParamBool(r, "useInsertionDate"))) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, ledgercontroller.ErrInvalidQuery{}) || errors.Is(err, ledgercontroller.ErrMissingFeature{}): api.BadRequest(w, ErrValidation, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v2/controllers_ledgers_create.go b/internal/api/v2/controllers_ledgers_create.go index 47ea6842b..754625c3e 100644 --- a/internal/api/v2/controllers_ledgers_create.go +++ b/internal/api/v2/controllers_ledgers_create.go @@ -2,7 +2,7 @@ package v2 import ( "encoding/json" - "github.com/formancehq/go-libs/platform/postgres" + "github.com/formancehq/ledger/internal/api/common" "io" "net/http" @@ -33,15 +33,13 @@ func createLedger(systemController system.Controller) http.HandlerFunc { if err := systemController.CreateLedger(r.Context(), chi.URLParam(r, "ledger"), configuration); err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, system.ErrLedgerAlreadyExists): api.BadRequest(w, ErrValidation, err) case errors.Is(err, ledger.ErrInvalidLedgerName{}) || errors.Is(err, ledger.ErrInvalidBucketName{}): api.BadRequest(w, ErrValidation, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v2/controllers_ledgers_delete_metadata.go b/internal/api/v2/controllers_ledgers_delete_metadata.go index 1bbbae69a..0199e03ab 100644 --- a/internal/api/v2/controllers_ledgers_delete_metadata.go +++ b/internal/api/v2/controllers_ledgers_delete_metadata.go @@ -1,8 +1,7 @@ package v2 import ( - "errors" - "github.com/formancehq/go-libs/platform/postgres" + "github.com/formancehq/ledger/internal/api/common" "net/http" "github.com/formancehq/ledger/internal/controller/system" @@ -14,12 +13,7 @@ import ( func deleteLedgerMetadata(b system.Controller) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if err := b.DeleteLedgerMetadata(r.Context(), chi.URLParam(r, "ledger"), chi.URLParam(r, "key")); err != nil { - switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) - default: - api.InternalServerError(w, r, err) - } + common.HandleCommonErrors(w, r, err) return } diff --git a/internal/api/v2/controllers_ledgers_info.go b/internal/api/v2/controllers_ledgers_info.go index 1cac87d11..a9bc9b93d 100644 --- a/internal/api/v2/controllers_ledgers_info.go +++ b/internal/api/v2/controllers_ledgers_info.go @@ -1,8 +1,6 @@ package v2 import ( - "errors" - "github.com/formancehq/go-libs/platform/postgres" "net/http" "github.com/go-chi/chi/v5" @@ -31,12 +29,7 @@ func getLedgerInfo(w http.ResponseWriter, r *http.Request) { } res.Storage.Migrations, err = ledger.GetMigrationsInfo(r.Context()) if err != nil { - switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) - default: - api.InternalServerError(w, r, err) - } + common.HandleCommonErrors(w, r, err) return } diff --git a/internal/api/v2/controllers_ledgers_list.go b/internal/api/v2/controllers_ledgers_list.go index 843b53450..372e70e46 100644 --- a/internal/api/v2/controllers_ledgers_list.go +++ b/internal/api/v2/controllers_ledgers_list.go @@ -1,7 +1,7 @@ package v2 import ( - "github.com/formancehq/go-libs/platform/postgres" + "github.com/formancehq/ledger/internal/api/common" "net/http" "errors" @@ -31,12 +31,10 @@ func listLedgers(b system.Controller) http.HandlerFunc { ledgers, err := b.ListLedgers(r.Context(), *query) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, ledgercontroller.ErrInvalidQuery{}) || errors.Is(err, ledgercontroller.ErrMissingFeature{}): api.BadRequest(w, ErrValidation, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v2/controllers_ledgers_read.go b/internal/api/v2/controllers_ledgers_read.go index fcd317e31..ddbf16031 100644 --- a/internal/api/v2/controllers_ledgers_read.go +++ b/internal/api/v2/controllers_ledgers_read.go @@ -1,7 +1,7 @@ package v2 import ( - "errors" + "github.com/formancehq/ledger/internal/api/common" "net/http" "github.com/formancehq/ledger/internal/controller/system" @@ -17,12 +17,10 @@ func readLedger(b system.Controller) http.HandlerFunc { ledger, err := b.GetLedger(r.Context(), chi.URLParam(r, "ledger")) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case postgres.IsNotFoundError(err): api.NotFound(w, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v2/controllers_ledgers_update_metadata.go b/internal/api/v2/controllers_ledgers_update_metadata.go index 80b595bc3..e9bde54c9 100644 --- a/internal/api/v2/controllers_ledgers_update_metadata.go +++ b/internal/api/v2/controllers_ledgers_update_metadata.go @@ -2,7 +2,7 @@ package v2 import ( "encoding/json" - "github.com/formancehq/go-libs/platform/postgres" + "github.com/formancehq/ledger/internal/api/common" "net/http" "errors" @@ -22,12 +22,7 @@ func updateLedgerMetadata(systemController systemcontroller.Controller) http.Han } if err := systemController.UpdateLedgerMetadata(r.Context(), chi.URLParam(r, "ledger"), m); err != nil { - switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) - default: - api.InternalServerError(w, r, err) - } + common.HandleCommonErrors(w, r, err) return } diff --git a/internal/api/v2/controllers_logs_export.go b/internal/api/v2/controllers_logs_export.go index d5ba02061..b9a0bf299 100644 --- a/internal/api/v2/controllers_logs_export.go +++ b/internal/api/v2/controllers_logs_export.go @@ -3,13 +3,10 @@ package v2 import ( "context" "encoding/json" - "errors" - "github.com/formancehq/go-libs/platform/postgres" "net/http" ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger" - "github.com/formancehq/go-libs/api" ledger "github.com/formancehq/ledger/internal" "github.com/formancehq/ledger/internal/api/common" ) @@ -20,12 +17,7 @@ func exportLogs(w http.ResponseWriter, r *http.Request) { if err := common.LedgerFromContext(r.Context()).Export(r.Context(), ledgercontroller.ExportWriterFn(func(ctx context.Context, log ledger.Log) error { return enc.Encode(log) })); err != nil { - switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) - default: - api.InternalServerError(w, r, err) - } + common.HandleCommonErrors(w, r, err) return } } diff --git a/internal/api/v2/controllers_logs_import.go b/internal/api/v2/controllers_logs_import.go index 0437f7f04..3f2edc396 100644 --- a/internal/api/v2/controllers_logs_import.go +++ b/internal/api/v2/controllers_logs_import.go @@ -2,7 +2,6 @@ package v2 import ( "encoding/json" - "github.com/formancehq/go-libs/platform/postgres" "io" "net/http" @@ -24,12 +23,10 @@ func importLogs(w http.ResponseWriter, r *http.Request) { dec := json.NewDecoder(r.Body) handleError := func(err error) { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, ledgercontroller.ErrImport{}): api.BadRequest(w, "IMPORT", err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } } for { diff --git a/internal/api/v2/controllers_logs_list.go b/internal/api/v2/controllers_logs_list.go index 21e65b298..72768893f 100644 --- a/internal/api/v2/controllers_logs_list.go +++ b/internal/api/v2/controllers_logs_list.go @@ -2,7 +2,6 @@ package v2 import ( "fmt" - "github.com/formancehq/go-libs/platform/postgres" "net/http" "errors" @@ -48,12 +47,10 @@ func listLogs(w http.ResponseWriter, r *http.Request) { cursor, err := l.ListLogs(r.Context(), query) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, ledgercontroller.ErrInvalidQuery{}): api.BadRequest(w, ErrValidation, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v2/controllers_stats.go b/internal/api/v2/controllers_stats.go index 055a8d52b..be5d983e4 100644 --- a/internal/api/v2/controllers_stats.go +++ b/internal/api/v2/controllers_stats.go @@ -1,8 +1,6 @@ package v2 import ( - "errors" - "github.com/formancehq/go-libs/platform/postgres" "net/http" "github.com/formancehq/go-libs/api" @@ -14,12 +12,7 @@ func readStats(w http.ResponseWriter, r *http.Request) { stats, err := l.GetStats(r.Context()) if err != nil { - switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) - default: - api.InternalServerError(w, r, err) - } + common.HandleCommonErrors(w, r, err) return } diff --git a/internal/api/v2/controllers_transactions_add_metadata.go b/internal/api/v2/controllers_transactions_add_metadata.go index e05ceeb5b..c08738d0a 100644 --- a/internal/api/v2/controllers_transactions_add_metadata.go +++ b/internal/api/v2/controllers_transactions_add_metadata.go @@ -2,7 +2,6 @@ package v2 import ( "encoding/json" - "github.com/formancehq/go-libs/platform/postgres" "net/http" "strconv" @@ -35,12 +34,10 @@ func addTransactionMetadata(w http.ResponseWriter, r *http.Request) { Metadata: m, })); err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, ledgercontroller.ErrNotFound): api.NotFound(w, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v2/controllers_transactions_count.go b/internal/api/v2/controllers_transactions_count.go index 5ea259507..04ad971ab 100644 --- a/internal/api/v2/controllers_transactions_count.go +++ b/internal/api/v2/controllers_transactions_count.go @@ -2,7 +2,6 @@ package v2 import ( "fmt" - "github.com/formancehq/go-libs/platform/postgres" "net/http" "errors" @@ -23,12 +22,10 @@ func countTransactions(w http.ResponseWriter, r *http.Request) { CountTransactions(r.Context(), ledgercontroller.NewListTransactionsQuery(*options)) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, ledgercontroller.ErrInvalidQuery{}) || errors.Is(err, ledgercontroller.ErrMissingFeature{}): api.BadRequest(w, ErrValidation, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v2/controllers_transactions_create.go b/internal/api/v2/controllers_transactions_create.go index 0ab65b95e..cfc679df5 100644 --- a/internal/api/v2/controllers_transactions_create.go +++ b/internal/api/v2/controllers_transactions_create.go @@ -2,7 +2,6 @@ package v2 import ( "encoding/json" - "github.com/formancehq/go-libs/platform/postgres" "net/http" ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger" @@ -34,8 +33,6 @@ func createTransaction(w http.ResponseWriter, r *http.Request) { res, err := l.CreateTransaction(r.Context(), getCommandParameters(r, *payload.ToRunScript(api.QueryParamBool(r, "force")))) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, &ledgercontroller.ErrInsufficientFunds{}): api.BadRequest(w, ErrInsufficientFund, err) case errors.Is(err, &ledgercontroller.ErrInvalidVars{}) || errors.Is(err, ledgercontroller.ErrCompilationFailed{}): @@ -49,7 +46,7 @@ func createTransaction(w http.ResponseWriter, r *http.Request) { case errors.Is(err, ledgercontroller.ErrInvalidIdempotencyInput{}): api.BadRequest(w, ErrValidation, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v2/controllers_transactions_delete_metadata.go b/internal/api/v2/controllers_transactions_delete_metadata.go index 397635bd9..afc0a6d71 100644 --- a/internal/api/v2/controllers_transactions_delete_metadata.go +++ b/internal/api/v2/controllers_transactions_delete_metadata.go @@ -1,7 +1,6 @@ package v2 import ( - "github.com/formancehq/go-libs/platform/postgres" "net/http" "strconv" @@ -31,12 +30,10 @@ func deleteTransactionMetadata(w http.ResponseWriter, r *http.Request) { Key: metadataKey, })); err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, ledgercontroller.ErrNotFound): api.NotFound(w, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v2/controllers_transactions_list.go b/internal/api/v2/controllers_transactions_list.go index 6af61c5eb..263fe11c9 100644 --- a/internal/api/v2/controllers_transactions_list.go +++ b/internal/api/v2/controllers_transactions_list.go @@ -1,7 +1,6 @@ package v2 import ( - "github.com/formancehq/go-libs/platform/postgres" "net/http" "errors" @@ -39,12 +38,10 @@ func listTransactions(w http.ResponseWriter, r *http.Request) { cursor, err := l.ListTransactions(r.Context(), *query) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, ledgercontroller.ErrInvalidQuery{}) || errors.Is(err, ledgercontroller.ErrMissingFeature{}): api.BadRequest(w, ErrValidation, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v2/controllers_transactions_read.go b/internal/api/v2/controllers_transactions_read.go index 228b9b121..753d5f584 100644 --- a/internal/api/v2/controllers_transactions_read.go +++ b/internal/api/v2/controllers_transactions_read.go @@ -1,7 +1,6 @@ package v2 import ( - "errors" "net/http" "strconv" @@ -39,12 +38,10 @@ func readTransaction(w http.ResponseWriter, r *http.Request) { tx, err := l.GetTransaction(r.Context(), query) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case postgres.IsNotFoundError(err): api.NotFound(w, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v2/controllers_transactions_revert.go b/internal/api/v2/controllers_transactions_revert.go index 37d9855ba..b10da2c82 100644 --- a/internal/api/v2/controllers_transactions_revert.go +++ b/internal/api/v2/controllers_transactions_revert.go @@ -1,7 +1,6 @@ package v2 import ( - "github.com/formancehq/go-libs/platform/postgres" "net/http" "strconv" @@ -32,8 +31,6 @@ func revertTransaction(w http.ResponseWriter, r *http.Request) { ) if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, &ledgercontroller.ErrInsufficientFunds{}): api.BadRequest(w, ErrInsufficientFund, err) case errors.Is(err, ledgercontroller.ErrAlreadyReverted{}): @@ -41,7 +38,7 @@ func revertTransaction(w http.ResponseWriter, r *http.Request) { case errors.Is(err, ledgercontroller.ErrNotFound): api.NotFound(w, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return } diff --git a/internal/api/v2/controllers_volumes.go b/internal/api/v2/controllers_volumes.go index a06a439db..30b5dd454 100644 --- a/internal/api/v2/controllers_volumes.go +++ b/internal/api/v2/controllers_volumes.go @@ -1,7 +1,6 @@ package v2 import ( - "github.com/formancehq/go-libs/platform/postgres" "net/http" "errors" @@ -39,12 +38,10 @@ func readVolumes(w http.ResponseWriter, r *http.Request) { if err != nil { switch { - case errors.Is(err, postgres.ErrTooManyClient{}): - api.WriteErrorResponse(w, http.StatusServiceUnavailable, api.ErrorInternal, err) case errors.Is(err, ledgercontroller.ErrInvalidQuery{}) || errors.Is(err, ledgercontroller.ErrMissingFeature{}): api.BadRequest(w, ErrValidation, err) default: - api.InternalServerError(w, r, err) + common.HandleCommonErrors(w, r, err) } return }