Skip to content

Commit 6e46a03

Browse files
committed
Handle exceptions at the top level as well
1 parent b20b71e commit 6e46a03

File tree

1 file changed

+95
-54
lines changed

1 file changed

+95
-54
lines changed

kore/app/repl/Main.hs

Lines changed: 95 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,81 @@
55
module Main (main) where
66

77
import Control.Concurrent.MVar
8+
import Control.Monad.Catch
9+
( SomeException
10+
, fromException
11+
, handle
12+
, throwM
13+
)
814
import Data.Reflection
915
import GlobalMain
1016
import Kore.BugReport
11-
import Kore.Exec (
12-
proveWithRepl,
13-
)
14-
import qualified Kore.IndexedModule.MetadataToolsBuilder as MetadataTools (
15-
build,
16-
)
17-
import Kore.Log (
18-
KoreLogOptions (..),
19-
runLoggerT,
20-
swappableLogger,
21-
withLogger,
22-
)
23-
import Kore.Log.KoreLogOptions (
24-
parseKoreLogOptions,
25-
)
26-
import Kore.Log.WarnIfLowProductivity (
27-
warnIfLowProductivity,
28-
)
17+
import Kore.Exec
18+
( proveWithRepl
19+
)
20+
import qualified Kore.IndexedModule.MetadataToolsBuilder as MetadataTools
21+
( build
22+
)
23+
import Kore.Log
24+
( KoreLogOptions (..)
25+
, SomeEntry (..)
26+
, logEntry
27+
, runLoggerT
28+
, swappableLogger
29+
, withLogger
30+
)
31+
import Kore.Log.ErrorException
32+
( errorException
33+
)
34+
import Kore.Log.KoreLogOptions
35+
( parseKoreLogOptions
36+
)
37+
import Kore.Log.WarnIfLowProductivity
38+
( warnIfLowProductivity
39+
)
40+
import qualified Kore.Reachability.Claim as Claim
2941
import Kore.Repl.Data
3042
import Kore.Step.SMT.Lemma
31-
import Kore.Syntax.Module (
32-
ModuleName (..),
33-
)
34-
import Options.Applicative (
35-
InfoMod,
36-
Parser,
37-
argument,
38-
flag,
39-
fullDesc,
40-
header,
41-
help,
42-
long,
43-
metavar,
44-
progDesc,
45-
short,
46-
str,
47-
strOption,
48-
)
49-
import Options.SMT (
50-
KoreSolverOptions (..),
51-
parseKoreSolverOptions,
52-
)
43+
import Kore.Syntax.Module
44+
( ModuleName (..)
45+
)
46+
import Kore.Unparser
47+
( unparseToString
48+
)
49+
import Options.Applicative
50+
( InfoMod
51+
, Parser
52+
, argument
53+
, flag
54+
, fullDesc
55+
, header
56+
, help
57+
, long
58+
, metavar
59+
, progDesc
60+
, short
61+
, str
62+
, strOption
63+
)
64+
import Options.SMT
65+
( KoreSolverOptions (..)
66+
, parseKoreSolverOptions
67+
)
5368
import Prelude.Kore
5469
import qualified SMT
55-
import System.Clock (
56-
Clock (Monotonic),
57-
TimeSpec,
58-
getTime,
59-
)
60-
import System.Exit (
61-
exitFailure,
62-
exitWith,
63-
)
64-
import System.IO (
65-
hPutStrLn,
66-
stderr,
67-
)
70+
import System.Clock
71+
( Clock (Monotonic)
72+
, TimeSpec
73+
, getTime
74+
)
75+
import System.Exit
76+
( exitFailure
77+
, exitWith
78+
)
79+
import System.IO
80+
( hPutStrLn
81+
, stderr
82+
)
6883

6984
-- | Represents a file name along with its module name passed.
7085
data KoreModule = KoreModule
@@ -198,7 +213,7 @@ mainWithOptions
198213
withLogger tempDirectory koreLogOptions $ \actualLogAction -> do
199214
mvarLogAction <- newMVar actualLogAction
200215
let swapLogAction = swappableLogger mvarLogAction
201-
flip runLoggerT swapLogAction $ do
216+
flip runLoggerT swapLogAction $ runExceptionHandlers $ do
202217
definition <- loadDefinitions [definitionFileName, specFile]
203218
indexedModule <- loadModule mainModuleName definition
204219
specDefIndexedModule <- loadModule specModule definition
@@ -254,6 +269,32 @@ mainWithOptions
254269
pure ExitSuccess
255270
exitWith exitCode
256271
where
272+
runExceptionHandlers action =
273+
action
274+
& handle exitReplHandler
275+
& handle withConfigurationHandler
276+
& handle someExceptionHandler
277+
278+
exitReplHandler :: ExitCode -> Main ExitCode
279+
exitReplHandler = pure
280+
281+
someExceptionHandler :: SomeException -> Main ExitCode
282+
someExceptionHandler someException = do
283+
case fromException someException of
284+
Just (SomeEntry entry) -> logEntry entry
285+
Nothing -> errorException someException
286+
throwM someException
287+
288+
withConfigurationHandler :: Claim.WithConfiguration -> Main ExitCode
289+
withConfigurationHandler
290+
(Claim.WithConfiguration lastConfiguration someException) =
291+
do
292+
liftIO $
293+
hPutStrLn
294+
stderr
295+
("// Last configuration:\n" <> unparseToString lastConfiguration)
296+
throwM someException
297+
257298
mainModuleName :: ModuleName
258299
mainModuleName = moduleName definitionModule
259300

0 commit comments

Comments
 (0)