on master help sign in
Catch errors in compilers
commitf1a19c860f115dda68bc9ee3bff2fb1059ee1b08 authorJasper Van der Jeugt <m@jaspervdj.be> dateSun 18 Mar 2012, 10:44 (15 years ago) parent7184769
2 files changed+31−24
src/Hakyll/Core/Compiler.hs+5−3 src/Hakyll/Core/Run.hs+26−21
src/Hakyll/Core/Compiler.hs+5−3
@@ -88,7 +88,7 @@
8888-- the type @a@. It is /very/ important that the compiler which produced this
8989-- value, produced the right type as well!
9090--
91−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
91+{-# LANGUAGE GeneralizedNewtypeDeriving, ScopedTypeVariables #-}
9292module Hakyll.Core.Compiler
9393 ( Compiler
9494 , runCompiler
@@ -118,6 +118,7 @@ module Hakyll.Core.Compiler
118118import Prelude hiding ((.), id)
119119import Control.Arrow ((>>>), (&&&), arr, first)
120120import Control.Applicative ((<$>))
121+import Control.Exception (SomeException, handle)
121122import Control.Monad.Reader (ask)
122123import Control.Monad.Trans (liftIO)
123124import Control.Monad.Error (throwError)
@@ -155,8 +156,9 @@ runCompiler :: Compiler () CompileRule -- ^ Compiler to run
155156 -> IO (Throwing CompileRule) -- ^ Resulting item
156157runCompiler compiler id' provider universe routes store modified logger = do
157158 -- Run the compiler job
158− result <- runCompilerJob compiler id' provider universe
159− routes store modified logger
159+ result <- handle (\(e :: SomeException) -> return $ Left $ show e) $
160+ runCompilerJob compiler id' provider universe routes store modified
161+ logger
160162
161163 -- Inspect the result
162164 case result of
src/Hakyll/Core/Run.hs+26−21
@@ -5,33 +5,35 @@ module Hakyll.Core.Run
55 ( run
66 ) where
77
8−import Prelude hiding (reverse)
9−import Control.Monad (filterM, forM_)
10−import Control.Monad.Trans (liftIO)
118import Control.Applicative (Applicative, (<$>))
9+import Control.Exception (handle)
10+import Control.Monad (filterM, forM_)
11+import Control.Monad.Error (ErrorT, runErrorT, throwError)
1212import Control.Monad.Reader (ReaderT, runReaderT, ask)
1313import Control.Monad.State.Strict (StateT, runStateT, get, put)
14+import Control.Monad.Trans (liftIO)
1415import Data.Map (Map)
15−import qualified Data.Map as M
1616import Data.Monoid (mempty, mappend)
17+import Prelude hiding (reverse)
1718import System.FilePath ((</>))
19+import qualified Data.Map as M
1820import qualified Data.Set as S
1921
20−import Hakyll.Core.Routes
21−import Hakyll.Core.Identifier
22−import Hakyll.Core.Util.File
2322import Hakyll.Core.Compiler
2423import 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
2529import Hakyll.Core.Resource
2630import Hakyll.Core.Resource.Provider
2731import Hakyll.Core.Resource.Provider.File
32+import Hakyll.Core.Routes
2833import Hakyll.Core.Rules.Internal
29−import Hakyll.Core.DirectedGraph
30−import Hakyll.Core.DependencyAnalyzer
31−import Hakyll.Core.Writable
3234import Hakyll.Core.Store
33−import Hakyll.Core.Configuration
34−import Hakyll.Core.Logger
35+import Hakyll.Core.Util.File
36+import Hakyll.Core.Writable
3537
3638-- | Run all rules needed, return the rule set used
3739--
@@ -66,14 +68,18 @@ run configuration rules = do
6668 }
6769
6870 -- Run the program and fetch the resulting state
69− ((), state') <- runStateT stateT $ RuntimeState
71+ result <- runErrorT $ runStateT stateT $ RuntimeState
7072 { hakyllAnalyzer = makeDependencyAnalyzer mempty (const False) oldGraph
7173 , hakyllCompilers = M.empty
7274 }
7375
74− -- We want to save the final dependency graph for the next run
75− storeSet store "Hakyll.Core.Run.run" "dependencies" $
76− analyzerGraph $ hakyllAnalyzer state'
76+ case result of
77+ Left e ->
78+ thrown logger e
79+ Right ((), state') ->
80+ -- We want to save the final dependency graph for the next run
81+ storeSet store "Hakyll.Core.Run.run" "dependencies" $
82+ analyzerGraph $ hakyllAnalyzer state'
7783
7884 -- Flush and return
7985 flushLogger logger
@@ -94,7 +100,8 @@ data RuntimeState = RuntimeState
94100 }
95101
96102newtype Runtime a = Runtime
97− { unRuntime :: ReaderT RuntimeEnvironment (StateT RuntimeState IO) a
103+ { unRuntime :: ReaderT RuntimeEnvironment
104+ (StateT RuntimeState (ErrorT String IO)) a
98105 } deriving (Functor, Applicative, Monad)
99106
100107-- | Add a number of compilers and continue using these compilers
@@ -205,7 +212,5 @@ build id' = Runtime $ do
205212 -- Actually I was just kidding, it's not hard at all
206213 unRuntime $ addNewCompilers newCompilers
207214
208− -- Some error happened, log and continue
209− Left err -> do
210− thrown logger err
211− unRuntime stepAnalyzer
215+ -- Some error happened, rethrow in Runtime monad
216+ Left err -> throwError err