Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,7 @@ Authorization ( Scopes: ledger:write )
DELETE http://localhost:8080/v2/{ledger}/transactions/{id}/metadata/{key} HTTP/1.1
Host: localhost:8080
Accept: application/json
Idempotency-Key: string

```

Expand All @@ -951,6 +952,7 @@ Delete metadata by key
|ledger|path|string|true|Name of the ledger.|
|id|path|integer(bigint)|true|Transaction ID.|
|key|path|string|true|The key to remove.|
|Idempotency-Key|header|string|false|Use an idempotency key|

> Example responses

Expand Down
20 changes: 19 additions & 1 deletion internal/api/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package common

import (
"errors"
"net/http"

"github.com/formancehq/go-libs/v2/api"
"github.com/formancehq/go-libs/v2/logging"
"github.com/formancehq/go-libs/v2/otlp"
"github.com/formancehq/go-libs/v2/platform/postgres"
"net/http"
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"
)

const (
Expand All @@ -28,6 +30,22 @@ const (
ErrScriptMetadataOverride = "METADATA_OVERRIDE"
)

// HandleCommonWriteErrors specifically handles errors related to write operations
// such as idempotency errors
func HandleCommonWriteErrors(w http.ResponseWriter, r *http.Request, err error) {
switch {
case errors.Is(err, ledgercontroller.ErrIdempotencyKeyConflict{}):
api.WriteErrorResponse(w, http.StatusConflict, ErrConflict, err)
case errors.Is(err, ledgercontroller.ErrInvalidIdempotencyInput{}):
api.BadRequest(w, ErrValidation, err)
case errors.Is(err, ledgercontroller.ErrNotFound):
api.NotFound(w, err)
default:
HandleCommonErrors(w, r, err)
}
}

// HandleCommonErrors handles more general common errors
func HandleCommonErrors(w http.ResponseWriter, r *http.Request, err error) {
switch {
case errors.Is(err, postgres.ErrTooManyClient{}):
Expand Down
8 changes: 5 additions & 3 deletions internal/api/v1/controllers_accounts_add_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package v1

import (
"encoding/json"
"github.com/formancehq/ledger/internal/controller/ledger"
"github.com/formancehq/ledger/pkg/accounts"
"net/http"
"net/url"

"github.com/formancehq/ledger/internal/controller/ledger"
"github.com/formancehq/ledger/pkg/accounts"

"errors"

"github.com/formancehq/go-libs/v2/api"
"github.com/formancehq/go-libs/v2/metadata"
"github.com/formancehq/ledger/internal/api/common"
Expand Down Expand Up @@ -38,7 +40,7 @@ func addAccountMetadata(w http.ResponseWriter, r *http.Request) {
Metadata: m,
}))
if err != nil {
common.HandleCommonErrors(w, r, err)
common.HandleCommonWriteErrors(w, r, err)
return
}

Expand Down
5 changes: 3 additions & 2 deletions internal/api/v1/controllers_accounts_delete_metadata.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package v1

import (
"github.com/formancehq/ledger/internal/controller/ledger"
"net/http"
"net/url"

"github.com/formancehq/ledger/internal/controller/ledger"

"github.com/formancehq/go-libs/v2/api"
"github.com/formancehq/ledger/internal/api/common"
"github.com/go-chi/chi/v5"
Expand All @@ -25,7 +26,7 @@ func deleteAccountMetadata(w http.ResponseWriter, r *http.Request) {
Key: chi.URLParam(r, "key"),
}),
); err != nil {
common.HandleCommonErrors(w, r, err)
common.HandleCommonWriteErrors(w, r, err)
return
}

Expand Down
8 changes: 4 additions & 4 deletions internal/api/v1/controllers_transactions_add_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"

"errors"

"github.com/formancehq/go-libs/v2/api"
"github.com/formancehq/go-libs/v2/metadata"
"github.com/formancehq/ledger/internal/api/common"
Expand All @@ -33,11 +34,10 @@ func addTransactionMetadata(w http.ResponseWriter, r *http.Request) {
TransactionID: int(txID),
Metadata: m,
})); err != nil {
switch {
case errors.Is(err, ledgercontroller.ErrNotFound):
if errors.Is(err, ledgercontroller.ErrNotFound) {
api.NotFound(w, err)
default:
common.HandleCommonErrors(w, r, err)
} else {
common.HandleCommonWriteErrors(w, r, err)
}
return
}
Expand Down
9 changes: 4 additions & 5 deletions internal/api/v1/controllers_transactions_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"

"errors"

"github.com/formancehq/go-libs/v2/api"
"github.com/formancehq/go-libs/v2/metadata"
"github.com/formancehq/go-libs/v2/time"
Expand Down Expand Up @@ -92,13 +93,12 @@ func createTransaction(w http.ResponseWriter, r *http.Request) {
api.BadRequest(w, common.ErrScriptCompilationFailed, err)
case errors.Is(err, &ledgercontroller.ErrMetadataOverride{}):
api.BadRequest(w, common.ErrScriptMetadataOverride, err)
case errors.Is(err, ledgercontroller.ErrNoPostings) ||
errors.Is(err, ledgercontroller.ErrInvalidIdempotencyInput{}):
case errors.Is(err, ledgercontroller.ErrNoPostings):
api.BadRequest(w, common.ErrValidation, err)
case errors.Is(err, ledgercontroller.ErrTransactionReferenceConflict{}):
api.WriteErrorResponse(w, http.StatusConflict, common.ErrConflict, err)
default:
common.HandleCommonErrors(w, r, err)
common.HandleCommonWriteErrors(w, r, err)
}
return
}
Expand Down Expand Up @@ -127,13 +127,12 @@ func createTransaction(w http.ResponseWriter, r *http.Request) {
case errors.Is(err, &ledgercontroller.ErrInvalidVars{}) ||
errors.Is(err, ledgercontroller.ErrCompilationFailed{}) ||
errors.Is(err, &ledgercontroller.ErrMetadataOverride{}) ||
errors.Is(err, ledgercontroller.ErrInvalidIdempotencyInput{}) ||
errors.Is(err, ledgercontroller.ErrNoPostings):
api.BadRequest(w, common.ErrValidation, err)
case errors.Is(err, ledgercontroller.ErrTransactionReferenceConflict{}):
api.WriteErrorResponse(w, http.StatusConflict, common.ErrConflict, err)
default:
common.HandleCommonErrors(w, r, err)
common.HandleCommonWriteErrors(w, r, err)
}
return
}
Expand Down
8 changes: 2 additions & 6 deletions internal/api/v1/controllers_transactions_delete_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"

"errors"

"github.com/formancehq/go-libs/v2/api"
"github.com/formancehq/ledger/internal/api/common"
"github.com/go-chi/chi/v5"
Expand All @@ -27,12 +28,7 @@ func deleteTransactionMetadata(w http.ResponseWriter, r *http.Request) {
TransactionID: int(transactionID),
Key: metadataKey,
})); err != nil {
switch {
case errors.Is(err, ledgercontroller.ErrNotFound):
api.NotFound(w, err)
default:
common.HandleCommonErrors(w, r, err)
}
common.HandleCommonWriteErrors(w, r, err)
return
}

Expand Down
8 changes: 5 additions & 3 deletions internal/api/v2/controllers_accounts_add_metadata.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
//go:build !wasm

package v2

import (
"encoding/json"
"github.com/formancehq/ledger/internal/controller/ledger"
"errors"
"net/http"
"net/url"

"errors"
"github.com/formancehq/go-libs/v2/api"
"github.com/formancehq/go-libs/v2/metadata"
"github.com/formancehq/ledger/internal/api/common"
"github.com/formancehq/ledger/internal/controller/ledger"
"github.com/go-chi/chi/v5"
)

Expand All @@ -33,7 +35,7 @@ func addAccountMetadata(w http.ResponseWriter, r *http.Request) {
Metadata: m,
}))
if err != nil {
common.HandleCommonErrors(w, r, err)
common.HandleCommonWriteErrors(w, r, err)
return
}

Expand Down
5 changes: 3 additions & 2 deletions internal/api/v2/controllers_accounts_delete_metadata.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package v2

import (
"github.com/formancehq/ledger/internal/controller/ledger"
"net/http"
"net/url"

"github.com/formancehq/ledger/internal/controller/ledger"

"github.com/go-chi/chi/v5"

"github.com/formancehq/go-libs/v2/api"
Expand All @@ -26,7 +27,7 @@ func deleteAccountMetadata(w http.ResponseWriter, r *http.Request) {
Key: chi.URLParam(r, "key"),
}),
); err != nil {
common.HandleCommonErrors(w, r, err)
common.HandleCommonWriteErrors(w, r, err)
return
}

Expand Down
5 changes: 3 additions & 2 deletions internal/api/v2/controllers_ledgers_delete_metadata.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package v2

import (
"github.com/formancehq/ledger/internal/api/common"
"net/http"

"github.com/formancehq/ledger/internal/api/common"

"github.com/formancehq/ledger/internal/controller/system"

"github.com/formancehq/go-libs/v2/api"
Expand All @@ -13,7 +14,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 {
common.HandleCommonErrors(w, r, err)
common.HandleCommonWriteErrors(w, r, err)
return
}

Expand Down
6 changes: 4 additions & 2 deletions internal/api/v2/controllers_ledgers_update_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package v2

import (
"encoding/json"
"github.com/formancehq/ledger/internal/api/common"
"net/http"

"github.com/formancehq/ledger/internal/api/common"

"errors"

"github.com/formancehq/go-libs/v2/api"
"github.com/formancehq/go-libs/v2/metadata"
systemcontroller "github.com/formancehq/ledger/internal/controller/system"
Expand All @@ -22,7 +24,7 @@ func updateLedgerMetadata(systemController systemcontroller.Controller) http.Han
}

if err := systemController.UpdateLedgerMetadata(r.Context(), chi.URLParam(r, "ledger"), m); err != nil {
common.HandleCommonErrors(w, r, err)
common.HandleCommonWriteErrors(w, r, err)
return
}

Expand Down
8 changes: 2 additions & 6 deletions internal/api/v2/controllers_transactions_add_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"

"errors"

"github.com/formancehq/go-libs/v2/api"
"github.com/formancehq/go-libs/v2/metadata"
"github.com/formancehq/ledger/internal/api/common"
Expand All @@ -33,12 +34,7 @@ func addTransactionMetadata(w http.ResponseWriter, r *http.Request) {
TransactionID: int(txID),
Metadata: m,
})); err != nil {
switch {
case errors.Is(err, ledgercontroller.ErrNotFound):
api.NotFound(w, err)
default:
common.HandleCommonErrors(w, r, err)
}
common.HandleCommonWriteErrors(w, r, err)
return
}

Expand Down
7 changes: 3 additions & 4 deletions internal/api/v2/controllers_transactions_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package v2

import (
"encoding/json"
"github.com/formancehq/ledger/internal/api/bulking"
"net/http"

"github.com/formancehq/ledger/internal/api/bulking"

ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"

"errors"
Expand Down Expand Up @@ -51,14 +52,12 @@ func createTransaction(w http.ResponseWriter, r *http.Request) {
api.BadRequest(w, common.ErrNoPostings, err)
case errors.Is(err, ledgercontroller.ErrTransactionReferenceConflict{}):
api.WriteErrorResponse(w, http.StatusConflict, common.ErrConflict, err)
case errors.Is(err, ledgercontroller.ErrInvalidIdempotencyInput{}):
api.BadRequest(w, common.ErrValidation, err)
case errors.Is(err, ledgercontroller.ErrParsing{}):
api.BadRequest(w, common.ErrInterpreterParse, err)
case errors.Is(err, ledgercontroller.ErrRuntime{}):
api.BadRequest(w, common.ErrInterpreterRuntime, err)
default:
common.HandleCommonErrors(w, r, err)
common.HandleCommonWriteErrors(w, r, err)
}
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/go-chi/chi/v5"

"errors"
"github.com/formancehq/ledger/internal/api/common"

"github.com/formancehq/go-libs/v2/api"
Expand All @@ -29,12 +28,7 @@ func deleteTransactionMetadata(w http.ResponseWriter, r *http.Request) {
TransactionID: int(txID),
Key: metadataKey,
})); err != nil {
switch {
case errors.Is(err, ledgercontroller.ErrNotFound):
api.NotFound(w, err)
default:
common.HandleCommonErrors(w, r, err)
}
common.HandleCommonWriteErrors(w, r, err)
return
}

Expand Down
1 change: 1 addition & 0 deletions internal/api/v2/controllers_transactions_revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"

"errors"

"github.com/formancehq/go-libs/v2/api"
"github.com/formancehq/ledger/internal/api/common"
"github.com/go-chi/chi/v5"
Expand Down
10 changes: 10 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,11 @@ paths:
schema:
type: string
example: foo
- name: Idempotency-Key
in: header
description: Use an idempotency key
schema:
type: string
responses:
2XX:
description: Key deleted
Expand Down Expand Up @@ -2116,6 +2121,11 @@ paths:
schema:
type: string
example: foo
- name: Idempotency-Key
in: header
description: Use an idempotency key
schema:
type: string
responses:
2XX:
description: Key deleted
Expand Down
Loading
Loading