@@ -9,17 +9,18 @@ module Ormolu
9 , defaultConfig
9 , defaultConfig
10 , DynOption (..)
10 , DynOption (..)
11 , OrmoluException (..)
11 , OrmoluException (..)
12+ , withPrettyOrmoluExceptions
15import Control.Exception
16import Control.Exception
16import Control.Monad
17import Control.Monad
17−import Control.Monad.Catch (MonadThrow (..))
18import Control.Monad.IO.Class (MonadIO (..))
18import Control.Monad.IO.Class (MonadIO (..))
19import Data.Text (Text)
19import Data.Text (Text)
20import Language.Haskell.GHC.ExactPrint.Types
20import Language.Haskell.GHC.ExactPrint.Types
21import Ormolu.Config
21import Ormolu.Config
22import Ormolu.Diff
22import Ormolu.Diff
23+import Ormolu.Exception
23import Ormolu.Parser
24import Ormolu.Parser
24import Ormolu.Printer
25import Ormolu.Printer
25import qualified Data.Text as T
26import qualified Data.Text as T
@@ -37,7 +38,7 @@ import qualified GHC
37-- * Throws 'OrmoluException'.
38-- * Throws 'OrmoluException'.
40− :: (MonadIO m, MonadThrow m)
41+ :: MonadIO m
41 => Config -- ^ Ormolu configuration
42 => Config -- ^ Ormolu configuration
42 -> FilePath -- ^ Location of source file
43 -> FilePath -- ^ Location of source file
43 -> String -- ^ Input to format
44 -> String -- ^ Input to format
@@ -51,7 +52,7 @@ ormolu cfg path str = do
51 (anns1, parsedSrc1) <-
52 (anns1, parsedSrc1) <-
52 parseModule' cfg OrmoluOutputParsingFailed "<rendered>" (T.unpack txt)
53 parseModule' cfg OrmoluOutputParsingFailed "<rendered>" (T.unpack txt)
53 when (diff (anns0, parsedSrc0) (anns1, parsedSrc1)) $
54 when (diff (anns0, parsedSrc0) (anns1, parsedSrc1)) $
54− throwM (OrmoluASTDiffers str txt)
55+ liftIO $ throwIO (OrmoluASTDiffers str txt)
55 return txt
56 return txt
57-- | Load a file and format it. The file stays intact and the rendered
58-- | Load a file and format it. The file stays intact and the rendered
@@ -61,7 +62,7 @@ ormolu cfg path str = do
61-- > liftIO (readFile path) >>= ormolu cfg path
62-- > liftIO (readFile path) >>= ormolu cfg path
64− :: (MonadIO m, MonadThrow m)
65+ :: MonadIO m
65 => Config -- ^ Ormolu configuration
66 => Config -- ^ Ormolu configuration
66 -> FilePath -- ^ Location of source file
67 -> FilePath -- ^ Location of source file
67 -> m Text -- ^ Resulting rendition
68 -> m Text -- ^ Resulting rendition
@@ -71,7 +72,7 @@ ormoluFile cfg path =
71-- | A wrapper around 'parseModule'.
72-- | A wrapper around 'parseModule'.
73parseModule'
74parseModule'
74− :: (MonadIO m, MonadThrow m)
75+ :: MonadIO m
75 => Config -- ^ Ormolu configuration
76 => Config -- ^ Ormolu configuration
76 -> (GHC.SrcSpan -> String -> OrmoluException)
77 -> (GHC.SrcSpan -> String -> OrmoluException)
77 -- ^ How to obtain 'OrmoluException' to throw when parsing fails
78 -- ^ How to obtain 'OrmoluException' to throw when parsing fails
@@ -79,21 +80,7 @@ parseModule'
79 -> String -- ^ Actual input for the parser
80 -> String -- ^ Actual input for the parser
80 -> m (Anns, GHC.ParsedSource) -- ^ Annotations and parsed source
81 -> m (Anns, GHC.ParsedSource) -- ^ Annotations and parsed source
81parseModule' Config {..} mkException path str = do
82parseModule' Config {..} mkException path str = do
82− (_, r) <- liftIO (parseModule cfgDynOptions path str)
83+ (_, r) <- parseModule cfgDynOptions path str
84− Left (spn, err) -> throwM (mkException spn err)
85+ Left (spn, err) -> liftIO $ throwIO (mkException spn err)
85 Right x -> return x
86 Right x -> return x
87−-- | Ormolu exception representing all cases when 'ormoluFile' can fail.
90− = OrmoluParsingFailed GHC.SrcSpan String
91− -- ^ Parsing of original source code failed
92− | OrmoluOutputParsingFailed GHC.SrcSpan String
93− -- ^ Parsing of formatted source code failed
94− | OrmoluASTDiffers String Text
95− -- ^ Original and resulting ASTs differ, first argument is the original
96− -- source code, second argument is rendered source code
99−instance Exception OrmoluException