Skip to content

Commit

Permalink
better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
bergus committed Mar 20, 2018
1 parent 7228a3f commit 744c65f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
8 changes: 4 additions & 4 deletions lib/Hakyll/Web/Template/Context.hs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ mapContext f (Context c) = Context $ \k a i -> do
case fld of
EmptyField -> wrongType "boolField"
StringField str -> return $ StringField (f str)
ListField _ _ -> wrongType "ListField"
_ -> wrongType "ListField"
where
wrongType typ = fail $ "Hakyll.Web.Template.Context.mapContext: " ++
"can't map over a " ++ typ ++ "!"
Expand Down Expand Up @@ -308,10 +308,10 @@ lookupNestedValue [] v = return $ let Just s = toString v in String
lookupNestedValue (k:ks) (Object m) = case H.lookup (T.pack k) m of
Nothing -> failBranch $ "No '"++k++"' property in object" -- ++ debug m
Just v -> lookupNestedValue ks v
lookupNestedValue (k:ks) (Array v) = case readMaybe k :: Maybe Int of
lookupNestedValue (k:ks) (Array a) = case readMaybe k :: Maybe Int of
Nothing -> failBranch $ "No '"++k++"' element in array" -- ++ debug v
Just n -> case v V.!? n of
Nothing -> failBranch $ "No '"++k++"' index in array of size " ++ show (length v) -- ++ debug v
Just n -> case a V.!? n of
Nothing -> failBranch $ "No '"++k++"' index in array of size " ++ show (length a) -- ++ debug a
Just v -> lookupNestedValue ks v
lookupNestedValue (k:_) _ = failBranch $ "no '"++k++"' in primitive value" -- ++ debug p

Expand Down
35 changes: 18 additions & 17 deletions lib/Hakyll/Web/Template/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -114,29 +114,30 @@ applyTemplate :: Template -- ^ Template
-> Item a -- ^ Page
-> Compiler (Item String) -- ^ Resulting item
applyTemplate tpl context item = do
body <- applyTemplate' (unTemplate tpl) (getOrigin tpl) context item
body <- applyTemplate' (unTemplate tpl) context item `catchError` handler
return $ itemSetBody body item
where
tplName = getOrigin tpl
itemName = show $ itemIdentifier item
handler es = fail $ "Hakyll.Web.Template.applyTemplate: Failed to " ++
(if tplName == itemName
then "interpolate template in item " ++ itemName
else "apply template " ++ tplName ++ " to item " ++ itemName) ++
":\n" ++ intercalate ",\n" es



--------------------------------------------------------------------------------
applyTemplate'
:: forall a.
[TemplateElement] -- ^ Unwrapped Template
-> FilePath -- ^ template name
-> Context a -- ^ Context
-> Item a -- ^ Page
-> Compiler String -- ^ Resulting item
applyTemplate' tes name context x = go tes `catchError` handler
applyTemplate' tes context x = go tes
where
context' :: String -> [String] -> Item a -> Compiler ContextField
context' = unContext (context `mappend` missingField)

itemName = show $ itemIdentifier x
handler es = fail $ "Hakyll.Web.Template.applyTemplate: Failed to " ++
(if name == itemName
then "interpolate template in item " ++ name
else "apply template " ++ name ++ " to item " ++ itemName) ++
":\n" ++ intercalate ",\n" es
context' = unContext (context <> missingField)

go = fmap concat . mapM applyElem

Expand Down Expand Up @@ -168,19 +169,19 @@ applyTemplate' tes name context x = go tes `catchError` handler
applyElem (For e b s) = applyExpr e >>= \cf -> case cf of
EmptyField -> expected "ListField" "boolField" e
StringField _ -> expected "ListField" "StringField" e
ListField c xs -> do
ListField c xs -> mapError ("In for loop context":) $ do
sep <- maybe (return "") go s
bs <- mapM (applyTemplate' b name c) xs
bs <- mapM (applyTemplate' b c) xs
return $ intercalate sep bs
LexicalListField mc vs -> do
LexicalListField mc vs -> mapError ("In for loop":) $ do
sep <- maybe (return "") go s
bs <- mapM (\v -> applyTemplate' b name (mc context v) x) vs
bs <- mapM (\v -> applyTemplate' b (mc context v) x) vs
return $ intercalate sep bs

applyElem (Partial e) = do
applyElem (Partial e) = mapError ("In partial":) $ do
p <- applyExpr e >>= getString e
tpl' <- loadBody (fromFilePath p)
applyTemplate' (unTemplate tpl') (getOrigin tpl') context x
itemBody <$> applyTemplate tpl' context x

---------------------------------------------------------------------------

Expand Down

0 comments on commit 744c65f

Please sign in to comment.