on master help sign in
Better errors for redirects
commit83a95b3eb41745d3a6f7cb6fcb7ba6c1e1657a84 authorJasper Van der Jeugt <m@jaspervdj.be> dateWed 5 Jul 2017, 10:21 (9 years ago) parent3fcd4b7
1 file changed+11−4
lib/Hakyll/Web/Redirect.hs+11−4
lib/Hakyll/Web/Redirect.hs+11−4
@@ -9,7 +9,7 @@ module Hakyll.Web.Redirect
99import Control.Applicative ((<$>))
1010import Control.Monad (forM_, when)
1111import Data.Binary (Binary (..))
12−import Data.List (nub)
12+import Data.List (sort, group)
1313import Hakyll.Core.Compiler
1414import Hakyll.Core.Identifier
1515import Hakyll.Core.Routes
@@ -57,9 +57,16 @@ import Hakyll.Core.Writable (Writable (..))
5757createRedirects :: [(Identifier, String)] -> Rules ()
5858createRedirects redirects =
5959 do -- redirects are many-to-fewer; keys must be unique, and must point somewhere else:
60− let keys = map fst redirects
61− when (length keys /= length (nub keys)) $ error "Overlapping 301 redirects; check your mapping for redundant keys"
62− when (or $ map (\(r,t) -> toFilePath r == t) redirects) $ error "Self-redirect detected: at least one redirect's target points to itself."
60+ let gkeys = group $ sort $ map fst redirects
61+ forM_ gkeys $ \gkey -> case gkey of
62+ (k : _ : _) -> fail $
63+ "Duplicate 301 redirects; " ++ show k ++ " is ambiguous."
64+ _ -> return ()
65+
66+ forM_ redirects $ \(r, t) ->
67+ when (toFilePath r == t) $ fail $
68+ "Self-redirect detected: " ++ show r ++ " points to itself."
69+
6370 forM_ redirects $ \(ident, to) ->
6471 create [ident] $ do
6572 route idRoute