Skip to content

Commit

Permalink
[ADP-3306] Make NoSuchWallet error machine-readable. (#4597)
Browse files Browse the repository at this point in the history
- [x] Extend `NoSuchWallet` to include `WalletId`.
- [x] Adjust unit tests and swagger accordingly.
- [x] use `NoSuchWallet` rather than `err403NoSuchErrorMsg` throughout
integration tests

### Issue
ADP-3306
  • Loading branch information
paweljakubas authored May 23, 2024
2 parents 67d134f + 7050122 commit 8faf0a2
Show file tree
Hide file tree
Showing 21 changed files with 265 additions and 981 deletions.
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

0 comments on commit 8faf0a2

Please sign in to comment.