Skip to content

Commit

Permalink
Make TH parse errors nicer
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkkrp committed Mar 5, 2017
1 parent b3843db commit 910bca1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## Stache 0.2.1

* Made TH parse errors nicer.

## Stache 0.2.0

* Breaking change: the `renderMustache` function will throw an exception
Expand Down
14 changes: 10 additions & 4 deletions Text/Mustache/Compile/TH.hs
Expand Up @@ -26,13 +26,13 @@ module Text.Mustache.Compile.TH
, mustache )
where

import Control.Exception (Exception(..))
import Control.Monad.Catch (try)
import Data.Text.Lazy (Text)
import Data.Typeable (cast)
import Language.Haskell.TH hiding (Dec)
import Language.Haskell.TH.Quote (QuasiQuoter (..))
import Language.Haskell.TH.Syntax (lift)
import Text.Megaparsec hiding (try)
import Text.Mustache.Type
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
Expand Down Expand Up @@ -83,7 +83,8 @@ compileMustacheText
-> Text -- ^ The template to compile
-> Q Exp
compileMustacheText pname text =
handleEither (C.compileMustacheText pname text)
(handleEither . either (Left . MustacheParserException) Right)
(C.compileMustacheText pname text)

-- | Compile Mustache using QuasiQuoter. Usage:
--
Expand All @@ -109,10 +110,15 @@ mustache = QuasiQuoter
-- | Given an 'Either' result return 'Right' and signal pretty-printed error
-- if we have a 'Left'.

handleEither :: Either (ParseError Char Dec) Template -> Q Exp
handleEither :: Either MustacheException Template -> Q Exp
handleEither val =
case val of
Left err -> fail (parseErrorPretty err)
Left err -> fail $
#if MIN_VERSION_base(4,8,0)
displayException err
#else
show err
#endif
Right template -> dataToExpQ (fmap liftText . cast) template

-- | Lift strict 'T.Text' to 'Q' 'Exp'.
Expand Down

0 comments on commit 910bca1

Please sign in to comment.