dev: rename exitOnError -> handleExit, improve doc

This commit is contained in:
Simon Michael 2025-06-12 18:37:40 -10:00
parent 0750a27d00
commit 57031b0e78
4 changed files with 27 additions and 26 deletions

View File

@ -27,7 +27,7 @@ module Hledger.Utils.IO (
ansiFormatWarning, ansiFormatWarning,
printError, printError,
exitWithErrorMessage, exitWithErrorMessage,
exitOnError, handleExit,
-- * Time -- * Time
getCurrentLocalTime, getCurrentLocalTime,
@ -262,39 +262,40 @@ then exit the program with a non-zero exit code.
exitWithErrorMessage :: String -> IO () exitWithErrorMessage :: String -> IO ()
exitWithErrorMessage msg = printError msg >> exitFailure exitWithErrorMessage msg = printError msg >> exitFailure
-- | This wraps a program's main routine so as to display more consistent -- | This wraps a program's main routine so as to display more consistent,
-- and useful error output for some common program-terminating exceptions, -- useful, and GHC-version-independent error output when the program exits
-- independent of compiler version. It catches: -- because of certain common exceptions. It
-- --
-- - UnicodeException - unicode errors in pure code -- 1. disables SIGPIPE errors, which are usually harmless,
-- caused when our output is truncated in a piped command.
-- --
-- - IOException AKA IOError - I/O errors, including unicode errors during I/O -- 2. catches these common exceptions:
-- --
-- - ErrorCall - @error@ / @errorWithoutStackTrace@ calls -- - UnicodeException, caused eg by text decoding errors in pure code
-- --
-- and: -- - IOException, caused by I/O errors, including text decoding errors during I/O
--
-- - removes the trailing newlines added by some GHC 9.10.*
-- --
-- - removes "uncaught exception" output added by some GHC 9.12.* -- - ErrorCall - @error@ / @errorWithoutStackTrace@ calls
-- --
-- - ensures a consistent "programname: " prefix -- 3. compensates for GHC output bugs:
-- --
-- - applies ANSI styling (bold bright red) to the first line if that is supported and allowed -- - removes the trailing newlines added by some GHC 9.10.* versions
-- --
-- - for unicode exceptions, and I/O exceptions which look like they were -- - removes "uncaught exception" output added by some GHC 9.12.* versions
-- caused by a unicode error (usually text decoding failure),
-- it adds (english) text explaining the problem and what to do.
-- --
-- Some exceptions this does not handle: -- - ensures a consistent "PROGNAME: " prefix
-- ExitCode (exitSuccess/exitFailure/exitWith),
-- UserInterrupt (control-C).
-- --
-- Also, this ignores SIGPIPE errors, which are usually harmless, -- 4. applies bold bright red ANSI styling to the first line of error output,
-- caused when our output is truncated in a piped command. -- if that is supported and allowed
-- --
exitOnError :: IO () -> IO () -- 5. for unicode exceptions and I/O exceptions which look like they were
exitOnError = flip catches [ -- unicode-related, it adds a message (in english) explaining the problem and what to do.
--
-- Some exceptions this does not catch are ExitCode (exitSuccess/exitFailure/exitWith)
-- and UserInterrupt (control-C).
--
handleExit :: IO () -> IO ()
handleExit = flip catches [
-- Handler (\(e::SomeException) -> error' $ pshow e), -- debug -- Handler (\(e::SomeException) -> error' $ pshow e), -- debug
Handler (\(e::UnicodeException) -> exitUnicode e) Handler (\(e::UnicodeException) -> exitUnicode e)
,Handler (\(e::IOException) -> if ,Handler (\(e::IOException) -> if

View File

@ -76,7 +76,7 @@ writeChan = BC.writeBChan
hledgerUiMain :: IO () hledgerUiMain :: IO ()
hledgerUiMain = exitOnError $ withGhcDebug' $ withProgName "hledger-ui.log" $ do -- force Hledger.Utils.Debug.* to log to hledger-ui.log hledgerUiMain = handleExit $ withGhcDebug' $ withProgName "hledger-ui.log" $ do -- force Hledger.Utils.Debug.* to log to hledger-ui.log
when (ghcDebugMode == GDPauseAtStart) $ ghcDebugPause' when (ghcDebugMode == GDPauseAtStart) $ ghcDebugPause'
#if MIN_VERSION_base(4,20,0) #if MIN_VERSION_base(4,20,0)

View File

@ -61,7 +61,7 @@ hledgerWebDev =
-- Run normally. -- Run normally.
hledgerWebMain :: IO () hledgerWebMain :: IO ()
hledgerWebMain = exitOnError $ withGhcDebug' $ do hledgerWebMain = handleExit $ withGhcDebug' $ do
when (ghcDebugMode == GDPauseAtStart) $ ghcDebugPause' when (ghcDebugMode == GDPauseAtStart) $ ghcDebugPause'
#if MIN_VERSION_base(4,20,0) #if MIN_VERSION_base(4,20,0)

View File

@ -200,7 +200,7 @@ confflagsmode = defMode{
-- implementing that would simplify hledger's CLI processing a lot. -- implementing that would simplify hledger's CLI processing a lot.
-- --
main :: IO () main :: IO ()
main = exitOnError $ withGhcDebug' $ do main = handleExit $ withGhcDebug' $ do
#if MIN_VERSION_base(4,20,0) #if MIN_VERSION_base(4,20,0)
-- Control ghc 9.10+'s stack traces. -- Control ghc 9.10+'s stack traces.