Discussion:
[hakyll] Failure handling.
p***@gmail.com
2018-03-13 12:18:16 UTC
Permalink
Hi!

I'm having a small issue with error handling in Hakyll.

Consider this piece of code:

titleContext :: String -> Context String
titleContext language = field "title" $
\item -> do
let identifier = itemIdentifier item
metadata <- getMetadata $ identifier
return $ case HM.lookup "title" metadata of
Nothing -> fail $ "Missing \"title\" metadata in " ++ show identifier
Just value -> case value of
Object o -> case HM.lookup (pack language) o of
Nothing -> fail $ "Missing title in " ++ show identifier
Just value' -> case value' of
String s -> unpack s
_ -> fail $ "Invalid " ++ language ++ " title value type in " ++ show identifier
_ -> fail $ "Invalid title value type in " ++ show identifier

And this markdown metadata:
---
title:
Fr: Article en Français
En: English written article
---

If the metadata is well formed, it works as expected: the context contains
the $title$ of the given language.

Now, the issue is: if the metadata is not well formed as in:
---
foo:
Fr: Article en Français
Xx: English written article
---

The code will silently pass through the function and the $title$ will be
missing without showing any hint a a failure.
That is, I won't have neither a title nor any CLI clue of my misfortune.

Do you have any clue on how to solve this unfortunate situation?
Many thanks in advance!
--
You received this message because you are subscribed to the Google Groups "hakyll" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hakyll+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
p***@gmail.com
2018-03-13 12:28:05 UTC
Permalink
And of course, I needed to search 10 more minutes to find out the why:

https://hackage.haskell.org/package/hakyll-4.11.0.0/docs/src/Hakyll-Web-Template-Context.html#field'
Post by p***@gmail.com
Hi!
I'm having a small issue with error handling in Hakyll.
titleContext :: String -> Context String
titleContext language = field "title" $
\item -> do
let identifier = itemIdentifier item
metadata <- getMetadata $ identifier
return $ case HM.lookup "title" metadata of
Nothing -> fail $ "Missing \"title\" metadata in " ++ show identifier
Just value -> case value of
Object o -> case HM.lookup (pack language) o of
Nothing -> fail $ "Missing title in " ++ show identifier
Just value' -> case value' of
String s -> unpack s
_ -> fail $ "Invalid " ++ language ++ " title value type in " ++ show identifier
_ -> fail $ "Invalid title value type in " ++ show identifier
---
Fr: Article en Français
En: English written article
---
If the metadata is well formed, it works as expected: the context contains
the $title$ of the given language.
---
Fr: Article en Français
Xx: English written article
---
The code will silently pass through the function and the $title$ will be
missing without showing any hint a a failure.
That is, I won't have neither a title nor any CLI clue of my misfortune.
Do you have any clue on how to solve this unfortunate situation?
Many thanks in advance!
--
You received this message because you are subscribed to the Google Groups "hakyll" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hakyll+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...