Skip to content
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

Make the NotEnoughMoney API error machine-readable. #4348

Merged

Conversation

jonathanknowles
Copy link
Member

@jonathanknowles jonathanknowles commented Dec 15, 2023

Issue

ADP-3247

Description

This PR revises the NotEnoughMoney API error so that it is machine-readable, with a well-defined schema.

Example

{ "code": "not_enough_money"
, "message": "I can't process this payment as there are not enough funds available in the wallet."
, "info":
  { "shortfall":
    { "ada":
      { "quantity": 1
      , "unit": "lovelace"
      }
    , "assets":
      [ { "policy_id": "FRUIT"
        , "asset_name": "APPLE"
        , "quantity": 2
        }
      , { "policy_id": "FRUIT"
        , "asset_name": "BANANA"
        , "quantity": 4
        }
      ]
    }
  }
}

@jonathanknowles jonathanknowles self-assigned this Dec 15, 2023
@jonathanknowles jonathanknowles changed the title WIP: Make NotEnoughMoney a machine-readable error. WIP: Make NotEnoughMoney a machine-readable API error. Dec 15, 2023
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/machine-readable-not-enough-money branch 3 times, most recently from 91320a2 to 68a9d4d Compare December 19, 2023 07:10
@jonathanknowles jonathanknowles changed the base branch from master to jonathanknowles/api-amount December 19, 2023 11:27
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/machine-readable-not-enough-money branch 2 times, most recently from fa71b0e to 95f7f35 Compare December 19, 2023 11:41
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/machine-readable-not-enough-money branch 2 times, most recently from aca2498 to 1ab2d97 Compare December 20, 2023 02:53
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/api-amount branch 2 times, most recently from 36ef3c9 to de9165b Compare December 20, 2023 04:33
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/machine-readable-not-enough-money branch from 1ab2d97 to 9e4ce91 Compare December 20, 2023 04:34
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/machine-readable-not-enough-money branch from 9e4ce91 to e903be4 Compare December 20, 2023 05:11
@jonathanknowles jonathanknowles changed the base branch from jonathanknowles/api-amount to jonathanknowles/no-utxos-available December 20, 2023 05:11
@jonathanknowles jonathanknowles changed the title WIP: Make NotEnoughMoney a machine-readable API error. Make NotEnoughMoney a machine-readable API error. Dec 20, 2023
Base automatically changed from jonathanknowles/no-utxos-available to master December 20, 2023 09:39
github-merge-queue bot pushed a commit that referenced this pull request Dec 20, 2023
## Summary

This PR forks the HTTP API error `not_enough_money` into two separate
errors:
- `not_enough_money`
    for when there **_really_** isn't enough money
- `no_utxos_available`
for when there **_might_** be enough money, but there are no UTxOs
available

These two cases can require subtly different workarounds from the user.

## Details

This PR solves two problems.

### Problem 1

The `not_enough_money` error is **_inappropriate_** in the case where
the reward balance alone would be sufficient to cover the amount
required.

For example, suppose a user has a wallet with:
- no UTxOs;
- an unspent reward balance of `1,000,000,000` ada.

Suppose that the user attempts to make a payment transaction of `10`
ada. In this case, the attempt will fail because:
- every transaction is required to spend at least one UTxO; but
- the wallet has no UTxOs available.

But the user will receive a misleading `not_enough_money` error, even
though they have more than enough money.

This PR solves this problem by always using `no_utxos_available` when
there are no UTxOs.

### Problem 2

In the case where UTxOs **_are_** available, but their value is
insufficient, we currently attempt to embed information about the
shortfall within the error message string:
> ```
> I can't process this payment as there are not enough funds in the
wallet. I am missing:
> - lovelace: 123,456 lovelace
> - assets: ... <information embedded within string>
> ```

This information is valuable for UI clients that wish to inform users
how much extra value they would need, but the information is tricky for
UI clients to parse, as it's embedded within a **_string_**.

We wish to turn the shortfall case into a **_machine-readable_** error
with a specified JSON encoding (see #4348). However, this error would
not be appropriate for the case where there are no UTxOs available at
all, as there would not necessarily even be a "shortfall".

This PR avoids this problem by giving the former case (no UTxOs
available) its own dedicated error that will not be affected by #4348.
WilliamKingNoel-Bot pushed a commit that referenced this pull request Dec 20, 2023
…This PR forks the HTTP API error `not_enough_money` into two separate errors: - `not_enough_money` for when there **_really_** isn't enough money - `no_utxos_available` for when there **_might_** be enough money, but there are no UTxOs available These two cases can require subtly different workarounds from the user. ## Details This PR solves two problems. ### Problem 1 The `not_enough_money` error is **_inappropriate_** in the case where the reward balance alone would be sufficient to cover the amount required. For example, suppose a user has a wallet with: - no UTxOs; - an unspent reward balance of `1,000,000,000` ada. Suppose that the user attempts to make a payment transaction of `10` ada. In this case, the attempt will fail because: - every transaction is required to spend at least one UTxO; but - the wallet has no UTxOs available. But the user will receive a misleading `not_enough_money` error, even though they have more than enough money. This PR solves this problem by always using `no_utxos_available` when there are no UTxOs. ### Problem 2 In the case where UTxOs **_are_** available, but their value is insufficient, we currently attempt to embed information about the shortfall within the error message string: > ``` > I can't process this payment as there are not enough funds in the wallet. I am missing: > - lovelace: 123,456 lovelace > - assets: ... <information embedded within string> > ``` This information is valuable for UI clients that wish to inform users how much extra value they would need, but the information is tricky for UI clients to parse, as it's embedded within a **_string_**. We wish to turn the shortfall case into a **_machine-readable_** error with a specified JSON encoding (see #4348). However, this error would not be appropriate for the case where there are no UTxOs available at all, as there would not necessarily even be a "shortfall". This PR avoids this problem by giving the former case (no UTxOs available) its own dedicated error that will not be affected by #4348. Source commit: 15f75af
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/machine-readable-not-enough-money branch 2 times, most recently from 5b3937b to be2e18c Compare December 21, 2023 02:43
@jonathanknowles jonathanknowles marked this pull request as ready for review December 21, 2023 04:10
@jonathanknowles jonathanknowles marked this pull request as draft December 21, 2023 04:11
@jonathanknowles jonathanknowles marked this pull request as ready for review December 21, 2023 07:44
@jonathanknowles jonathanknowles changed the title Make NotEnoughMoney a machine-readable API error. Make the NotEnoughMoney API error machine-readable. Dec 21, 2023
@jonathanknowles jonathanknowles marked this pull request as draft December 21, 2023 11:02
@jonathanknowles jonathanknowles marked this pull request as ready for review December 22, 2023 00:13
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/machine-readable-not-enough-money branch from be2e18c to 885a999 Compare December 22, 2023 01:44
description: May occur when there's not enough money in the wallet to cover a requested payment.
description: |
Indicates that there's not enough money in the wallet to cover a
requested payment.
Copy link
Member Author

Choose a reason for hiding this comment

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

Add a sentence that describes the shortfall field in more detail.
Emphasise that the shortfall just describes the assets that are missing, and that if supplied, would unblock whatever operation it was that the user was attempting.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed in 0432db5.

@@ -378,11 +377,6 @@ errMsg403NotAnIcarusWallet =
"I cannot derive new address for this wallet type.\
\ Make sure to use a sequential wallet style, like Icarus."

errMsg403NotEnoughMoney :: String
Copy link
Contributor

Choose a reason for hiding this comment

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

🥇

Instead of inspecting the `message` string.
, expectErrorMessage errMsg403NotEnoughMoney
]
verify rTx [expectResponseCode HTTP.status403]
decodeErrorInfo rTx `shouldSatisfy` \case
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

@paweljakubas paweljakubas left a comment

Choose a reason for hiding this comment

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

very nice PR

@jonathanknowles jonathanknowles added this pull request to the merge queue Dec 22, 2023
Merged via the queue into master with commit 4335f75 Dec 22, 2023
4 checks passed
@jonathanknowles jonathanknowles deleted the jonathanknowles/machine-readable-not-enough-money branch December 22, 2023 10:40
WilliamKingNoel-Bot pushed a commit that referenced this pull request Dec 22, 2023
…# Issue ADP-3247 ## Description This PR revises the `NotEnoughMoney` API error so that it is machine-readable, with a well-defined schema. ## Example ```json { "code": "not_enough_money" , "message": "I can't process this payment as there are not enough funds available in the wallet." , "info": { "shortfall": { "ada": { "quantity": 1 , "unit": "lovelace" } , "assets": [ { "policy_id": "FRUIT" , "asset_name": "APPLE" , "quantity": 2 } , { "policy_id": "FRUIT" , "asset_name": "BANANA" , "quantity": 4 } ] } } } ``` Source commit: 4335f75
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