on master help sign in
Catch errors in compilers
f1a19c8Jasper Van der Jeugt
src/Hakyll/Core/Compiler.hs+5−3
@@ -88,7 +88,7 @@
88-- the type @a@. It is /very/ important that the compiler which produced this
88-- the type @a@. It is /very/ important that the compiler which produced this
89-- value, produced the right type as well!
89-- value, produced the right type as well!
90--
90--
91−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
91+{-# LANGUAGE GeneralizedNewtypeDeriving, ScopedTypeVariables #-}
92module Hakyll.Core.Compiler
92module Hakyll.Core.Compiler
93 ( Compiler
93 ( Compiler
94 , runCompiler
94 , runCompiler
@@ -118,6 +118,7 @@ module Hakyll.Core.Compiler
118import Prelude hiding ((.), id)
118import Prelude hiding ((.), id)
119import Control.Arrow ((>>>), (&&&), arr, first)
119import Control.Arrow ((>>>), (&&&), arr, first)
120import Control.Applicative ((<$>))
120import Control.Applicative ((<$>))
121+import Control.Exception (SomeException, handle)
121import Control.Monad.Reader (ask)
122import Control.Monad.Reader (ask)
122import Control.Monad.Trans (liftIO)
123import Control.Monad.Trans (liftIO)
123import Control.Monad.Error (throwError)
124import Control.Monad.Error (throwError)
@@ -155,8 +156,9 @@ runCompiler :: Compiler () CompileRule -- ^ Compiler to run
155 -> IO (Throwing CompileRule) -- ^ Resulting item
156 -> IO (Throwing CompileRule) -- ^ Resulting item
156runCompiler compiler id' provider universe routes store modified logger = do
157runCompiler compiler id' provider universe routes store modified logger = do
157 -- Run the compiler job
158 -- Run the compiler job
159+ result <- handle (\(e :: SomeException) -> return $ Left $ show e) $
158− result <- runCompilerJob compiler id' provider universe
160+ runCompilerJob compiler id' provider universe routes store modified
159− routes store modified logger
161+ logger
160
162
161 -- Inspect the result
163 -- Inspect the result
162 case result of
164 case result of
src/Hakyll/Core/Run.hs+26−21
@@ -5,33 +5,35 @@ module Hakyll.Core.Run
5 ( run
5 ( run
6 ) where
6 ) where
7
7
8−import Prelude hiding (reverse)
9−import Control.Monad (filterM, forM_)
10−import Control.Monad.Trans (liftIO)
11import Control.Applicative (Applicative, (<$>))
8import Control.Applicative (Applicative, (<$>))
9+import Control.Exception (handle)
10+import Control.Monad (filterM, forM_)
11+import Control.Monad.Error (ErrorT, runErrorT, throwError)
12import Control.Monad.Reader (ReaderT, runReaderT, ask)
12import Control.Monad.Reader (ReaderT, runReaderT, ask)
13import Control.Monad.State.Strict (StateT, runStateT, get, put)
13import Control.Monad.State.Strict (StateT, runStateT, get, put)
14+import Control.Monad.Trans (liftIO)
14import Data.Map (Map)
15import Data.Map (Map)
15−import qualified Data.Map as M
16import Data.Monoid (mempty, mappend)
16import Data.Monoid (mempty, mappend)
17+import Prelude hiding (reverse)
17import System.FilePath ((</>))
18import System.FilePath ((</>))
19+import qualified Data.Map as M
18import qualified Data.Set as S
20import qualified Data.Set as S
19
21
20−import Hakyll.Core.Routes
21−import Hakyll.Core.Identifier
22−import Hakyll.Core.Util.File
23import Hakyll.Core.Compiler
22import Hakyll.Core.Compiler
24import Hakyll.Core.Compiler.Internal
23import Hakyll.Core.Compiler.Internal
24+import Hakyll.Core.Configuration
25+import Hakyll.Core.DependencyAnalyzer
26+import Hakyll.Core.DirectedGraph
27+import Hakyll.Core.Identifier
28+import Hakyll.Core.Logger
25import Hakyll.Core.Resource
29import Hakyll.Core.Resource
26import Hakyll.Core.Resource.Provider
30import Hakyll.Core.Resource.Provider
27import Hakyll.Core.Resource.Provider.File
31import Hakyll.Core.Resource.Provider.File
32+import Hakyll.Core.Routes
28import Hakyll.Core.Rules.Internal
33import Hakyll.Core.Rules.Internal
29−import Hakyll.Core.DirectedGraph
30−import Hakyll.Core.DependencyAnalyzer
31−import Hakyll.Core.Writable
32import Hakyll.Core.Store
34import Hakyll.Core.Store
33−import Hakyll.Core.Configuration
35+import Hakyll.Core.Util.File
34−import Hakyll.Core.Logger
36+import Hakyll.Core.Writable
35
37
36-- | Run all rules needed, return the rule set used
38-- | Run all rules needed, return the rule set used
37--
39--
@@ -66,14 +68,18 @@ run configuration rules = do
66 }
68 }
67
69
68 -- Run the program and fetch the resulting state
70 -- Run the program and fetch the resulting state
69− ((), state') <- runStateT stateT $ RuntimeState
71+ result <- runErrorT $ runStateT stateT $ RuntimeState
70 { hakyllAnalyzer = makeDependencyAnalyzer mempty (const False) oldGraph
72 { hakyllAnalyzer = makeDependencyAnalyzer mempty (const False) oldGraph
71 , hakyllCompilers = M.empty
73 , hakyllCompilers = M.empty
72 }
74 }
73
75
76+ case result of
77+ Left e ->
78+ thrown logger e
79+ Right ((), state') ->
74− -- We want to save the final dependency graph for the next run
80+ -- We want to save the final dependency graph for the next run
75− storeSet store "Hakyll.Core.Run.run" "dependencies" $
81+ storeSet store "Hakyll.Core.Run.run" "dependencies" $
76− analyzerGraph $ hakyllAnalyzer state'
82+ analyzerGraph $ hakyllAnalyzer state'
77
83
78 -- Flush and return
84 -- Flush and return
79 flushLogger logger
85 flushLogger logger
@@ -94,7 +100,8 @@ data RuntimeState = RuntimeState
94 }
100 }
95
101
96newtype Runtime a = Runtime
102newtype Runtime a = Runtime
97− { unRuntime :: ReaderT RuntimeEnvironment (StateT RuntimeState IO) a
103+ { unRuntime :: ReaderT RuntimeEnvironment
104+ (StateT RuntimeState (ErrorT String IO)) a
98 } deriving (Functor, Applicative, Monad)
105 } deriving (Functor, Applicative, Monad)
99
106
100-- | Add a number of compilers and continue using these compilers
107-- | Add a number of compilers and continue using these compilers
@@ -205,7 +212,5 @@ build id' = Runtime $ do
205 -- Actually I was just kidding, it's not hard at all
212 -- Actually I was just kidding, it's not hard at all
206 unRuntime $ addNewCompilers newCompilers
213 unRuntime $ addNewCompilers newCompilers
207
214
208− -- Some error happened, log and continue
215+ -- Some error happened, rethrow in Runtime monad
209− Left err -> do
216+ Left err -> throwError err
210− thrown logger err
211− unRuntime stepAnalyzer