@@ -1,6 +1,13 @@
1+{-# LANGUAGE CPP #-}
12{-# LANGUAGE RankNTypes #-}
23module Data.Yaml.Include (decodeFile, decodeFileEither) where
34
5+#if !MIN_VERSION_directory(1, 2, 3)
6+import Control.Exception (handleJust)
7+import Control.Monad (guard)
8+import System.IO.Error (ioeGetFileName, ioeGetLocation, isDoesNotExistError)
9+#endif
10+
411import Control.Exception (throwIO)
512import Control.Monad (when)
613import Control.Monad.IO.Class (liftIO)
@@ -25,7 +32,7 @@ eventsFromFile = go []
2532 where
2633 go :: MonadResource m => [FilePath] -> FilePath -> Producer m Event
2734 go seen fp = do
28− cfp <- liftIO $ canonicalizePath fp
35+ cfp <- liftIO $ handleNotFound $ canonicalizePath fp
2936 when (cfp `elem` seen) $ do
3037 liftIO $ throwIO CyclicIncludes
3138 Y.decodeFile cfp $= do
@@ -37,6 +44,17 @@ eventsFromFile = go []
3744
3845 irrelevantEvents = [EventStreamStart, EventDocumentStart, EventDocumentEnd, EventStreamEnd]
3946
47+#if !MIN_VERSION_directory(1, 2, 3)
48+ handleNotFound = handleJust
49+ (\e -> do
50+ guard (isDoesNotExistError e)
51+ guard (ioeGetLocation e == "canonicalizePath")
52+ ioeGetFileName e)
53+ (throwIO . YamlException . ("Yaml file not found: " ++))
54+#else
55+ handleNotFound = id
56+#endif
57+
4058-- | Like `Data.Yaml.decodeFile` but with support for relative and absolute
4159-- includes.
4260--