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

[ADP-3306] Make NoSuchWallet error machine-readable. #4597

Merged
merged 26 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
62696fd
introduce ApiErrorNoSuchWallet error
paweljakubas May 15, 2024
47379d0
swagger
paweljakubas May 15, 2024
b76246c
remove err403NoSuchWallet - part 1
paweljakubas May 16, 2024
8bc8161
refactor Shared.Wallet
paweljakubas May 16, 2024
cd2c47c
refactor Shelley.StakePools
paweljakubas May 16, 2024
0977b1a
refactor Shared.Transactions
paweljakubas May 16, 2024
5798a13
refactor Shelley.TransactionsNew
paweljakubas May 16, 2024
48bfe95
Shelley.Addresses
paweljakubas May 16, 2024
fd55950
refactor Byron.Migrations
paweljakubas May 16, 2024
3b2c25e
Shelley.Migrations
paweljakubas May 17, 2024
d7a72a4
refactor Shelley.Transaction
paweljakubas May 17, 2024
b5bd885
sort out DSL and ApiError naming overlap
paweljakubas May 17, 2024
fca2c64
Shelley.Wallets
paweljakubas May 17, 2024
607903e
Byron.Transactions
paweljakubas May 17, 2024
4196e24
Byron.Wallets
paweljakubas May 17, 2024
1b40fa3
Byron.Addresses
paweljakubas May 17, 2024
60f1249
Byron.CoinSelections
paweljakubas May 17, 2024
6557962
revert CLI changes
paweljakubas May 17, 2024
64dc4ab
reqquested review changes
paweljakubas May 22, 2024
c664feb
Make `WalletId` an instance of `Data`.
jonathanknowles May 23, 2024
00fd690
Use `ApiT WalletId` instead of `Text` for `ApiErrorNoSuchWallet`.
jonathanknowles May 23, 2024
c9a339a
Regenerate `ApiErrorNoSuchWallet.json`.
jonathanknowles May 23, 2024
0f2d84a
Rename `Err404DbNotInitialized` to `Err404WalletNotInitialized`.
jonathanknowles May 23, 2024
ae50531
Merge branch 'master' into paweljakubas/adp-3306/refactor-no-such-wallet
jonathanknowles May 23, 2024
5253bc6
Fix order of imports, placed out of order by prior merge.
jonathanknowles May 23, 2024
7050122
Revert use of `decodeErrorInfo` for `WALLETS_SIGNATURES_03`.
jonathanknowles May 23, 2024
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
9 changes: 7 additions & 2 deletions lib/api/src/Cardano/Wallet/Api/Http/Server/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ import Cardano.Wallet.Api.Http.Server.Error.IsServerError
import Cardano.Wallet.Api.Types
( ApiCosignerIndex (..)
, ApiCredentialType (..)
, ApiT (ApiT)
, Iso8601Time (..)
)
import Cardano.Wallet.Api.Types.Error
( ApiErrorBalanceTxUnderestimatedFee (..)
, ApiErrorInfo (..)
, ApiErrorMissingWitnessesInTransaction (..)
, ApiErrorNoSuchPool (..)
, ApiErrorNoSuchWallet (..)
, ApiErrorNodeNotYetInRecentEra (..)
, ApiErrorNotEnoughMoney (..)
, ApiErrorNotEnoughMoneyShortfall (..)
Expand Down Expand Up @@ -254,15 +256,18 @@ instance IsServerError WalletException where
instance IsServerError ErrNoSuchWallet where
toServerError = \case
ErrNoSuchWallet wid ->
apiError err404 NoSuchWallet $ mconcat
flip (apiError err404) message $
NoSuchWallet ApiErrorNoSuchWallet { walletId = ApiT wid }
where
message = mconcat
[ "I couldn't find a wallet with the given id: "
, toText wid
]

instance IsServerError ErrWalletNotInitialized where
toServerError = \case
ErrWalletNotInitialized ->
apiError err404 NoSuchWallet $ mconcat
apiError err404 WalletNotInitialized $ mconcat
[ "The database for the requested wallet is not initialized. "
]

Expand Down
14 changes: 14 additions & 0 deletions lib/api/src/Cardano/Wallet/Api/Types/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module Cardano.Wallet.Api.Types.Error
, ApiErrorNotEnoughMoneyShortfall (..)
, ApiErrorMissingWitnessesInTransaction (..)
, ApiErrorNoSuchPool (..)
, ApiErrorNoSuchWallet (..)
, ApiErrorUnsupportedEra (..)
)
where
Expand All @@ -44,6 +45,7 @@ import Cardano.Wallet.Api.Lib.Options
import Cardano.Wallet.Api.Types
( ApiCosignerIndex (..)
, ApiCredentialType (..)
, ApiT
)
import Cardano.Wallet.Api.Types.Amount
( ApiAmount
Expand All @@ -54,6 +56,9 @@ import Cardano.Wallet.Api.Types.Era
import Cardano.Wallet.Api.Types.WalletAssets
( ApiWalletAssets
)
import Cardano.Wallet.Primitive.Types
( WalletId
)
import Cardano.Wallet.Primitive.Types.Pool
( PoolId
)
Expand Down Expand Up @@ -165,6 +170,8 @@ data ApiErrorInfo
!ApiErrorNoSuchPool
| NoSuchTransaction
| NoSuchWallet
!ApiErrorNoSuchWallet
| WalletNotInitialized
| NoUtxosAvailable
| NodeNotYetInRecentEra
!ApiErrorNodeNotYetInRecentEra
Expand Down Expand Up @@ -329,3 +336,10 @@ data ApiErrorNoSuchPool = ApiErrorNoSuchPool
deriving (Data, Eq, Generic, Show, Typeable)
deriving (FromJSON, ToJSON) via DefaultRecord ApiErrorNoSuchPool
deriving anyclass NFData

data ApiErrorNoSuchWallet = ApiErrorNoSuchWallet
{ walletId :: !(ApiT WalletId)
}
deriving (Data, Eq, Generic, Show, Typeable)
deriving (FromJSON, ToJSON) via DefaultRecord ApiErrorNoSuchWallet
deriving anyclass NFData
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import Cardano.Wallet.Api.Types
, ApiT (..)
, WalletStyle (..)
)
import Cardano.Wallet.Api.Types.Error
( ApiErrorInfo (..)
, ApiErrorNoSuchWallet (ApiErrorNoSuchWallet)
)
import Cardano.Wallet.Primitive.NetworkId
( HasSNetworkId (..)
)
Expand Down Expand Up @@ -63,6 +67,7 @@ import Test.Integration.Framework.DSL
( Context
, Headers (..)
, Payload (..)
, decodeErrorInfo
, emptyIcarusWallet
, emptyIcarusWalletMws
, emptyRandomWallet
Expand All @@ -84,20 +89,19 @@ import Test.Integration.Framework.DSL
, randomAddresses
, request
, verify
, walletId
)
import Test.Integration.Framework.TestData
( errMsg403CouldntIdentifyAddrAsMine
, errMsg403NotAByronWallet
, errMsg403WrongPass
, errMsg404NoWallet
)
import Web.HttpApiData
( ToHttpApiData (..)
)

import qualified Cardano.Wallet.Api.Link as Link
import qualified Network.HTTP.Types.Status as HTTP
import qualified Test.Hspec.Expectations.Lifted as Lifted

spec
:: forall n
Expand Down Expand Up @@ -190,8 +194,9 @@ scenario_ADDRESS_LIST_04 fixture = it title $ \ctx -> runResourceT $ do
r <- request @[ApiAddressWithPath n] ctx (Link.listAddresses @'Byron w) Default Empty
verify r
[ expectResponseCode HTTP.status404
, expectErrorMessage $ errMsg404NoWallet $ w ^. walletId
]
decodeErrorInfo r `Lifted.shouldBe`
NoSuchWallet (ApiErrorNoSuchWallet $ w ^. #id)
where
title = "ADDRESS_LIST_04 - Delete wallet"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import Cardano.Wallet.Api.Types
import Cardano.Wallet.Api.Types.Amount
( ApiAmount (ApiAmount)
)
import Cardano.Wallet.Api.Types.Error
( ApiErrorInfo (..)
, ApiErrorNoSuchWallet (ApiErrorNoSuchWallet)
)
import Cardano.Wallet.Primitive.NetworkId
( HasSNetworkId
)
Expand All @@ -50,6 +54,7 @@ import Test.Integration.Framework.DSL
( Context (..)
, Headers (..)
, Payload (..)
, decodeErrorInfo
, emptyIcarusWallet
, emptyRandomWallet
, emptyWallet
Expand All @@ -64,11 +69,9 @@ import Test.Integration.Framework.DSL
, runResourceT
, selectCoins
, verify
, walletId
)
import Test.Integration.Framework.TestData
( errMsg403NotAnIcarusWallet
, errMsg404NoWallet
)

import qualified Cardano.Wallet.Api.Link as Link
Expand Down Expand Up @@ -150,7 +153,9 @@ spec = describe "BYRON_COIN_SELECTION" $ do
let minUTxOValue' = ApiAmount . minUTxOValue $ _mainEra ctx
let payments = pure (AddressAmount addr minUTxOValue' mempty)
_ <- request @ApiByronWallet ctx (Link.deleteWallet @'Byron icW) Default Empty
selectCoins @_ @'Byron ctx icW payments >>= flip verify
r <- selectCoins @_ @'Byron ctx icW payments
verify r
[ expectResponseCode HTTP.status404
, expectErrorMessage (errMsg404NoWallet $ icW ^. walletId)
]
decodeErrorInfo r `shouldBe`
NoSuchWallet (ApiErrorNoSuchWallet $ icW ^. #id)
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ import Cardano.Wallet.Api.Types.Amount
import Cardano.Wallet.Api.Types.Era
( ApiEra (..)
)
import Cardano.Wallet.Api.Types.Error
( ApiErrorInfo (..)
, ApiErrorNoSuchWallet (ApiErrorNoSuchWallet)
)
import Cardano.Wallet.Primitive.NetworkId
( HasSNetworkId (..)
)
Expand Down Expand Up @@ -82,6 +86,7 @@ import Test.Integration.Framework.DSL
( Context (..)
, Headers (..)
, Payload (..)
, decodeErrorInfo
, emptyIcarusWallet
, emptyRandomWallet
, emptyWallet
Expand Down Expand Up @@ -109,14 +114,14 @@ import Test.Integration.Framework.DSL
import Test.Integration.Framework.TestData
( errMsg403NothingToMigrate
, errMsg403WrongPass
, errMsg404NoWallet
)

import qualified Cardano.Faucet.Mnemonics as Mnemonics
import qualified Cardano.Wallet.Api.Link as Link
import qualified Cardano.Wallet.Api.Types as ApiTypes
import qualified Data.Map.Strict as Map
import qualified Network.HTTP.Types.Status as HTTP
import qualified Test.Hspec.Expectations.Lifted as Lifted

spec :: forall n. HasSNetworkId n => SpecWith Context
spec = describe "BYRON_MIGRATIONS" $ do
Expand Down Expand Up @@ -189,9 +194,9 @@ spec = describe "BYRON_MIGRATIONS" $ do
(Json [json|{addresses: #{targetAddressIds}}|])
verify response
[ expectResponseCode HTTP.status404
, expectErrorMessage
(errMsg404NoWallet $ sourceWallet ^. walletId)
]
decodeErrorInfo response `Lifted.shouldBe`
NoSuchWallet (ApiErrorNoSuchWallet $ sourceWallet ^. #id)

it "BYRON_CREATE_MIGRATION_PLAN_04 - \
\Cannot create a plan for a wallet that only contains dust."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import Cardano.Wallet.Api.Types.Amount
( ApiAmount (ApiAmount)
)
import Cardano.Wallet.Api.Types.Error
( ApiErrorInfo (UtxoTooSmall)
( ApiErrorInfo (..)
, ApiErrorNoSuchWallet (ApiErrorNoSuchWallet)
, ApiErrorTxOutputLovelaceInsufficient (ApiErrorTxOutputLovelaceInsufficient)
)
import Cardano.Wallet.Api.Types.Transaction
Expand Down Expand Up @@ -93,6 +94,7 @@ import Test.Integration.Framework.DSL
, Headers (..)
, Payload (..)
, between
, decodeErrorInfo
, emptyIcarusWallet
, emptyRandomWallet
, emptyWallet
Expand Down Expand Up @@ -132,7 +134,6 @@ import Test.Integration.Framework.Request
import Test.Integration.Framework.TestData
( errMsg400StartTimeLaterThanEndTime
, errMsg404NoAsset
, errMsg404NoWallet
, steveToken
)

Expand Down Expand Up @@ -555,7 +556,8 @@ spec = describe "BYRON_TRANSACTIONS" $ do
let endpoint = "v2/wallets/" <> wid <> "/transactions"
r <- request @(ApiTransaction n) ctx ("POST", endpoint) Default payload
expectResponseCode HTTP.status404 r
expectErrorMessage (errMsg404NoWallet wid) r
decodeErrorInfo r `shouldBe`
NoSuchWallet (ApiErrorNoSuchWallet (w ^. #id))

it "BYRON_TRANS_DELETE -\
\ Cannot delete tx on Byron wallet using shelley ep" $ \ctx -> runResourceT $ do
Expand All @@ -565,7 +567,8 @@ spec = describe "BYRON_TRANSACTIONS" $ do
let endpoint = "v2/wallets/" <> wid <> "/transactions/" <> txid
r <- request @ApiTxId ctx ("DELETE", endpoint) Default Empty
expectResponseCode HTTP.status404 r
expectErrorMessage (errMsg404NoWallet wid) r
decodeErrorInfo r `shouldBe`
NoSuchWallet (ApiErrorNoSuchWallet (w ^. #id))

it "BYRON_TRANS_ESTIMATE -\
\ Cannot estimate tx on Byron wallet using shelley ep" $ \ctx -> runResourceT $ do
Expand All @@ -586,7 +589,8 @@ spec = describe "BYRON_TRANSACTIONS" $ do
let endpoint = "v2/wallets/" <> wid <> "/payment-fees"
r <- request @ApiFee ctx ("POST", endpoint) Default payload
expectResponseCode HTTP.status404 r
expectErrorMessage (errMsg404NoWallet wid) r
decodeErrorInfo r `shouldBe`
NoSuchWallet (ApiErrorNoSuchWallet (w ^. #id))

it "BYRON_TX_LIST_02 -\
\ Byron endpoint does not list Shelley wallet transactions" $ \ctx -> runResourceT $ do
Expand All @@ -596,8 +600,9 @@ spec = describe "BYRON_TRANSACTIONS" $ do
r <- request @([ApiTransaction n]) ctx ep Default Empty
verify r
[ expectResponseCode HTTP.status404
, expectErrorMessage (errMsg404NoWallet wid)
]
decodeErrorInfo r `shouldBe`
NoSuchWallet (ApiErrorNoSuchWallet (w ^. #id))

it "BYRON_TX_LIST_03 -\
\ Shelley endpoint does not list Byron wallet transactions" $ \ctx -> runResourceT $ do
Expand All @@ -607,8 +612,9 @@ spec = describe "BYRON_TRANSACTIONS" $ do
r <- request @([ApiTransaction n]) ctx ep Default Empty
verify r
[ expectResponseCode HTTP.status404
, expectErrorMessage (errMsg404NoWallet wid)
]
decodeErrorInfo r `shouldBe`
NoSuchWallet (ApiErrorNoSuchWallet (w ^. #id))

it "BYRON_RESTORE_09 - Ledger wallet" $ \ctx -> runResourceT $ do
-- NOTE
Expand Down Expand Up @@ -778,7 +784,8 @@ spec = describe "BYRON_TRANSACTIONS" $ do
let link = Link.listTransactions @'Byron w
r <- request @([ApiTransaction n]) ctx link Default Empty
expectResponseCode HTTP.status404 r
expectErrorMessage (errMsg404NoWallet $ w ^. walletId) r
decodeErrorInfo r `shouldBe`
NoSuchWallet (ApiErrorNoSuchWallet (w ^. #id))

describe "BYRON_TX_LIST_ADDRESS - Transactions can be filtered by address" $
forM_ [ (fixtureRandomWallet, emptyRandomWallet, "Byron wallet")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import Cardano.Wallet.Api.Types
import Cardano.Wallet.Api.Types.Amount
( ApiAmount (ApiAmount)
)
import Cardano.Wallet.Api.Types.Error
( ApiErrorInfo (..)
, ApiErrorNoSuchWallet (ApiErrorNoSuchWallet)
)
import Cardano.Wallet.Primitive.NetworkId
( HasSNetworkId
)
Expand Down Expand Up @@ -75,6 +79,7 @@ import Test.Integration.Framework.DSL
( Context (..)
, Headers (..)
, Payload (..)
, decodeErrorInfo
, emptyByronWalletFromXPrvWith
, emptyByronWalletWith
, emptyIcarusWallet
Expand Down Expand Up @@ -106,7 +111,6 @@ import Test.Integration.Framework.TestData
( arabicWalletName
, errMsg400NumberOfWords
, errMsg403WrongPass
, errMsg404NoWallet
, kanjiWalletName
, polishWalletName
, russianWalletName
Expand All @@ -129,7 +133,8 @@ spec = describe "BYRON_WALLETS" $ do
_ <- request @ApiByronWallet ctx (Link.deleteWallet @'Byron w) Default Empty
rg <- request @ApiByronWallet ctx (Link.getWallet @'Byron w) Default Empty
expectResponseCode HTTP.status404 rg
expectErrorMessage (errMsg404NoWallet $ w ^. walletId) rg
decodeErrorInfo rg `shouldBe`
NoSuchWallet (ApiErrorNoSuchWallet (w ^. #id))

it "BYRON_LIST_01 - Byron Wallets are listed from oldest to newest" $
\ctx -> runResourceT $ do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import Cardano.Wallet.Api.Types.Amount
import Cardano.Wallet.Api.Types.Error
( ApiErrorInfo (..)
, ApiErrorMissingWitnessesInTransaction (..)
, ApiErrorNoSuchWallet (ApiErrorNoSuchWallet)
, ApiErrorTxOutputLovelaceInsufficient (ApiErrorTxOutputLovelaceInsufficient)
)
import Cardano.Wallet.Api.Types.Transaction
Expand Down Expand Up @@ -204,7 +205,6 @@ import Test.Integration.Framework.DSL
, verify
, waitForNextEpoch
, waitNumberOfEpochBoundaries
, walletId
, (.>)
)
import Test.Integration.Framework.Request
Expand All @@ -213,7 +213,6 @@ import Test.Integration.Framework.Request
import Test.Integration.Framework.TestData
( errMsg400StartTimeLaterThanEndTime
, errMsg404CannotFindTx
, errMsg404NoWallet
)

import qualified Cardano.Address.Script as CA
Expand Down Expand Up @@ -1609,7 +1608,8 @@ spec = describe "SHARED_TRANSACTIONS" $ do
(Link.listTransactions @'Shared w)
Default Empty
expectResponseCode HTTP.status404 r
expectErrorMessage (errMsg404NoWallet $ w ^. walletId) r
decodeErrorInfo r `shouldBe`
NoSuchWallet (ApiErrorNoSuchWallet (w ^. #id))

it "SHARED_TRANSACTIONS_LIST_RANGE_01 - \
\Transaction at time t is SELECTED by small ranges that cover it" $
Expand Down Expand Up @@ -1741,7 +1741,8 @@ spec = describe "SHARED_TRANSACTIONS" $ do
let link = Link.getTransaction @'Shared w (ApiTxId txid)
r <- request @(ApiTransaction n) ctx link Default Empty
expectResponseCode HTTP.status404 r
expectErrorMessage (errMsg404NoWallet $ w ^. walletId) r
decodeErrorInfo r `shouldBe`
NoSuchWallet (ApiErrorNoSuchWallet (w ^. #id))

it "SHARED_TRANSACTIONS_GET_03 - \
\Using wrong transaction id" $
Expand Down
Loading
Loading