Skip to content

Commit 05222ef

Browse files
authored
Merge pull request #954 from GaloisInc/repl-catch
Catch stack trace exceptions in the REPL
2 parents 404e227 + 1ff3e6d commit 05222ef

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

saw/SAWScript/REPL/Command.hs

+6-7
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,13 @@ genHelp cs = map cmdHelp cs
139139
runCommand :: Command -> REPL ()
140140
runCommand c = case c of
141141

142-
Command cmd -> cmd `SAWScript.REPL.Monad.catch` handler
143-
`SAWScript.REPL.Monad.catchIO` handlerIO
144-
`SAWScript.REPL.Monad.catchFail` handler2
145-
`SAWScript.REPL.Monad.catchTypeErrors` handlerIO
142+
Command cmd -> cmd `SAWScript.REPL.Monad.catch` handlerPP
143+
`SAWScript.REPL.Monad.catchFail` handlerFail
144+
`SAWScript.REPL.Monad.catchOther` handlerPrint
146145
where
147-
handler re = io (putStrLn "" >> print (pp re))
148-
handler2 s = io (putStrLn "" >> putStrLn s)
149-
handlerIO e = io (putStrLn "" >> print e)
146+
handlerPP re = io (putStrLn "" >> print (pp re))
147+
handlerPrint e = io (putStrLn "" >> print e)
148+
handlerFail s = io (putStrLn "" >> putStrLn s)
150149

151150
Unknown cmd -> io (putStrLn ("Unknown command: " ++ cmd))
152151

saw/SAWScript/REPL/Monad.hs

+5-11
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ module SAWScript.REPL.Monad (
1717
, raise
1818
, stop
1919
, catch
20-
, catchIO
2120
, catchFail
22-
, catchTypeErrors
21+
, catchOther
2322

2423
-- ** Errors
2524
, REPLException(..)
@@ -82,7 +81,6 @@ import qualified Data.AIG as AIG
8281
--------------------
8382

8483
import SAWScript.AST (Located(getVal))
85-
import SAWScript.Exceptions
8684
import SAWScript.Interpreter (buildTopLevelEnv)
8785
import SAWScript.Options (Options)
8886
import SAWScript.TopLevel (TopLevelRO(..), TopLevelRW(..))
@@ -214,14 +212,6 @@ raise exn = io (X.throwIO exn)
214212
catchEx :: X.Exception e => REPL a -> (e -> REPL a) -> REPL a
215213
catchEx m k = REPL (\ ref -> unREPL m ref `X.catch` \ e -> unREPL (k e) ref)
216214

217-
-- | Handle 'IOError' exceptions in 'REPL' actions.
218-
catchIO :: REPL a -> (IOError -> REPL a) -> REPL a
219-
catchIO = catchEx
220-
221-
-- | Handle SAWScript type error exceptions in 'REPL' actions.
222-
catchTypeErrors :: REPL a -> (TypeErrors -> REPL a) -> REPL a
223-
catchTypeErrors = catchEx
224-
225215
-- | Handle 'REPLException' exceptions in 'REPL' actions.
226216
catch :: REPL a -> (REPLException -> REPL a) -> REPL a
227217
catch = catchEx
@@ -234,6 +224,10 @@ catchFail m k = REPL (\ ref -> X.catchJust sel (unREPL m ref) (\s -> unREPL (k s
234224
sel e | isUserError e = Just (ioeGetErrorString e)
235225
| otherwise = Nothing
236226

227+
-- | Handle any other exception
228+
catchOther :: REPL a -> (X.SomeException -> REPL a) -> REPL a
229+
catchOther = catchEx
230+
237231
rethrowEvalError :: IO a -> IO a
238232
rethrowEvalError m = run `X.catch` rethrow
239233
where

src/SAWScript/Exceptions.hs

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ data TopLevelException
3131
| CrucibleSetupException ProgramLoc String
3232
| OverrideMatcherException ProgramLoc String
3333
| LLVMMethodSpecException ProgramLoc String
34-
deriving Show
34+
35+
instance Show TopLevelException where
36+
show (TopLevelException _ msg) = msg
37+
show (JavaException _ msg) = msg
38+
show (CrucibleSetupException _ msg) = msg
39+
show (OverrideMatcherException _ msg) = msg
40+
show (LLVMMethodSpecException _ msg) = msg
41+
3542
instance Exception TopLevelException
3643

3744
data TraceException = TraceException String

src/SAWScript/Value.hs

+1-5
Original file line numberDiff line numberDiff line change
@@ -988,11 +988,7 @@ addTraceIO str action = X.catches action
988988
where
989989
rethrow msg = X.throwIO . SS.TraceException $ mconcat [str, ":\n", msg]
990990
handleTopLevel :: SS.TopLevelException -> IO a
991-
handleTopLevel (SS.TopLevelException _pos msg) = rethrow msg
992-
handleTopLevel (SS.JavaException _pos msg) = rethrow msg
993-
handleTopLevel (SS.CrucibleSetupException _loc msg) = rethrow msg
994-
handleTopLevel (SS.OverrideMatcherException _loc msg) = rethrow msg
995-
handleTopLevel (SS.LLVMMethodSpecException _loc msg) = rethrow msg
991+
handleTopLevel e = rethrow $ show e
996992
handleTrace (SS.TraceException msg) = rethrow msg
997993
handleIO :: X.IOException -> IO a
998994
handleIO e

0 commit comments

Comments
 (0)