Skip to content

A peer-to-peer stack for code collaboration

Fixes issue #199 in which load! exceptions would kill the repl

Luke Palmerd47b51ca477a
diff --git a/src/Radicle/Internal/Effects.hs b/src/Radicle/Internal/Effects.hs
index eece9c6..f9140f5 100644
--- a/src/Radicle/Internal/Effects.hs
+++ b/src/Radicle/Internal/Effects.hs
@@ -165,14 +165,18 @@ replPrimFns = fromList $ allDocs $
, ( "read-file!"
, [md|Reads the contents of a file and returns it as a string.|]
, oneArg "read-file" $ \case
- String filename -> String <$> readFileS filename
+ String filename -> readFileS filename >>= \case
+ Left err -> throwErrorHere . OtherError $ "Error reading file: " <> err
+ Right text -> pure $ String text
_ -> throwErrorHere $ TypeError "read-file: expects a string"
)
, ( "load!"
, [md|Evaluates the contents of a file. Each seperate radicle expression is
`eval`uated according to the current definition of `eval`.|]
, oneArg "load!" $ \case
- String filename -> readFileS filename >>= interpretMany ("[load! " <> filename <> "]")
+ String filename -> readFileS filename >>= \case
+ Left err -> throwErrorHere . OtherError $ "Error reading file: " <> err
+ Right text -> interpretMany ("[load! " <> filename <> "]") text
_ -> throwErrorHere $ TypeError "load: expects a string"
)
, ( "gen-key-pair!"
diff --git a/src/Radicle/Internal/Effects/Capabilities.hs b/src/Radicle/Internal/Effects/Capabilities.hs
index a0a58df..7ae3927 100644
--- a/src/Radicle/Internal/Effects/Capabilities.hs
+++ b/src/Radicle/Internal/Effects/Capabilities.hs
@@ -8,7 +8,7 @@ module Radicle.Internal.Effects.Capabilities where
import Protolude
import Data.Text.Prettyprint.Doc (PageWidth)
-import System.Console.Haskeline
+import System.Console.Haskeline hiding (catch)
#ifdef ghcjs_HOST_OS
import GHCJS.DOM.XMLHttpRequest
(getResponseText, newXMLHttpRequest, openSimple, send)
@@ -55,7 +55,7 @@ class (Monad m) => SetSubs m where
setSubS :: Text -> (Value -> m ()) -> m ()
class (Monad m) => ReadFile m where
- readFileS :: Text -> m Text
+ readFileS :: Text -> m (Either Text Text) -- ^ Left error or Right contents
#ifdef ghcjs_HOST_OS
instance ReadFile (InputT IO) where
readFileS = lift . requestFile
@@ -67,11 +67,12 @@ instance ReadFile (InputT IO) where
send req
resp <- getResponseText req
pure $ case resp of
- Nothing -> ""
- Just v -> v
+ Nothing -> Left "no response from server"
+ Just v -> Right v
#else
instance ReadFile (InputT IO) where
- readFileS = lift . readFile . toS
+ readFileS fname = lift $ (Right <$> readFile (toS fname))
+ `catch` (\(e :: IOException) -> pure (Left (show e)))
#endif
instance {-# OVERLAPPABLE #-} ReadFile m => ReadFile (Lang m) where
readFileS = lift . readFileS
diff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hs
index 1b5d938..bbfb0b1 100644
--- a/test/spec/Radicle/Internal/TestCapabilities.hs
+++ b/test/spec/Radicle/Internal/TestCapabilities.hs
@@ -95,8 +95,8 @@ instance {-# OVERLAPPING #-} ReadFile TestLang where
readFileS fn = do
fs <- lift $ gets worldStateFiles
case Map.lookup fn fs of
- Just f -> pure f
- Nothing -> throwErrorHere . OtherError $ "File not found: " <> fn
+ Just f -> pure $ Right f
+ Nothing -> pure . Left $ "File not found: " <> fn
instance MonadRandom (State WorldState) where
getRandomBytes i = do
diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hs
index 9663b8b..d99aca5 100644
--- a/test/spec/Radicle/Tests.hs
+++ b/test/spec/Radicle/Tests.hs
@@ -4,7 +4,7 @@ module Radicle.Tests where
import Protolude hiding (toList)
import Codec.Serialise (Serialise, deserialise, serialise)
-import Data.List (isInfixOf, isSuffixOf)
+import Data.List (isInfixOf, isSuffixOf, last)
import qualified Data.Map.Strict as Map
import Data.Maybe (fromJust)
import Data.Scientific (Scientific)
@@ -671,6 +671,20 @@ test_repl =
]
(_, result) <- runInRepl input
result @==> output
+
+ , testCase "exceptions are non-fatal" $ do
+ let input = [ "(throw 'something \"something happened\")"
+ , "#t"
+ ]
+ (_, result) <- runInRepl input
+ last result @?= "#t"
+
+ , testCase "load! a non-existent file is a non-fatal exception" $ do
+ let input = [ "(load! \"not-a-thing.rad\")"
+ , "#t"
+ ]
+ (_, result) <- runInRepl input
+ last result @?= "#t"
]
where
-- In addition to the output of the lines tested, 'should-be's get