Skip to content

Commit

Permalink
NetworkLayer.listen: don't "die" when node backend fails
Browse files Browse the repository at this point in the history
"die" will exit the process, which might not be what we want in all
cases. Throw an exception instead, which will also result in the
process exiting, but could be caught if needed.
  • Loading branch information
rvl committed Apr 4, 2019
1 parent efde04f commit 366200b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import Cardano.Wallet.Primitive.Model
( Wallet, applyBlock, initWallet )
import Cardano.Wallet.Primitive.Types
( Block (..), WalletId (..), WalletMetadata (..), WalletName (..) )
import Control.Exception
( Exception )
import Control.Monad.IO.Class
( liftIO )
import Control.Monad.Trans.Except
Expand Down Expand Up @@ -84,7 +86,7 @@ newtype CreateWalletError

-- | Create a new instance of the wallet layer.
mkWalletLayer
:: (Show e0)
:: (Exception e0)
=> DBLayer IO SeqState
-> NetworkLayer IO e0 e1
-> WalletLayer SeqState
Expand Down
11 changes: 4 additions & 7 deletions src/Cardano/Wallet/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ import Cardano.Wallet.Primitive.Types
( Block (..), BlockHeader (..), Hash (..), SignedTx, SlotId (..) )
import Control.Concurrent
( threadDelay )
import Control.Exception
( Exception, throwIO )
import Control.Monad.IO.Class
( MonadIO, liftIO )
import Control.Monad.Trans.Except
( ExceptT, runExceptT )
import Data.Time.Units
( Millisecond, toMicroseconds )
import Fmt
( fmt, (+||), (||+) )
import System.Exit
( die )


data NetworkLayer m e0 e1 = NetworkLayer
Expand Down Expand Up @@ -75,7 +73,7 @@ data TickResult a
-- | Retrieve blocks from a chain producer and execute some given action for
-- each block.
listen
:: forall e0 e1. (Show e0)
:: forall e0 e1. (Exception e0)
=> NetworkLayer IO e0 e1
-> ([Block] -> IO ())
-> IO ()
Expand All @@ -93,8 +91,7 @@ listen network action = do
-- which is fair price to pay in order NOT to have to do any slotting
-- arithmetic nor track how many blocks are present per epochs.
case res of
Left err ->
die $ fmt $ "Chain producer error: "+||err||+""
Left err -> throwIO err
Right bs | length bs < 2 ->
pure (Sleep, current)
Right blocks ->
Expand Down
9 changes: 8 additions & 1 deletion src/Cardano/Wallet/Network/HttpBridge.hs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,14 @@ data HttpBridgeError
-- ^ Could not connect to or read from the node API.
| BadResponseFromNode String
-- ^ The node returned an unexpected response.
deriving (Show, Eq)
deriving (Eq)

instance Show HttpBridgeError where
show (NodeUnavailable msg) =
"Could not connect to or read from the node API: " ++ msg
show (BadResponseFromNode msg) =
"The node returned an unexpected response: " ++ msg
instance Exception HttpBridgeError

blockHeaderHash
:: WithHash algorithm (ApiT BlockHeader)
Expand Down

0 comments on commit 366200b

Please sign in to comment.