@@ -49,30 +49,17 @@ import Text.ProjectTemplate
49
49
--------------------------------------------------------------------------------
50
50
-- Exceptions
51
51
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
-
67
52
-- | Type representing \'pretty\' exceptions thrown by functions exported by the
68
53
-- "Stack.New" module.
69
54
data NewPrettyException
70
55
= ProjectDirAlreadyExists ! String ! (Path Abs Dir )
71
- | FailedToDownloadTemplate ! Text ! String ! VerifiedDownloadException
56
+ | DownloadTemplateFailed ! Text ! String ! VerifiedDownloadException
57
+ | LoadTemplateFailed ! TemplateName ! FilePath
58
+ | ExtractTemplateFailed ! TemplateName ! FilePath ! String
72
59
| TemplateInvalid ! TemplateName ! StyleDoc
73
60
| MagicPackageNameInvalid ! String
74
61
| AttemptedOverwrites ! Text ! [Path Abs File ]
75
- | FailedToDownloadTemplatesHelp ! HttpException
62
+ | DownloadTemplatesHelpFailed ! HttpException
76
63
| TemplatesHelpEncodingInvalid ! String ! UnicodeException
77
64
deriving (Show , Typeable )
78
65
@@ -87,7 +74,7 @@ instance Pretty NewPrettyException where
87
74
, style Dir (pretty path)
88
75
, flow " already exists."
89
76
]
90
- pretty (FailedToDownloadTemplate name url err) =
77
+ pretty (DownloadTemplateFailed name url err) =
91
78
" [S-1688]"
92
79
<> line
93
80
<> fillSep
@@ -134,6 +121,28 @@ instance Pretty NewPrettyException where
134
121
<> blankLine
135
122
<> fromString (displayException err)
136
123
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
137
146
pretty (TemplateInvalid name why) =
138
147
" [S-9490]"
139
148
<> line
@@ -182,7 +191,7 @@ instance Pretty NewPrettyException where
182
191
, style Shell " --force"
183
192
, " flag to ignore this and overwrite those files."
184
193
]
185
- pretty (FailedToDownloadTemplatesHelp err) =
194
+ pretty (DownloadTemplatesHelpFailed err) =
186
195
" [S-8143]"
187
196
<> line
188
197
<> fillSep
@@ -306,7 +315,7 @@ loadTemplate name logIt = do
306
315
(do f <- loadLocalFile relFile eitherByteStringToText
307
316
logIt LocalTemp
308
317
pure f)
309
- (\ (e :: NewException ) -> do
318
+ (\ (e :: PrettyException ) -> do
310
319
case relSettings rawParam of
311
320
Just settings -> do
312
321
let url = tplDownloadUrl settings
@@ -328,12 +337,12 @@ loadTemplate name logIt = do
328
337
then do
329
338
bs <- readFileBinary (toFilePath path) -- readFileUtf8 (toFilePath path)
330
339
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
334
342
Right template ->
335
343
pure template
336
- else throwM (FailedToLoadTemplate name (toFilePath path))
344
+ else throwM $ PrettyException $
345
+ LoadTemplateFailed name (toFilePath path)
337
346
relSettings :: String -> Maybe TemplateDownloadSettings
338
347
relSettings req = do
339
348
rtp <- parseRepoPathWithService defaultRepoService (T. pack req)
@@ -366,7 +375,7 @@ loadTemplate name logIt = do
366
375
\most recent version though."
367
376
)
368
377
else throwM $ PrettyException $
369
- FailedToDownloadTemplate (templateName name) url exception
378
+ DownloadTemplateFailed (templateName name) url exception
370
379
371
380
data TemplateDownloadSettings = TemplateDownloadSettings
372
381
{ tplDownloadUrl :: String
@@ -601,7 +610,7 @@ templatesHelp = do
601
610
req <- liftM setGitHubHeaders (parseUrlThrow url)
602
611
resp <- catch
603
612
(httpLbs req)
604
- (throwM . PrettyException . FailedToDownloadTemplatesHelp )
613
+ (throwM . PrettyException . DownloadTemplatesHelpFailed )
605
614
case decodeUtf8' $ LB. toStrict $ getResponseBody resp of
606
615
Left err -> throwM $ PrettyException $ TemplatesHelpEncodingInvalid url err
607
616
Right txt -> logInfo $ display txt
0 commit comments