Skip to content

fix: return 400 instead of 500 for idempotency key reuse with different body #921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ dumps
.env
!.envrc
Pulumi.*.yaml
coverage.txt
2 changes: 2 additions & 0 deletions docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,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 @@ -980,6 +981,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
17 changes: 16 additions & 1 deletion internal/api/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import (
"errors"
"net/http"

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

const (
Expand Down Expand Up @@ -37,6 +39,19 @@
}
}

func HandleCommonWriteErrors(w http.ResponseWriter, r *http.Request, err error) {
switch {
case errors.Is(err, ledgercontroller.ErrIdempotencyKeyConflict{}):
api.WriteErrorResponse(w, http.StatusConflict, ErrConflict, err)

Check warning on line 45 in internal/api/common/errors.go

View check run for this annotation

Codecov / codecov/patch

internal/api/common/errors.go#L44-L45

Added lines #L44 - L45 were not covered by tests
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)
}
}

func InternalServerError(w http.ResponseWriter, r *http.Request, err error) {
otlp.RecordError(r.Context(), err)
logging.FromContext(r.Context()).Error(err)
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 @@

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/v3/api"
"github.com/formancehq/go-libs/v3/metadata"
"github.com/formancehq/ledger/internal/api/common"
Expand Down Expand Up @@ -38,7 +40,7 @@
Metadata: m,
}))
if err != nil {
common.HandleCommonErrors(w, r, err)
common.HandleCommonWriteErrors(w, r, err)

Check warning on line 43 in internal/api/v1/controllers_accounts_add_metadata.go

View check run for this annotation

Codecov / codecov/patch

internal/api/v1/controllers_accounts_add_metadata.go#L43

Added line #L43 was not covered by tests
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/v3/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
3 changes: 2 additions & 1 deletion internal/api/v1/controllers_transactions_add_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"

"errors"

"github.com/formancehq/go-libs/v3/api"
"github.com/formancehq/go-libs/v3/metadata"
"github.com/formancehq/ledger/internal/api/common"
Expand Down Expand Up @@ -37,7 +38,7 @@
case errors.Is(err, ledgercontroller.ErrNotFound):
api.NotFound(w, err)
default:
common.HandleCommonErrors(w, r, err)
common.HandleCommonWriteErrors(w, r, err)

Check warning on line 41 in internal/api/v1/controllers_transactions_add_metadata.go

View check run for this annotation

Codecov / codecov/patch

internal/api/v1/controllers_transactions_add_metadata.go#L41

Added line #L41 was not covered by tests
}
return
}
Expand Down
5 changes: 3 additions & 2 deletions internal/api/v1/controllers_transactions_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"

"errors"

"github.com/formancehq/go-libs/v3/api"
"github.com/formancehq/go-libs/v3/metadata"
"github.com/formancehq/go-libs/v3/time"
Expand Down Expand Up @@ -100,7 +101,7 @@
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)

Check warning on line 104 in internal/api/v1/controllers_transactions_create.go

View check run for this annotation

Codecov / codecov/patch

internal/api/v1/controllers_transactions_create.go#L104

Added line #L104 was not covered by tests
}
return
}
Expand Down Expand Up @@ -135,7 +136,7 @@
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)

Check warning on line 139 in internal/api/v1/controllers_transactions_create.go

View check run for this annotation

Codecov / codecov/patch

internal/api/v1/controllers_transactions_create.go#L139

Added line #L139 was not covered by tests
}
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/v3/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: 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
6 changes: 4 additions & 2 deletions internal/api/v2/controllers_accounts_add_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package v2

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

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

"errors"

"github.com/formancehq/go-libs/v3/api"
"github.com/formancehq/go-libs/v3/metadata"
"github.com/formancehq/ledger/internal/api/common"
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/v3/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/v3/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 @@

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/v3/api"
"github.com/formancehq/go-libs/v3/metadata"
systemcontroller "github.com/formancehq/ledger/internal/controller/system"
Expand All @@ -22,7 +24,7 @@
}

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

Check warning on line 27 in internal/api/v2/controllers_ledgers_update_metadata.go

View check run for this annotation

Codecov / codecov/patch

internal/api/v2/controllers_ledgers_update_metadata.go#L27

Added line #L27 was not covered by tests
return
}

Expand Down
3 changes: 2 additions & 1 deletion 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/v3/api"
"github.com/formancehq/go-libs/v3/metadata"
"github.com/formancehq/ledger/internal/api/common"
Expand Down Expand Up @@ -37,7 +38,7 @@ func addTransactionMetadata(w http.ResponseWriter, r *http.Request) {
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/v3/api"
Expand All @@ -29,12 +28,7 @@ func deleteTransactionMetadata(w http.ResponseWriter, r *http.Request) {
TransactionID: 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/v3/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 @@ -1864,6 +1864,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 @@ -2241,6 +2246,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
10 changes: 10 additions & 0 deletions openapi/v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,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 @@ -983,6 +988,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