on master help sign in
Improve error pretty printing
commit62cbd42ddde3b596b62013779fb10f10748606ce authormrkkrp <mark.karpov@tweag.io> dateSun 14 Jul 2019, 12:54 (7 years ago) parent78e8dd8
4 files changed+31−19
src/Ormolu.hs+1−1 src/Ormolu/Diff.hs+4−2 src/Ormolu/Exception.hs+25−15 src/Ormolu/Parser.hs+1−1
src/Ormolu.hs+1−1
@@ -67,7 +67,7 @@ ormolu cfg path str = do
6767 (T.unpack txt)
6868 case diff result0 result1 of
6969 Same -> return ()
70− Different ss -> liftIO $ throwIO (OrmoluASTDiffers ss str txt)
70+ Different ss -> liftIO $ throwIO (OrmoluASTDiffers path ss)
7171 return txt
7272
7373-- | Load a file and format it. The file stays intact and the rendered
src/Ormolu/Diff.hs+4−2
@@ -84,6 +84,8 @@ matchIgnoringSrcSpans = genericQuery
8484 maybe id appendSpan (cast mspn) (genericQuery x y)
8585
8686 appendSpan :: SrcSpan -> Diff -> Diff
87− appendSpan s (Different ss) | fresh = Different (s:ss)
88− where fresh = not $ any (flip isSubspanOf s) ss
87+ appendSpan s (Different ss) | fresh && helpful = Different (s:ss)
88+ where
89+ fresh = not $ any (flip isSubspanOf s) ss
90+ helpful = isGoodSrcSpan s
8991 appendSpan _ d = d
src/Ormolu/Exception.hs+25−15
@@ -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
src/Ormolu/Parser.hs+1−1
@@ -54,7 +54,7 @@ parseModule dynOpts path input' = liftIO $ do
5454 -- otherwise the exception will be wrapped as a GHC panic, which we don't
5555 -- want.
5656 when (GHC.xopt Cpp dynFlags) $
57− throwIO OrmoluCppEnabled
57+ throwIO (OrmoluCppEnabled path)
5858 let r = case runParser GHC.parseModule dynFlags path input of
5959 GHC.PFailed _ ss m ->
6060 Left (ss, GHC.showSDoc dynFlags m)