module Hakyll.Error ( ErrorConv (..) , throwErr , liftErr , squashErr , CompilerError ) where import Prolog import Text.Parsec.Error class ErrorConv c err where toErr :: c -> err type CompilerError c = ErrorConv c [String] instance ErrorConv err err where toErr = id instance Exception c => ErrorConv c SomeException where toErr = SomeException -- instance Functor f => ErrorConv c err => ErrorConv (f c) (f err) where -- toErr = fmap toErr -- instance (Applicative f, ErrorConv c err) => ErrorConv c (f err) where -- toErr = pure . toErr throwErr :: (MonadError err m, ErrorConv c err) => c -> m a throwErr = throwError . toErr liftErr :: (MonadError err m, ErrorConv c err) => Either c a -> m a liftErr = either throwErr pure squashErr :: (MonadError err m, ErrorConv c err) => m (Either c a) -> m a squashErr = (>>= liftErr) instance ErrorConv Message String where toErr = messageString instance ErrorConv String [String] where toErr = pure instance ErrorConv Message [String] where toErr = pure . messageString instance ErrorConv ParseError [String] where toErr = fmap messageString . errorMessages -- class CompilerError c where -- toCompilerError :: c -> [String] -- default toCompilerError :: Exception c => c -> [String] -- toCompilerError = pure . displayException -- toCompilerErrors :: [c] -> [String] -- toCompilerErrors = (>>= toCompilerError) -- instance CompilerError c => CompilerError [c] where -- toCompilerError = toCompilerErrors -- throwCompilerError :: (MonadError [String] m, CompilerError c) => c -> m a -- throwCompilerError = throwError . toCompilerError -- liftCompilerError :: (MonadError [String] m, CompilerError c) => Either c a -> m a -- liftCompilerError = either throwError pure -- squashCompilerError :: (MonadError [String] m, CompilerError c) => m (Either c a) -> m a -- squashCompilerError = (>>= liftCompilerError) -- instance CompilerError Char where -- toCompilerError = pure . pure -- toCompilerErrors = pure -- instance CompilerError Message where -- toCompilerError = pure . messageString -- toCompilerErrors = fmap messageString -- instance CompilerError ParseError where -- toCompilerError = toCompilerErrors . errorMessages