[Haskell-cafe] redirecting ghc (as a library) output

Martin Hilbig martin at mhilbig.de
Sat Feb 27 16:50:40 EST 2010

hi, i'm writing a Haskell View Server for CouchDB.

it communicates with couchdb over stdin and stdout. it gets JSON encoded 
haskell code, compiles it (like on 
http://www.haskell.org/haskellwiki/GHC/As_a_library), gets values, runs 
the given code over the given values and writes the results back (also 
json encoded).

when there is an error in the given haskell code it should reply with 
the error json encoded and exit. but f.e. when the given code imports a 
module, which cannot be found (here Reaction) ghc just spits out a panic 
directly to stdin and exits like this:

 > ViewServer: panic! (the 'impossible' happened)
 >   (GHC version 6.12.1 for x86_64-unknown-linux):
 > Could not find module `Reaction':
 >   Use -v to see a list of the files searched for.
 > Please report this as a GHC bug:  http://www.haskell.org/ghc/reportabug

it puts other errors directly to stdout too, like:

 > Assembler messages:
 > Fatal error: can't create
 > /home/*/AlkylRadicalDecomposition.o:
 > Permission denied

this confuses couchdb, because it expects some JSON and i cant see whats 
going on between them.

now, how can i prevent ghc from using stdout and wrap the output in some 
JSON? is this even possible?

with ghc 6.10 this usage of 'handle' worked for me:

 > main = handle (\e -> do
 >            let e' = show (e::SomeException)
 >            case fromException e of
 >              Just UserInterrupt -> exitSuccess
 >              _ -> do
 >                  let err = error2json "the impossible happened..." e'
 >                  putStrLn err
 >                  logToFile err
 >                  return []) main_loop

did i got the Exception handling wrong?

thanks in advance.

have fun

More information about the Haskell-Cafe mailing list