Skip to content

fix: map ledger errors to correct HTTP status codes (EN-1203)#108

Open
flemzord wants to merge 1 commit into
mainfrom
fix/error-mapping
Open

fix: map ledger errors to correct HTTP status codes (EN-1203)#108
flemzord wants to merge 1 commit into
mainfrom
fix/error-mapping

Conversation

@flemzord

Copy link
Copy Markdown
Member

Problem (High + Medium — H1, M2, M3, M4, M6)

Several error paths returned 500 (or leaked internals) where a 4xx was correct, and the insufficient-funds mapping was effectively dead code.

  • H1Manager.CreateTransaction only mapped insufficient funds when the error was *sdkerrors.WalletsErrorResponse, but the ledger SDK's V2.CreateTransaction always returns *sdkerrors.V2ErrorResponse. The branch never matched in production, so a debit/confirm with insufficient funds returned 500 instead of 400 INSUFFICIENT_FUND. The unit test passed only because the mock injected the wrong (fake) type.
  • M2GetWallet, GetBalance, GetHold, ConfirmHold, VoidHold returned 500 (or a phantom 200) for missing entities instead of 404.
  • M3internalError echoed err.Error() to the client, leaking ledger URLs, account addresses and SDK internals.
  • M4 — a debit whose resolved balances are all expired produced source = { }, an invalid script the ledger rejects as a 500.
  • M6 — a malformed pagination cursor surfaced the ledger's 400 as an unmapped 500.

Fix

  • Translate the SDK error to ErrInsufficientFundError in DefaultLedger.CreateTransaction (same pattern already used by GetAccount for not-found); drop the dead switch in Manager.CreateTransaction.
  • Map ErrAccountNotFound to the proper domain errors in the read/confirm/void paths; verify IsHold in GetHold/VoidHold; handlers answer 404.
  • internalError returns a generic message and logs the full error server-side.
  • A debit with no usable (non-expired) source returns INSUFFICIENT_FUND.
  • New ErrValidation sentinel: ListAccounts/ListTransactions map the ledger VALIDATION error so list handlers answer 400.

Tests

  • Insufficient-funds debit test now exercises the real domain error.
  • New: TestHoldsGetNotFound, TestHoldsGetWrongAccountType, and a debit "with only expired balance" case → 400.

Note

Touches the four list handlers, which are also edited by #107 (api-hardening, pageSize). Expect a trivial rebase on whichever merges second.

From the in-depth repository review.

Several error paths returned 500 (or leaked internals) where a 4xx was
correct, and the insufficient-funds mapping was dead code.

- H1: DefaultLedger.CreateTransaction now translates the SDK
  *sdkerrors.V2ErrorResponse (INSUFFICIENT_FUND) to ErrInsufficientFundError.
  The previous switch on *sdkerrors.WalletsErrorResponse never matched in
  production (the SDK never returns that type), so insufficient funds 500'd.
- M2: GetWallet/GetBalance/GetHold/ConfirmHold/VoidHold now map account
  not-found to the proper domain error (404). GetHold/VoidHold also verify
  the account is actually a hold.
- M3: internalError no longer echoes err.Error() to the client (it leaked
  ledger URLs, account addresses and SDK internals); a generic message is
  returned and the full error is still logged.
- M4: a debit whose resolved balances are all expired returns
  INSUFFICIENT_FUND instead of building an empty-source script (ledger 500).
- M6: a malformed pagination cursor (ledger VALIDATION error) now returns
  400 instead of 500.

Updates the insufficient-funds test to use the real domain error and adds
regression tests for hold 404 and the all-expired-balance debit.
@flemzord flemzord requested a review from a team as a code owner June 11, 2026 08:00
@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@flemzord, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 43 minutes and 19 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more credits in the billing tab to continue.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: bc8200b3-7db0-4384-b71c-f9f3c4f93669

📥 Commits

Reviewing files that changed from the base of the PR and between 85e1d3f and fcc9cd6.

📒 Files selected for processing (12)
  • pkg/api/handler_balances_list.go
  • pkg/api/handler_holds_get.go
  • pkg/api/handler_holds_get_test.go
  • pkg/api/handler_holds_list.go
  • pkg/api/handler_holds_void.go
  • pkg/api/handler_transactions_list.go
  • pkg/api/handler_wallets_debit_test.go
  • pkg/api/handler_wallets_list.go
  • pkg/api/utils.go
  • pkg/error.go
  • pkg/ledger_interface.go
  • pkg/manager.go
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/error-mapping

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@flemzord flemzord changed the title fix: map ledger errors to correct HTTP status codes fix: map ledger errors to correct HTTP status codes (EN-1203) Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants