Skip to content

fix: clear error when fiat exchange rate is unavailable - #90

Open
dadofsambonzuki wants to merge 1 commit into
lnbits:mainfrom
dadofsambonzuki:fix/clear-fiat-rate-error
Open

dadofsambonzuki wants to merge 1 commit into
lnbits:mainfrom
dadofsambonzuki:fix/clear-fiat-rate-error

Conversation

@dadofsambonzuki

Copy link
Copy Markdown

Summary

When no Bitcoin price provider responds (e.g. during a transient DNS/outbound-network outage), get_fiat_rate_satoshis() silently returns 0.0 instead of raising. Because api_charge_create computes data.amount = round(rate * data.currency_amount) without validating the rate, the charge amount becomes 0 and create_charge() fails with the generic:

Exception: Amount must be greater than 0

That generic failure propagates to checkout callers (e.g. the events extension), which surface it as:

HTTP 400: Could not create payment for any enabled method.
Last error: SatsPay error: Unexpected error! ID: ...

The request was fine — only the fiat→sats conversion was impossible at that moment.

Change

In views_api.py, after fetching the fiat rate, raise a clear HTTP 503 Service Unavailable when rate <= 0:

if rate <= 0:
    raise HTTPException(
        status_code=HTTPStatus.SERVICE_UNAVAILABLE,
        detail=(
            f"Could not fetch Bitcoin price for {data.currency}. "
            "Please try again later."
        ),
    )

Callers (and customers on the checkout page) now see an actionable, retryable message instead of the confusing "Amount must be greater than 0" / "Unexpected error!".

Note: get_fiat_rate_satoshis() has a sibling helper (fiat_amount_as_satoshis()) that already guards rate > 0 with a ValueError — this brings the API path in line with that behavior.

This is a single, scoped change on a fresh branch from main (based on e0d176c).

When no Bitcoin price provider responds (e.g. transient DNS/outbound
network issues), get_fiat_rate_satoshis() silently returns 0.0, so the
computed sats amount becomes 0 and create_charge() fails with the
generic 'Amount must be greater than 0' exception. That surfaces in
checkout flows as 'Could not create payment for any enabled method'.

Raise a clear HTTP 503 with the failing currency instead, so callers and
users can distinguish a transient rate outage from a bad request.
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.

1 participant