Skip to content

Commit

Permalink
give better error in cases of invalid (or empty) templates
Browse files Browse the repository at this point in the history
  • Loading branch information
kadoban committed Dec 22, 2015
1 parent 189d677 commit cc4552b
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/Stack/New.hs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ loadTemplate name logIt = do

-- | Apply and unpack a template into a directory.
applyTemplate
:: (MonadIO m, MonadThrow m, MonadReader r m, HasConfig r, MonadLogger m)
:: (MonadIO m, MonadThrow m, MonadCatch m, MonadReader r m, HasConfig r, MonadLogger m)
=> PackageName
-> TemplateName
-> Map Text Text
Expand All @@ -189,9 +189,14 @@ applyTemplate project template nonceParams dir templateText = do
unless (S.null missingKeys)
($logInfo (T.pack (show (MissingParameters project template missingKeys (configUserConfigPath config)))))
files :: Map FilePath LB.ByteString <-
execWriterT $
yield (T.encodeUtf8 (LT.toStrict applied)) $$
unpackTemplate receiveMem id
catch (execWriterT $
yield (T.encodeUtf8 (LT.toStrict applied)) $$
unpackTemplate receiveMem id
)
(\(e :: ProjectTemplateException) ->
throwM (InvalidTemplate template (show e)))
when (M.null files) $
throwM (InvalidTemplate template "Template did not specify any files.")
liftM
M.fromList
(mapM
Expand Down Expand Up @@ -319,6 +324,7 @@ data NewException
| BadTemplatesJSON !String !LB.ByteString
| AlreadyExists !(Path Abs Dir)
| MissingParameters !PackageName !TemplateName !(Set String) !(Path Abs File)
| InvalidTemplate !TemplateName !String
deriving (Typeable)

instance Exception NewException
Expand Down Expand Up @@ -373,3 +379,7 @@ instance Show NewException where
(\key ->
"-p \"" <> key <> ":value\"")
(S.toList missingKeys))]
show (InvalidTemplate name why) =
"The template \"" <> T.unpack (templateName name) <>
"\" is invalid and couldn't be used. " <>
"The error was: " <> why

0 comments on commit cc4552b

Please sign in to comment.