Skip to content

Commit

Permalink
Merge #2059
Browse files Browse the repository at this point in the history
2059: Catch PastHorizon Exception and return 400 with descriptive Error r=hasufell a=hasufell

# Issue Number

#1971 

# Overview

- catch the exception and return 400 with descriptive Error

# Comments

- should this be unit tested? If so, how?

Co-authored-by: Julian Ospald <julian.ospald@iohk.io>
Co-authored-by: Julian Ospald <hasufell@posteo.de>
  • Loading branch information
3 people committed Aug 27, 2020
2 parents ef1ed03 + 8f09068 commit b35d737
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,11 @@ import Cardano.Wallet.Primitive.Model
, updateState
)
import Cardano.Wallet.Primitive.Slotting
( TimeInterpreter, slotRangeFromTimeRange, startTime )
( PastHorizonException (..)
, TimeInterpreter
, slotRangeFromTimeRange
, startTime
)
import Cardano.Wallet.Primitive.SyncProgress
( SyncProgress, SyncTolerance (..), syncProgress )
import Cardano.Wallet.Primitive.Types
Expand Down Expand Up @@ -327,7 +331,7 @@ import Cardano.Wallet.Transaction
import Cardano.Wallet.Unsafe
( unsafeXPrv )
import Control.Exception
( Exception )
( Exception, try )
import Control.Monad
( forM, forM_, replicateM, unless, when )
import Control.Monad.IO.Class
Expand Down Expand Up @@ -1838,7 +1842,12 @@ listTransactions ctx wid mMinWithdrawal mStart mEnd order = db & \DBLayer{..} ->
let err = ErrStartTimeLaterThanEndTime start end
throwE (ErrListTransactionsStartTimeLaterThanEndTime err)
_ -> do
liftIO $ ti $ slotRangeFromTimeRange $ Range mStart mEnd
liftIO (try $ ti $ slotRangeFromTimeRange $ Range mStart mEnd) >>=
\case
Left e@(PastHorizon{}) ->
throwE (ErrListTransactionsPastHorizonException e)
Right r -> pure r


-- | Get transaction and metadata from history for a given wallet.
getTransaction
Expand Down Expand Up @@ -2196,7 +2205,8 @@ data ErrListTransactions
= ErrListTransactionsNoSuchWallet ErrNoSuchWallet
| ErrListTransactionsStartTimeLaterThanEndTime ErrStartTimeLaterThanEndTime
| ErrListTransactionsMinWithdrawalWrong
deriving (Show, Eq)
| ErrListTransactionsPastHorizonException PastHorizonException
deriving (Show)

-- | Errors that can occur when trying to get transaction.
data ErrGetTransaction
Expand Down
10 changes: 10 additions & 0 deletions lib/core/src/Cardano/Wallet/Api/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2312,6 +2312,7 @@ instance LiftHandler ErrListTransactions where
ErrListTransactionsMinWithdrawalWrong ->
apiError err400 MinWithdrawalWrong
"The minimum withdrawal value must be at least 1 Lovelace."
ErrListTransactionsPastHorizonException e -> handler e

instance LiftHandler ErrStartTimeLaterThanEndTime where
handler err = apiError err400 StartTimeLaterThanEndTime $ mconcat
Expand All @@ -2322,6 +2323,15 @@ instance LiftHandler ErrStartTimeLaterThanEndTime where
, "'."
]

instance LiftHandler PastHorizonException where
handler _ = apiError err503 PastHorizon $ mconcat
[ "Tried to convert something that is past the horizon"
, " (due to uncertainty about the next hard fork)."
, " Wait for the node to finish syncing to the hard fork."
, " Depending on the blockchain, this process can take an"
, " unknown amount of time."
]

instance LiftHandler ErrGetTransaction where
handler = \case
ErrGetTransactionNoSuchWallet e -> handler e
Expand Down
1 change: 1 addition & 0 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ data ApiErrorCode
| MinWithdrawalWrong
| AlreadyWithdrawing
| WithdrawalNotWorth
| PastHorizon
deriving (Eq, Generic, Show)

-- | Defines a point in time that can be formatted as and parsed from an
Expand Down

0 comments on commit b35d737

Please sign in to comment.