Skip to content

fix(common): improve resilience when market API returns no data for currency#14922

Merged
gre-ledger merged 1 commit intodevelopfrom
bugfix/market-currency-id-resilience
Mar 2, 2026
Merged

fix(common): improve resilience when market API returns no data for currency#14922
gre-ledger merged 1 commit intodevelopfrom
bugfix/market-currency-id-resilience

Conversation

@gre-ledger
Copy link
Contributor

@gre-ledger gre-ledger commented Mar 2, 2026

✅ Checklist

  • npx changeset was attached.
  • Covered by automatic tests. Added test for empty API response in useCurrencyData.test.ts.
  • Impact of the changes:
    • Market: when opening a coin detail for an id that has no market data (e.g. unknown/unsupported), the app no longer crashes; useCurrencyData returns data: undefined. QA: open Market coin detail for an unsupported/edge currency and confirm no crash and graceful handling.

📝 Description

Problem: Requesting market data for a currency id that doesn’t exist in the API (e.g. unknown or unsupported coin) returned an empty array. The code did format(response[0]) without checking, causing TypeError: Cannot read property 'id' of undefined in the getCurrencyData transformResponse.

Solution: Guard the transformResponse: only call format(response[0]) when response?.[0] exists; otherwise return undefined. Type the endpoint as MarketCurrencyData | undefined. Added a unit test for the empty-response case.

Before After
Crash when API returns [] for the requested id Query succeeds with data: undefined; callers can handle “no market data”

❓ Context


🧐 Checklist for the PR Reviewers

  • The code aligns with the requirements described in the linked JIRA or GitHub issue.
  • The PR description clearly documents the changes made and explains any technical trade-offs or design decisions.
  • There are no undocumented trade-offs, technical debt, or maintainability issues.
  • The PR has been tested thoroughly, and any potential edge cases have been considered and handled.
  • Any new dependencies have been justified and documented.
  • Performance considerations have been taken into account. (changes have been profiled or benchmarked if necessary)

…urrency id

Guard getCurrencyData transformResponse against empty API response to prevent
'Cannot read property id of undefined' crash. Return undefined when id does
not resolve to a currency. Add test for empty response case.

Made-with: Cursor
@live-github-bot live-github-bot bot added the common Has changes in live-common label Mar 2, 2026
@gre-ledger gre-ledger marked this pull request as ready for review March 2, 2026 13:53
@gre-ledger gre-ledger requested a review from a team as a code owner March 2, 2026 13:53
Copilot AI review requested due to automatic review settings March 2, 2026 13:53
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves robustness of the market “currency data” query in @ledgerhq/live-common by preventing a crash when the Countervalues Market API returns an empty array for an unknown/unsupported currency id.

Changes:

  • Update getCurrencyData RTK Query endpoint to return undefined when the API returns no items instead of calling format(response[0]) and crashing.
  • Add a unit test covering the empty-array response scenario with MSW.
  • Add a changeset describing the behavior change.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
libs/ledger-live-common/src/market/state-manager/countervaluesApi.ts Guards transformResponse and widens the endpoint’s data type to allow undefined on empty API response.
libs/ledger-live-common/src/market/hooks/tests/useCurrencyData.test.ts Adds MSW override + assertion that the query succeeds with data: undefined for empty responses.
.changeset/market-currency-id-resilience.md Declares the package release bump for the change.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 2, 2026

@gre-ledger gre-ledger changed the title fix(common): improve resilience when market API returns no data for c… fix(common): improve resilience when market API returns no data for currency Mar 2, 2026
@gre-ledger gre-ledger merged commit 06a7ac1 into develop Mar 2, 2026
126 of 130 checks passed
@gre-ledger gre-ledger deleted the bugfix/market-currency-id-resilience branch March 2, 2026 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

common Has changes in live-common

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants