@@ -17,7 +17,7 @@ module Stack.Clean
1717 , clean
1818 ) where
1919
20- import Data.List ( (\\) , intercalate )
20+ import Data.List ( (\\) )
2121import qualified Data.Map.Strict as Map
2222import Path.IO ( ignoringAbsence , removeDirRecur )
2323import Stack.Config ( withBuildConfig )
@@ -30,27 +30,38 @@ import Stack.Types.Config ( Config )
3030import Stack.Types.Runner ( Runner )
3131import 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.
5667data 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
8394cleanDir :: Path Abs Dir -> RIO Config (Maybe (Path Abs Dir , SomeException ))
8495cleanDir 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