@@ -9,7 +9,6 @@ module Ormolu.Exception
99where
1010
1111import Control.Exception
12−import Data.Text (Text)
1312import Ormolu.Utils (showOutputable)
1413import System.Exit (ExitCode (..), exitWith)
1514import System.IO
@@ -18,29 +17,35 @@ import qualified GHC
1817-- | Ormolu exception representing all cases when Ormolu can fail.
1918
2019data OrmoluException
21− = OrmoluCppEnabled
20+ = OrmoluCppEnabled FilePath
2221 -- ^ Ormolu does not work with source files that use CPP
2322 | OrmoluParsingFailed GHC.SrcSpan String
2423 -- ^ Parsing of original source code failed
2524 | OrmoluOutputParsingFailed GHC.SrcSpan String
2625 -- ^ Parsing of formatted source code failed
27− | OrmoluASTDiffers [GHC.SrcSpan] String Text
28− -- ^ Original and resulting ASTs differ, first argument is the original
29− -- source code, second argument is rendered source code
26+ | OrmoluASTDiffers FilePath [GHC.SrcSpan]
27+ -- ^ Original and resulting ASTs differ
3028 deriving (Eq, Show)
3129
3230instance Exception OrmoluException where
3331 displayException = \case
34− OrmoluCppEnabled -> "CPP is not supported"
32+ OrmoluCppEnabled path -> unlines
33+ [ "CPP is not supported:"
34+ , withIndent path
35+ ]
3536 OrmoluParsingFailed s e ->
3637 showParsingErr "Parsing of source code failed:" s e
3738 OrmoluOutputParsingFailed s e ->
3839 showParsingErr "Parsing of formatted code failed:" s e ++
39− "Please, consider reporting the bug."
40− OrmoluASTDiffers ss _ _ -> unlines $
41− ["AST of input and AST of formatted code differ."]
42− ++ map (\s -> " at " ++ showOutputable s) ss ++
43− ["Please, consider reporting the bug."]
40+ "Please, consider reporting the bug.\n"
41+ OrmoluASTDiffers path ss -> unlines $
42+ [ "AST of input and AST of formatted code differ."
43+ ]
44+ ++ (fmap withIndent $ case fmap (\s -> "at " ++ showOutputable s) ss of
45+ [] -> ["in " ++ path]
46+ xs -> xs) ++
47+ [ "Please, consider reporting the bug."
48+ ]
4449
4550-- | Inside this wrapper 'OrmoluException' will be caught and displayed
4651-- nicely using 'displayException'.
@@ -56,10 +61,10 @@ withPrettyOrmoluExceptions m = m `catch` h
5661 exitWith . ExitFailure $
5762 case e of
5863 -- Error code 1 is for `error` or `notImplemented`
59− OrmoluCppEnabled -> 2
64+ OrmoluCppEnabled _ -> 2
6065 OrmoluParsingFailed _ _ -> 3
6166 OrmoluOutputParsingFailed _ _ -> 4
62− OrmoluASTDiffers _ _ _ -> 5
67+ OrmoluASTDiffers _ _ -> 5
6368
6469----------------------------------------------------------------------------
6570-- Helpers
@@ -69,6 +74,11 @@ withPrettyOrmoluExceptions m = m `catch` h
6974showParsingErr :: String -> GHC.SrcSpan -> String -> String
7075showParsingErr msg spn err = unlines
7176 [ msg
72− , showOutputable spn
73− , err
77+ , withIndent (showOutputable spn)
78+ , withIndent err
7479 ]
80+
81+-- | Indent with 2 spaces for readability.
82+
83+withIndent :: String -> String
84+withIndent txt = " " ++ txt