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.hsindex 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.hsindex 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 whereimport Protoludeimport Data.Text.Prettyprint.Doc (PageWidth)-import System.Console.Haskeline+import System.Console.Haskeline hiding (catch)#ifdef ghcjs_HOST_OSimport GHCJS.DOM.XMLHttpRequest(getResponseText, newXMLHttpRequest, openSimple, send)@@ -55,7 +55,7 @@ class (Monad m) => SetSubs m wheresetSubS :: 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_OSinstance ReadFile (InputT IO) wherereadFileS = lift . requestFile@@ -67,11 +67,12 @@ instance ReadFile (InputT IO) wheresend reqresp <- getResponseText reqpure $ case resp of- Nothing -> ""- Just v -> v+ Nothing -> Left "no response from server"+ Just v -> Right v#elseinstance ReadFile (InputT IO) where- readFileS = lift . readFile . toS+ readFileS fname = lift $ (Right <$> readFile (toS fname))+ `catch` (\(e :: IOException) -> pure (Left (show e)))#endifinstance {-# OVERLAPPABLE #-} ReadFile m => ReadFile (Lang m) wherereadFileS = lift . readFileSdiff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hsindex 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 wherereadFileS fn = dofs <- lift $ gets worldStateFilescase 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: " <> fninstance MonadRandom (State WorldState) wheregetRandomBytes i = dodiff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex 9663b8b..d99aca5 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -4,7 +4,7 @@ module Radicle.Tests whereimport 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 Mapimport Data.Maybe (fromJust)import Data.Scientific (Scientific)@@ -671,6 +671,20 @@ test_repl =](_, result) <- runInRepl inputresult @==> 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