module Cardano.CLI.TopHandler
( toplevelExceptionHandler
)
where
import Prelude
import Control.Exception
import System.Environment
import System.Exit
import System.IO
toplevelExceptionHandler :: IO a -> IO a
toplevelExceptionHandler :: forall a. IO a -> IO a
toplevelExceptionHandler IO a
prog = do
Handle -> BufferMode -> IO ()
hSetBuffering Handle
stderr BufferMode
LineBuffering
IO a -> [Handler a] -> IO a
forall a. IO a -> [Handler a] -> IO a
catches
IO a
prog
[ (SomeAsyncException -> IO a) -> Handler a
forall a e. Exception e => (e -> IO a) -> Handler a
Handler SomeAsyncException -> IO a
forall a. SomeAsyncException -> IO a
rethrowAsyncExceptions
, (ExitCode -> IO a) -> Handler a
forall a e. Exception e => (e -> IO a) -> Handler a
Handler ExitCode -> IO a
forall a. ExitCode -> IO a
rethrowExitCode
, (SomeException -> IO a) -> Handler a
forall a e. Exception e => (e -> IO a) -> Handler a
Handler SomeException -> IO a
forall a. SomeException -> IO a
handleSomeException
]
where
rethrowAsyncExceptions :: SomeAsyncException -> IO a
rethrowAsyncExceptions :: forall a. SomeAsyncException -> IO a
rethrowAsyncExceptions = SomeAsyncException -> IO a
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO
rethrowExitCode :: ExitCode -> IO a
rethrowExitCode :: forall a. ExitCode -> IO a
rethrowExitCode = ExitCode -> IO a
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO
handleSomeException :: SomeException -> IO a
handleSomeException :: forall a. SomeException -> IO a
handleSomeException SomeException
e = do
Handle -> IO ()
hFlush Handle
stdout
progname <- IO [Char]
getProgName
hPutStr stderr (renderSomeException progname e)
throwIO (ExitFailure 1)
renderSomeException :: String -> SomeException -> String
renderSomeException :: [Char] -> SomeException -> [Char]
renderSomeException [Char]
progname SomeException
e
| [Char]
showOutput [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
/= [Char]
displayOutput =
[Char]
showOutput [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"\n\n" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
progname [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
": " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
displayOutput
| Bool
otherwise =
[Char]
"\n" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
progname [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
": " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
showOutput
where
showOutput :: [Char]
showOutput = SomeException -> [Char]
forall a. Show a => a -> [Char]
show SomeException
e
displayOutput :: [Char]
displayOutput = SomeException -> [Char]
forall e. Exception e => e -> [Char]
displayException SomeException
e