Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing "runghc" in Snap and not throw a webhandler exception. Using tryIO still gives an error
    primarykey
    data
    text
    <p>I am trying to use runGHC inside of snap to filter out only code that can be compiled. However, I am using tryIO but still my webhandler throws an exception when there is a compile error instead of just returning an empty String.</p> <pre><code>import Exception (tryIO) ... runOnFileName :: String -&gt; IO (String) runOnFileName inp = do res &lt;- sanitizeSource inp case res of Just (code, _, _, _) -&gt; return $ ppr code Nothing -&gt; return "" sanitizeSourceString :: String -&gt; String -&gt; IO (String) sanitizeSourceString fn contents = do tmpdir &lt;- getTemporaryDirectory let tmp = tmpdir &lt;/&gt; fn ++ ".hs" exists &lt;- doesFileExist tmp unless exists $ writeFile tmp $ contents runOnFileName tmp sanitizeSource :: String -&gt; IO (Maybe RenamedSource) sanitizeSource inp = do runGhc (Just libdir) $ do dflags &lt;- getSessionDynFlags let dflags' = foldl xopt_set dflags [Opt_Cpp, Opt_ImplicitPrelude, Opt_MagicHash] setSessionDynFlags dflags target &lt;- guessTarget inp Nothing setTargets [target] load LoadAllTargets modSum &lt;- getModSummary $ mkModuleName "Main" p &lt;- parseModule modSum t &lt;- typecheckModule p d &lt;- desugarModule t return $ renamedSource d ... in my handler... eitherSan &lt;- liftIO $ tryIO $ sanitizeSourceString (T.unpack uuid) (fromMaybe "" content) let sanitized = case eitherSan of Left _ -&gt; "" Right r -&gt; r </code></pre> <p>However, if I pass "content" that does not compile, my handler fails with a</p> <pre><code> A web handler threw an exception. Details Parse error: naked expression at top level </code></pre> <p>or whatever the compiler error is. I thought <code>tryIO</code> was supposed to catch the exception.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload