Skip to content

Commit 9549918

Browse files
authored
Merge pull request #6791 from commercialhaskell/pretty-clean
Prettier `stack clean` exceptions
2 parents 1b1dba9 + 48b5e24 commit 9549918

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

doc/maintainers/stack_errors.md

Lines changed: 2 additions & 2 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: 2025-08-06.
8+
`master` branch of the Stack repository. Last updated: 2025-08-16.
99

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

@@ -65,7 +65,7 @@ to take stock of the errors that Stack itself can raise, by reference to the
6565
[S-4639] = NoArgumentsBug
6666
~~~
6767

68-
- `Stack.Clean.CleanException`
68+
- `Stack.Clean.CleanPrettyException`
6969

7070
~~~haskell
7171
[S-9463] = NonLocalPackages [PackageName]

src/Stack/Clean.hs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module Stack.Clean
1717
, clean
1818
) where
1919

20-
import Data.List ( (\\), intercalate )
20+
import Data.List ( (\\) )
2121
import qualified Data.Map.Strict as Map
2222
import Path.IO ( ignoringAbsence, removeDirRecur )
2323
import Stack.Config ( withBuildConfig )
@@ -30,27 +30,38 @@ import Stack.Types.Config ( Config )
3030
import Stack.Types.Runner ( Runner )
3131
import Stack.Types.SourceMap ( SMWanted (..), ppRoot )
3232

33-
-- | Type representing exceptions thrown by functions exported by the
33+
-- | Type representing \'pretty\' exceptions thrown by functions exported by the
3434
-- "Stack.Clean" module.
35-
data CleanException
35+
data CleanPrettyException
3636
= NonLocalPackages [PackageName]
3737
| DeletionFailures [(Path Abs Dir, SomeException)]
3838
deriving (Show, Typeable)
3939

40-
instance Exception CleanException where
41-
displayException (NonLocalPackages pkgs) = concat
42-
[ "Error: [S-9463]\n"
43-
, "The following packages are not part of this project: "
44-
, intercalate ", " (map show pkgs)
45-
]
46-
displayException (DeletionFailures failures) = concat
47-
[ "Error: [S-6321]\n"
48-
, "Exception while recursively deleting:\n"
49-
, concatMap (\(dir, e) ->
50-
toFilePath dir <> "\n" <> displayException e <> "\n") failures
51-
, "Perhaps you do not have permission to delete these files or they are in \
52-
\use?"
53-
]
40+
instance Pretty CleanPrettyException where
41+
pretty (NonLocalPackages pkgs) =
42+
"[S-9463]"
43+
<> line
44+
<> fillSep
45+
( flow "The following are not project packages:"
46+
: mkNarrativeList (Just Current) False
47+
(map fromPackageName pkgs :: [StyleDoc])
48+
)
49+
pretty (DeletionFailures failures) =
50+
"[S-6321]"
51+
<> line
52+
<> flow "Exception while recursively deleting:"
53+
<> line
54+
<> mconcat (map prettyFailure failures)
55+
<> flow "Perhaps you do not have permission to delete these files or they \
56+
\are in use?"
57+
where
58+
prettyFailure (dir, e) =
59+
pretty dir
60+
<> line
61+
<> string (displayException e)
62+
<> line
63+
64+
instance Exception CleanPrettyException
5465

5566
-- | Type representing command line options for the @stack clean@ command.
5667
data CleanOpts
@@ -78,7 +89,7 @@ clean cleanOpts = do
7889
failures <- catMaybes <$> mapM cleanDir toDelete
7990
case failures of
8091
[] -> pure ()
81-
_ -> throwIO $ DeletionFailures failures
92+
_ -> prettyThrowIO $ DeletionFailures failures
8293

8394
cleanDir :: Path Abs Dir -> RIO Config (Maybe (Path Abs Dir, SomeException))
8495
cleanDir dir = do
@@ -98,7 +109,7 @@ dirsToDelete cleanOpts = do
98109
getPkgDir pkgName' = fmap ppRoot (Map.lookup pkgName' packages)
99110
case targets \\ localPkgNames of
100111
[] -> mapM rootDistDirFromDir (mapMaybe getPkgDir targets)
101-
xs -> throwM (NonLocalPackages xs)
112+
xs -> prettyThrowM (NonLocalPackages xs)
102113
CleanFull -> do
103114
pkgWorkDirs <- mapM (workDirFromDir . ppRoot) $ Map.elems packages
104115
projectWorkDir <- getWorkDir

0 commit comments

Comments
 (0)