Skip to content

Commit c90327c

Browse files
authored
Merge pull request #6113 from commercialhaskell/pretty-upgrade
Prettier Stack.Upgrade errors
2 parents f9158b0 + 3822ef3 commit c90327c

File tree

2 files changed

+42
-48
lines changed

2 files changed

+42
-48
lines changed

doc/maintainers/stack_errors.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
In connection with considering Stack's support of the
66
[Haskell Error Index](https://errors.haskell.org/) initiative, this page seeks
77
to take stock of the errors that Stack itself can raise, by reference to the
8-
`master` branch of the Stack repository. Last updated: 2023-04-29.
8+
`master` branch of the Stack repository. Last updated: 2023-04-30.
99

1010
* `Main.main`: catches exceptions from action `commandLineHandler`.
1111

@@ -469,22 +469,17 @@ to take stock of the errors that Stack itself can raise, by reference to the
469469
[S-2628] | CouldNotParsePackageSelectors [String]
470470
~~~
471471

472-
- `Stack.Upgrade.UpgradeException`
472+
- `Stack.Upgrade.UpgradePrettyException`
473473

474474
~~~haskell
475-
[S-3642] = NeitherBinaryOrSourceSpecified
475+
[S-8761] = ResolverOptionInvalid
476+
[S-3642] | NeitherBinaryOrSourceSpecified
476477
[S-8716] | ExecutableFailure
477478
[S-7114] | CommitsNotFound String String
478479
[S-9668] | StackInPackageIndexNotFound
479480
[S-6648] | VersionWithNoRevision
480481
~~~
481482

482-
- `Stack.Upgrade.UpgradePrettyException`
483-
484-
~~~haskell
485-
[S-8761] = ResolverOptionInvalid
486-
~~~
487-
488483
- `Stack.Upload.UploadPrettyException`
489484

490485
~~~haskell

src/Stack/Upgrade.hs

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,43 +37,17 @@ import Stack.Types.StackYamlLoc ( StackYamlLoc (..) )
3737
import System.Console.ANSI ( hSupportsANSIWithoutEmulation )
3838
import System.Process ( rawSystem, readProcess )
3939

40-
-- | Type representing exceptions thrown by functions exported by the
40+
-- | Type representing \'pretty\' exceptions thrown by functions in the
4141
-- "Stack.Upgrade" module.
42-
data UpgradeException
43-
= NeitherBinaryOrSourceSpecified
42+
data UpgradePrettyException
43+
= ResolverOptionInvalid
44+
| NeitherBinaryOrSourceSpecified
4445
| ExecutableFailure
4546
| CommitsNotFound String String
4647
| StackInPackageIndexNotFound
4748
| VersionWithNoRevision
4849
deriving (Show, Typeable)
4950

50-
instance Exception UpgradeException where
51-
displayException NeitherBinaryOrSourceSpecified =
52-
"Error: [S-3642]\n"
53-
++ "You must allow either binary or source upgrade paths."
54-
displayException ExecutableFailure =
55-
"Error: [S-8716]\n"
56-
++ "Non-success exit code from running newly downloaded executable."
57-
displayException (CommitsNotFound branch repo) = concat
58-
[ "Error: [S-7114]\n"
59-
, "No commits found for branch "
60-
, branch
61-
, " on repo "
62-
, repo
63-
]
64-
displayException StackInPackageIndexNotFound =
65-
"Error: [S-9668]\n"
66-
++ "No Stack version found in package indices."
67-
displayException VersionWithNoRevision =
68-
"Error: [S-6648]\n"
69-
++ "Latest version with no revision."
70-
71-
-- | Type representing \'pretty\' exceptions thrown by functions in the
72-
-- "Stack.Upgrade" module.
73-
data UpgradePrettyException
74-
= ResolverOptionInvalid
75-
deriving (Show, Typeable)
76-
7751
instance Pretty UpgradePrettyException where
7852
pretty ResolverOptionInvalid =
7953
"[S-8761]"
@@ -85,6 +59,31 @@ instance Pretty UpgradePrettyException where
8559
, style Shell "upgrade"
8660
, "command."
8761
]
62+
pretty NeitherBinaryOrSourceSpecified =
63+
"[S-3642]"
64+
<> line
65+
<> flow "You must allow either binary or source upgrade paths."
66+
pretty ExecutableFailure =
67+
"[S-8716]"
68+
<> line
69+
<> flow "Non-success exit code from running newly downloaded executable."
70+
pretty (CommitsNotFound branch repo) =
71+
"[S-7114]"
72+
<> line
73+
<> fillSep
74+
[ flow "No commits found for branch"
75+
, style Current (fromString branch)
76+
, flow "on repo"
77+
, style Url (fromString repo) <> "."
78+
]
79+
pretty StackInPackageIndexNotFound =
80+
"[S-9668]"
81+
<> line
82+
<> flow "No Stack version found in package indices."
83+
pretty VersionWithNoRevision =
84+
"[S-6648]"
85+
<> line
86+
<> flow "Latest version with no revision."
8887

8988
instance Exception UpgradePrettyException
9089

@@ -130,7 +129,7 @@ upgrade builtHash (UpgradeOpts mbo mso) = case (mbo, mso) of
130129
-- FIXME It would be far nicer to capture this case in the options parser
131130
-- itself so we get better error messages, but I can't think of a way to
132131
-- make it happen.
133-
(Nothing, Nothing) -> throwIO NeitherBinaryOrSourceSpecified
132+
(Nothing, Nothing) -> prettyThrowIO NeitherBinaryOrSourceSpecified
134133
(Just bo, Nothing) -> binary bo
135134
(Nothing, Just so) -> source so
136135
-- See #2977 - if --git or --git-repo is specified, do source upgrade.
@@ -199,7 +198,7 @@ binaryUpgrade (BinaryOpts mplatform force' mver morg mrepo) =
199198
\tmpFile -> do
200199
-- Sanity check!
201200
ec <- rawSystem (toFilePath tmpFile) ["--version"]
202-
unless (ec == ExitSuccess) (throwIO ExecutableFailure)
201+
unless (ec == ExitSuccess) (prettyThrowIO ExecutableFailure)
203202

204203
sourceUpgrade ::
205204
Maybe String
@@ -215,7 +214,7 @@ sourceUpgrade builtHash (SourceOpts gitRepo) =
215214
[]
216215
latestCommit <-
217216
case words remote of
218-
[] -> throwIO $ CommitsNotFound branch repo
217+
[] -> prettyThrowIO $ CommitsNotFound branch repo
219218
x:_ -> pure x
220219
when (isNothing builtHash) $
221220
prettyWarnS
@@ -225,10 +224,10 @@ sourceUpgrade builtHash (SourceOpts gitRepo) =
225224
\the contrary."
226225
if builtHash == Just latestCommit
227226
then do
228-
prettyInfoS "Already up-to-date, no upgrade required"
227+
prettyInfoS "Already up-to-date, no upgrade required."
229228
pure Nothing
230229
else do
231-
prettyInfoS "Cloning stack"
230+
prettyInfoS "Cloning stack."
232231
-- NOTE: "--recursive" was added after v1.0.0 (and before the next
233232
-- release). This means that we can't use submodules in the Stack
234233
-- repo until we're comfortable with "stack upgrade --git" not
@@ -258,18 +257,18 @@ sourceUpgrade builtHash (SourceOpts gitRepo) =
258257
Nothing -> withConfig NoReexec $ do
259258
void
260259
$ updateHackageIndex
261-
$ Just "Updating index to make sure we find the latest Stack version"
260+
$ Just "Updating index to make sure we find the latest Stack version."
262261
mversion <- getLatestHackageVersion
263262
YesRequireHackageIndex
264263
"stack"
265264
UsePreferredVersions
266265
(PackageIdentifierRevision _ version _) <-
267266
case mversion of
268-
Nothing -> throwIO StackInPackageIndexNotFound
267+
Nothing -> prettyThrowIO StackInPackageIndexNotFound
269268
Just version -> pure version
270269
if version <= stackVersion
271270
then do
272-
prettyInfoS "Already at latest version, no upgrade required"
271+
prettyInfoS "Already at latest version, no upgrade required."
273272
pure Nothing
274273
else do
275274
suffix <- parseRelDir $ "stack-" ++ versionString version
@@ -279,7 +278,7 @@ sourceUpgrade builtHash (SourceOpts gitRepo) =
279278
"stack"
280279
version
281280
case mrev of
282-
Nothing -> throwIO VersionWithNoRevision
281+
Nothing -> prettyThrowIO VersionWithNoRevision
283282
Just (_rev, cfKey, treeKey) -> do
284283
let ident = PackageIdentifier "stack" version
285284
unpackPackageLocation dir $ PLIHackage ident cfKey treeKey

0 commit comments

Comments
 (0)