Skip to content

Commit ebae0aa

Browse files
committed
Re commercialhaskell#5955 Prettier LoadTemplateFailed and ExtractTemplateFailed
1 parent 6e36efa commit ebae0aa

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

doc/maintainers/stack_errors.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,21 +182,17 @@ to take stock of the errors that Stack itself can raise, by reference to the
182182
[S-3421] = ParseFailure [Value]
183183
~~~
184184

185-
- `Stack.New.NewException`
186-
187-
~~~haskell
188-
[S-3650] = FailedToLoadTemplate TemplateName FilePath
189-
~~~
190-
191185
- `Stack.New.NewPrettyException`
192186

193187
~~~haskell
194188
[S-2135] = ProjectDirAlreadyExists String (Path Abs Dir)
195-
[S-1688] | FailedToDownloadTemplate Text String VerifiedDownloadException
189+
[S-1688] | DownloadTemplateFailed Text String VerifiedDownloadException
190+
[S-3650] | LoadTemplateFailed TemplateName FilePath
191+
[S-9582] | ExtractTemplateFailed TemplateName FilePath String
196192
[S-9490] | TemplateInvalid TemplateName StyleDoc
197193
[S-5682] | MagicPackageNameInvalid String
198194
[S-3113] | AttemptedOverwrites [Path Abs File]
199-
[S-8143] | FailedToDownloadTemplatesHelp HttpException
195+
[S-8143] | DownloadTemplatesHelpFailed HttpException
200196
[S-6670] | TemplatesHelpEncodingInvalid String UnicodeException
201197
~~~
202198

src/Stack/New.hs

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,17 @@ import Text.ProjectTemplate
4949
--------------------------------------------------------------------------------
5050
-- Exceptions
5151

52-
-- | Type representing exceptions thrown by functions exported by the
53-
-- "Stack.New" module.
54-
data NewException
55-
= FailedToLoadTemplate !TemplateName !FilePath
56-
deriving (Show, Typeable)
57-
58-
instance Exception NewException where
59-
displayException (FailedToLoadTemplate name path) = concat
60-
[ "Error: [S-3650]\n"
61-
, "Failed to load download template "
62-
, T.unpack (templateName name)
63-
, " from "
64-
, path
65-
]
66-
6752
-- | Type representing \'pretty\' exceptions thrown by functions exported by the
6853
-- "Stack.New" module.
6954
data NewPrettyException
7055
= ProjectDirAlreadyExists !String !(Path Abs Dir)
71-
| FailedToDownloadTemplate !Text !String !VerifiedDownloadException
56+
| DownloadTemplateFailed !Text !String !VerifiedDownloadException
57+
| LoadTemplateFailed !TemplateName !FilePath
58+
| ExtractTemplateFailed !TemplateName !FilePath !String
7259
| TemplateInvalid !TemplateName !StyleDoc
7360
| MagicPackageNameInvalid !String
7461
| AttemptedOverwrites !Text ![Path Abs File]
75-
| FailedToDownloadTemplatesHelp !HttpException
62+
| DownloadTemplatesHelpFailed !HttpException
7663
| TemplatesHelpEncodingInvalid !String !UnicodeException
7764
deriving (Show, Typeable)
7865

@@ -87,7 +74,7 @@ instance Pretty NewPrettyException where
8774
, style Dir (pretty path)
8875
, flow "already exists."
8976
]
90-
pretty (FailedToDownloadTemplate name url err) =
77+
pretty (DownloadTemplateFailed name url err) =
9178
"[S-1688]"
9279
<> line
9380
<> fillSep
@@ -134,6 +121,28 @@ instance Pretty NewPrettyException where
134121
<> blankLine
135122
<> fromString (displayException err)
136123
in (msg', False)
124+
pretty (LoadTemplateFailed name path) =
125+
"[S-3650]"
126+
<> line
127+
<> fillSep
128+
[ flow "Stack failed to load the downloaded template"
129+
, style Current (fromString $ T.unpack $ templateName name)
130+
, "from"
131+
, style File (fromString path) <> "."
132+
]
133+
pretty (ExtractTemplateFailed name path err) =
134+
"[S-9582]"
135+
<> line
136+
<> fillSep
137+
[ flow "Stack failed to extract the loaded template"
138+
, style Current (fromString $ T.unpack $ templateName name)
139+
, "at"
140+
, style File (fromString path) <> "."
141+
]
142+
<> blankLine
143+
<> flow "While extracting, Stack encountered the following exception:"
144+
<> blankLine
145+
<> string err
137146
pretty (TemplateInvalid name why) =
138147
"[S-9490]"
139148
<> line
@@ -182,7 +191,7 @@ instance Pretty NewPrettyException where
182191
, style Shell "--force"
183192
, "flag to ignore this and overwrite those files."
184193
]
185-
pretty (FailedToDownloadTemplatesHelp err) =
194+
pretty (DownloadTemplatesHelpFailed err) =
186195
"[S-8143]"
187196
<> line
188197
<> fillSep
@@ -306,7 +315,7 @@ loadTemplate name logIt = do
306315
(do f <- loadLocalFile relFile eitherByteStringToText
307316
logIt LocalTemp
308317
pure f)
309-
(\(e :: NewException) -> do
318+
(\(e :: PrettyException) -> do
310319
case relSettings rawParam of
311320
Just settings -> do
312321
let url = tplDownloadUrl settings
@@ -328,12 +337,12 @@ loadTemplate name logIt = do
328337
then do
329338
bs <- readFileBinary (toFilePath path) --readFileUtf8 (toFilePath path)
330339
case extract bs of
331-
Left err -> do
332-
logWarn $ "Template extraction error: " <> display (T.pack err)
333-
throwM (FailedToLoadTemplate name (toFilePath path))
340+
Left err -> throwM $ PrettyException $
341+
ExtractTemplateFailed name (toFilePath path) err
334342
Right template ->
335343
pure template
336-
else throwM (FailedToLoadTemplate name (toFilePath path))
344+
else throwM $ PrettyException $
345+
LoadTemplateFailed name (toFilePath path)
337346
relSettings :: String -> Maybe TemplateDownloadSettings
338347
relSettings req = do
339348
rtp <- parseRepoPathWithService defaultRepoService (T.pack req)
@@ -366,7 +375,7 @@ loadTemplate name logIt = do
366375
\most recent version though."
367376
)
368377
else throwM $ PrettyException $
369-
FailedToDownloadTemplate (templateName name) url exception
378+
DownloadTemplateFailed (templateName name) url exception
370379

371380
data TemplateDownloadSettings = TemplateDownloadSettings
372381
{ tplDownloadUrl :: String
@@ -601,7 +610,7 @@ templatesHelp = do
601610
req <- liftM setGitHubHeaders (parseUrlThrow url)
602611
resp <- catch
603612
(httpLbs req)
604-
(throwM . PrettyException. FailedToDownloadTemplatesHelp)
613+
(throwM . PrettyException. DownloadTemplatesHelpFailed)
605614
case decodeUtf8' $ LB.toStrict $ getResponseBody resp of
606615
Left err -> throwM $ PrettyException $ TemplatesHelpEncodingInvalid url err
607616
Right txt -> logInfo $ display txt

0 commit comments

Comments
 (0)