Skip to content

Commit

Permalink
feat: factorize error handling on api
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent a310d95 commit bcc61b1
Show file tree
Hide file tree
Showing 43 changed files with 66 additions and 230 deletions.
17 changes: 17 additions & 0 deletions internal/api/common/errors.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
8 changes: 1 addition & 7 deletions internal/api/v1/controllers_accounts_add_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
5 changes: 1 addition & 4 deletions internal/api/v1/controllers_accounts_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package v1

import (
"fmt"
"github.com/formancehq/go-libs/platform/postgres"
"net/http"

"errors"
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 1 addition & 8 deletions internal/api/v1/controllers_accounts_delete_metadata.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
}

Expand Down
5 changes: 1 addition & 4 deletions internal/api/v1/controllers_accounts_list.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package v1

import (
"github.com/formancehq/go-libs/platform/postgres"
"net/http"

"errors"
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 1 addition & 4 deletions internal/api/v1/controllers_accounts_read.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package v1

import (
"errors"
"net/http"
"net/url"

Expand Down Expand Up @@ -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,
Expand All @@ -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
}
}
Expand Down
9 changes: 1 addition & 8 deletions internal/api/v1/controllers_balances_aggregates.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package v1

import (
"errors"
"github.com/formancehq/go-libs/platform/postgres"
"net/http"

"github.com/formancehq/go-libs/api"
Expand Down Expand Up @@ -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
}

Expand Down
9 changes: 1 addition & 8 deletions internal/api/v1/controllers_balances_list.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package v1

import (
"errors"
"github.com/formancehq/go-libs/platform/postgres"
"math/big"
"net/http"

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

Expand Down
3 changes: 2 additions & 1 deletion internal/api/v1/controllers_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
9 changes: 1 addition & 8 deletions internal/api/v1/controllers_info.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package v1

import (
"errors"
"github.com/formancehq/go-libs/platform/postgres"
"net/http"

"github.com/go-chi/chi/v5"
Expand Down Expand Up @@ -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
}

Expand Down
9 changes: 1 addition & 8 deletions internal/api/v1/controllers_logs_list.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package v1

import (
"errors"
"fmt"
"github.com/formancehq/go-libs/platform/postgres"
"net/http"

"github.com/formancehq/go-libs/api"
Expand Down Expand Up @@ -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
}

Expand Down
9 changes: 1 addition & 8 deletions internal/api/v1/controllers_stats.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package v1

import (
"errors"
"github.com/formancehq/go-libs/platform/postgres"
"net/http"

"github.com/formancehq/go-libs/api"
Expand All @@ -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
}

Expand Down
5 changes: 1 addition & 4 deletions internal/api/v1/controllers_transactions_add_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package v1

import (
"encoding/json"
"github.com/formancehq/go-libs/platform/postgres"
"net/http"
"strconv"

Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 1 addition & 8 deletions internal/api/v1/controllers_transactions_count.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package v1

import (
"errors"
"fmt"
"github.com/formancehq/go-libs/platform/postgres"
"net/http"

"github.com/formancehq/go-libs/api"
Expand All @@ -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
}

Expand Down
9 changes: 2 additions & 7 deletions internal/api/v1/controllers_transactions_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package v1
import (
"encoding/json"
"fmt"
"github.com/formancehq/go-libs/platform/postgres"
"math/big"
"net/http"

Expand Down Expand Up @@ -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{}):
Expand All @@ -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
}
Expand All @@ -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{}) ||
Expand All @@ -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
}
Expand Down
5 changes: 1 addition & 4 deletions internal/api/v1/controllers_transactions_delete_metadata.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package v1

import (
"github.com/formancehq/go-libs/platform/postgres"
"net/http"
"strconv"

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

0 comments on commit bcc61b1

Please sign in to comment.