Skip to content

Commit f0f0483

Browse files
committed
lib: defaultJournalSafely wasn't very safe; now catches error calls
1 parent bf40d28 commit f0f0483

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

hledger-lib/Hledger/Read.hs

+10-3
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,13 @@ defaultJournalSafely = defaultJournalSafelyWith definputopts
196196
-- | Read the default journal file specified by the environment,
197197
-- with the given input options, or return an error message.
198198
defaultJournalSafelyWith :: InputOpts -> IO (Either String Journal)
199-
defaultJournalSafelyWith iopts = defaultJournalPath >>= runExceptT . readJournalFile iopts
200-
199+
defaultJournalSafelyWith iopts = (do
200+
f <- defaultJournalPath
201+
runExceptT $ readJournalFile iopts f
202+
) `C.catches` [ -- XXX
203+
C.Handler (\(e :: C.ErrorCall) -> return $ Left $ rstrip $ show e)
204+
,C.Handler (\(e :: C.IOException) -> return $ Left $ rstrip $ show e)
205+
]
201206
-- | Get the default journal file path specified by the environment.
202207
-- Like ledger, we look first for the LEDGER_FILE environment
203208
-- variable, and if that does not exist, for the legacy LEDGER
@@ -256,6 +261,8 @@ readJournal iopts@InputOpts{strict_, _defer} mpath hdl = do
256261

257262
-- | Read a Journal from this file, or from stdin if the file path is -,
258263
-- with strict checks if enabled, or return an error message.
264+
-- XXX or, calls error if the file does not exist.
265+
--
259266
-- (Note strict checks are disabled temporarily here when this is called by readJournalFiles).
260267
-- The file path can have a READER: prefix.
261268
--
@@ -364,7 +371,7 @@ readJournalFiles' = orDieTrying . readJournalFiles definputopts
364371
orDieTrying :: MonadIO m => ExceptT String m a -> m a
365372
orDieTrying a = either (liftIO . fail) return =<< runExceptT a
366373

367-
-- | If the specified journal file does not exist (and is not "-"), give a helpful error and quit.
374+
-- | If the specified journal file does not exist (and is not "-"), call error with an informative message.
368375
-- (Using "journal file" generically here; it could be in any of hledger's supported formats.)
369376
requireJournalFileExists :: FilePath -> IO ()
370377
requireJournalFileExists "-" = return ()

0 commit comments

Comments
 (0)