Skip to content

Commit 87afbd3

Browse files
authored
Merge pull request #5759 from unisonweb/topic/panic
2 parents a9fa074 + 1356ab6 commit 87afbd3

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

unison-runtime/src/Unison/Runtime/Interface.hs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module Unison.Runtime.Interface
2929
where
3030

3131
import Control.Concurrent.STM as STM
32-
import Control.Exception (throwIO)
32+
import Control.Exception (fromException, throwIO, tryJust)
3333
import Control.Monad
3434
import Control.Monad.State
3535
import Data.Binary.Get (runGetOrFail)
@@ -1042,11 +1042,24 @@ evalInContext ppe ctx activeThreads w = do
10421042
decom = decompileCtx crs ctx
10431043
finish = fmap (first listErrors . decom)
10441044

1045-
prettyError (PE _ p) = p
1046-
prettyError (BU tr0 nm c) =
1047-
bugMsg ppe tr nm $ decom c
1045+
prettyError e
1046+
| Just rte <- fromException e = case rte of
1047+
PE _ p -> Just p
1048+
BU tr0 nm c -> Just . bugMsg ppe tr nm $ decom c
1049+
where
1050+
tr = first (backmapRef ctx) <$> tr0
1051+
| Just (Panic msg mval) <- fromException e =
1052+
Just . P.callout panicIcon . P.linesNonEmpty $
1053+
[ P.wrap $
1054+
"The program halted with a runtime panic:",
1055+
"",
1056+
P.string msg
1057+
]
1058+
++ maybe [] (render . decom) mval
1059+
| otherwise = Nothing
10481060
where
1049-
tr = first (backmapRef ctx) <$> tr0
1061+
render (errs, tm) =
1062+
["", P.indentN 2 $ pretty ppe tm, tabulateErrors errs]
10501063

10511064
debugText fancy val = case decom val of
10521065
(errs, dv)
@@ -1060,8 +1073,7 @@ evalInContext ppe ctx activeThreads w = do
10601073

10611074
result <-
10621075
traverse (const $ readIORef r)
1063-
. first prettyError
1064-
<=< try
1076+
<=< tryJust prettyError
10651077
$ apply0 (Just hook) ((ccache ctx) {tracer = debugText}) activeThreads w
10661078
pure $ finish result
10671079

@@ -1183,6 +1195,9 @@ stackTrace ppe tr = "\nStack trace:\n" <> P.indentN 2 (P.lines $ f <$> tr)
11831195
icon :: Pretty ColorText
11841196
icon = "💔💥"
11851197

1198+
panicIcon :: Pretty ColorText
1199+
panicIcon = "💥🤯💥"
1200+
11861201
catchInternalErrors ::
11871202
IO (Either Error a) ->
11881203
IO (Either Error a)

unison-runtime/src/Unison/Runtime/Machine.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,9 @@ encodeExn stk exc = do
469469
(Rf.ioFailureRef, disp be, unitValue)
470470
| Just (ie :: AsyncException) <- fromException exn =
471471
(Rf.threadKilledFailureRef, disp ie, unitValue)
472+
| Just (Panic msg v) <- fromException exn,
473+
msg <- Util.Text.pack $ "panic: " ++ msg =
474+
(Rf.miscFailureRef, msg, fromMaybe unitValue v)
472475
| otherwise = (Rf.miscFailureRef, disp exn, unitValue)
473476

474477
-- | Evaluate a section

0 commit comments

Comments
 (0)