Skip to content

Commit 03c5d9d

Browse files
committed
Prettier error S-4926
1 parent 3d15178 commit 03c5d9d

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

doc/list_command.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ Could not find package paltry, updating
3939
...
4040
Package index cache populated
4141
Error: [S-4926]
42-
- Could not find package paltry on Hackage. Perhaps you meant: pantry, pretty, pasty, xattr, alloy, para, pappy, alure, polar, factory
42+
* Could not find package paltry on Hackage. Perhaps you meant one of:
43+
pantry, pretty, pasty, xattr, alloy, para, pappy, alure, polar and
44+
factory.
4345
4446
stack --resolver lts-21.12 list base unix Win32 acme-missiles pantry
4547
Error: [S-4926]
46-
- Package does not appear in snapshot: base
47-
- Package does not appear in snapshot: unix
48-
- Package does not appear in snapshot: Win32
49-
- Package does not appear in snapshot: acme-missiles
48+
* Package does not appear in snapshot: base.
49+
* Package does not appear in snapshot: unix.
50+
* Package does not appear in snapshot: Win32.
51+
* Package does not appear in snapshot: acme-missiles.
5052
5153
stack --resolver lts-21.12 list pantry
5254
pantry-0.8.3

doc/maintainers/stack_errors.md

Lines changed: 3 additions & 3 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-09-02.
8+
`master` branch of the Stack repository. Last updated: 2023-09-16.
99

1010
* `GHC.GHC.Utils.GhcPkg.Main.Compat`
1111

@@ -183,10 +183,10 @@ to take stock of the errors that Stack itself can raise, by reference to the
183183
[S-2422] | ResolverPartial RawSnapshotLocation String
184184
~~~
185185

186-
- `Stack.List.ListException`
186+
- `Stack.List.ListPrettyException`
187187

188188
~~~haskell
189-
[S-4926] = CouldNotParsePackageSelectors [String]
189+
[S-4926] = CouldNotParsePackageSelectors [StyleDoc]
190190
~~~
191191

192192
- `Stack.Lock.LockPrettyException`

src/Stack/List.hs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ module Stack.List
88
) where
99

1010
import Pantry ( loadSnapshot )
11-
import RIO.List ( intercalate )
1211
import qualified RIO.Map as Map
1312
import RIO.Process ( HasProcessContext )
1413
import Stack.Config ( makeConcreteResolver )
@@ -19,14 +18,17 @@ import Stack.Types.Runner ( Runner, globalOptsL )
1918

2019
-- | Type representing exceptions thrown by functions exported by the
2120
-- "Stack.List" module.
22-
newtype ListException
23-
= CouldNotParsePackageSelectors [String]
21+
newtype ListPrettyException
22+
= CouldNotParsePackageSelectors [StyleDoc]
2423
deriving (Show, Typeable)
2524

26-
instance Exception ListException where
27-
displayException (CouldNotParsePackageSelectors strs) = unlines $
28-
"Error: [S-4926]"
29-
: map ("- " ++) strs
25+
instance Pretty ListPrettyException where
26+
pretty (CouldNotParsePackageSelectors errs) =
27+
"[S-4926]"
28+
<> line
29+
<> bulletedList errs
30+
31+
instance Exception ListPrettyException
3032

3133
-- | Function underlying the @stack list@ command. List packages.
3234
listCmd :: [String] -> RIO Runner ()
@@ -53,13 +55,13 @@ listPackages mSnapshot input = do
5355
(errs2, locs) <- partitionEithers <$> traverse toLoc names
5456
case errs1 ++ errs2 of
5557
[] -> pure ()
56-
errs -> throwM $ CouldNotParsePackageSelectors errs
58+
errs -> prettyThrowM $ CouldNotParsePackageSelectors errs
5759
mapM_ (prettyInfo . fromString . packageIdentifierString) locs
5860
where
5961
toLoc | Just snapshot <- mSnapshot = toLocSnapshot snapshot
6062
| otherwise = toLocNoSnapshot
6163

62-
toLocNoSnapshot :: PackageName -> RIO env (Either String PackageIdentifier)
64+
toLocNoSnapshot :: PackageName -> RIO env (Either StyleDoc PackageIdentifier)
6365
toLocNoSnapshot name = do
6466
mloc1 <-
6567
getLatestHackageLocation YesRequireHackageIndex name UsePreferredVersions
@@ -82,31 +84,38 @@ listPackages mSnapshot input = do
8284
case mloc of
8385
Nothing -> do
8486
candidates <- getHackageTypoCorrections name
85-
pure $ Left $ concat
86-
[ "Could not find package "
87-
, packageNameString name
88-
, " on Hackage"
87+
pure $ Left $ fillSep
88+
[ flow "Could not find package"
89+
, style Current (fromString $ packageNameString name)
90+
, flow "on Hackage."
8991
, if null candidates
90-
then ""
91-
else ". Perhaps you meant: " ++
92-
intercalate ", " (map packageNameString candidates)
92+
then mempty
93+
else fillSep $
94+
flow "Perhaps you meant one of:"
95+
: mkNarrativeList (Just Good) False
96+
(map (fromString . packageNameString) candidates :: [StyleDoc])
9397
]
9498
Just loc -> pure $ Right (packageLocationIdent loc)
9599

96100
toLocSnapshot ::
97101
RawSnapshot
98102
-> PackageName
99-
-> RIO env (Either String PackageIdentifier)
103+
-> RIO env (Either StyleDoc PackageIdentifier)
100104
toLocSnapshot snapshot name =
101105
case Map.lookup name (rsPackages snapshot) of
102106
Nothing ->
103-
pure $ Left $
104-
"Package does not appear in snapshot: " ++ packageNameString name
107+
pure $ Left $ fillSep
108+
[ flow "Package does not appear in snapshot:"
109+
, style Current (fromString $ packageNameString name) <> "."
110+
]
105111
Just sp -> do
106112
loc <- cplComplete <$> completePackageLocation (rspLocation sp)
107113
pure $ Right (packageLocationIdent loc)
108114

109115
parse s =
110116
case parsePackageName s of
111117
Just x -> Right x
112-
Nothing -> Left $ "Could not parse as package name or identifier: " ++ s
118+
Nothing -> Left $ fillSep
119+
[ flow "Could not parse as package name or identifier:"
120+
, style Current (fromString s) <> "."
121+
]

0 commit comments

Comments
 (0)