on master help sign in
Better error messages for type errors
commitda3aa16c8bffe896082288902256b4454e9dc415 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateThu 19 May 2011, 00:41 (15 years ago) parentdf1d77e
2 files changed+17−15
src/Hakyll/Core/Compiler.hs+11−9 src/Hakyll/Core/Store.hs+6−6
src/Hakyll/Core/Compiler.hs+11−9
@@ -217,16 +217,18 @@ getDependency id' = CompilerM $ do
217217 store <- compilerStore <$> ask
218218 result <- liftIO $ storeGet store "Hakyll.Core.Compiler.runCompiler" id'
219219 case result of
220− NotFound -> throwError notFound
221− WrongType -> throwError wrongType
222− Found x -> return x
220+ NotFound -> throwError notFound
221+ WrongType e r -> throwError $ wrongType e r
222+ Found x -> return x
223223 where
224− notFound = "Hakyll.Core.Compiler.getDependency: " ++ show id'
225− ++ " not found in the cache, the cache might be corrupted or"
226− ++ " the item you are referring to might not exist"
227− wrongType = "Hakyll.Core.Compiler.getDependency: " ++ show id'
228− ++ " was found in the cache, but does not have the expected "
229− ++ " type"
224+ notFound =
225+ "Hakyll.Core.Compiler.getDependency: " ++ show id' ++ " not found " ++
226+ "not found in the cache, the cache might be corrupted or " ++
227+ "the item you are referring to might not exist"
228+ wrongType e r =
229+ "Hakyll.Core.Compiler.getDependency: " ++ show id' ++ " was found " ++
230+ "in the cache, but does not have the right type: expected " ++ show e ++
231+ " but got " ++ show r
230232
231233-- | Variant of 'require' which drops the current value
232234--
src/Hakyll/Core/Store.hs+6−6
@@ -1,6 +1,6 @@
11-- | A store for stroing and retreiving items
22--
3−{-# LANGUAGE ExistentialQuantification #-}
3+{-# LANGUAGE ExistentialQuantification, ScopedTypeVariables #-}
44module Hakyll.Core.Store
55 ( Store
66 , StoreGet (..)
@@ -17,7 +17,7 @@ import Data.Map (Map)
1717import qualified Data.Map as M
1818
1919import Data.Binary (Binary, encodeFile, decodeFile)
20−import Data.Typeable (Typeable, cast)
20+import Data.Typeable (Typeable, TypeRep, cast, typeOf)
2121
2222import Hakyll.Core.Identifier
2323import Hakyll.Core.Util.File
@@ -30,8 +30,8 @@ data Storable = forall a. (Binary a, Typeable a) => Storable a
3030--
3131data StoreGet a = Found a
3232 | NotFound
33− | WrongType
34− deriving (Show, Eq, Ord)
33+ | WrongType TypeRep TypeRep
34+ deriving (Show, Eq)
3535
3636-- | Data structure used for the store
3737--
@@ -79,7 +79,7 @@ storeSet store name identifier value = do
7979
8080-- | Load an item
8181--
82−storeGet :: (Binary a, Typeable a)
82+storeGet :: forall a. (Binary a, Typeable a)
8383 => Store -> String -> Identifier -> IO (StoreGet a)
8484storeGet store name identifier = do
8585 -- First check the in-memory map
@@ -87,7 +87,7 @@ storeGet store name identifier = do
8787 case M.lookup path map' of
8888 -- Found in the in-memory map
8989 Just (Storable s) -> return $ case cast s of
90− Nothing -> WrongType
90+ Nothing -> WrongType (typeOf s) $ typeOf (undefined :: a)
9191 Just s' -> Found s'
9292 -- Not found in the map, try the filesystem
9393 Nothing -> do