@@ -9,17 +9,18 @@ module Ormolu
99 , defaultConfig
1010 , DynOption (..)
1111 , OrmoluException (..)
12+ , withPrettyOrmoluExceptions
1213 )
1314where
1415
1516import Control.Exception
1617import Control.Monad
17−import Control.Monad.Catch (MonadThrow (..))
1818import Control.Monad.IO.Class (MonadIO (..))
1919import Data.Text (Text)
2020import Language.Haskell.GHC.ExactPrint.Types
2121import Ormolu.Config
2222import Ormolu.Diff
23+import Ormolu.Exception
2324import Ormolu.Parser
2425import Ormolu.Printer
2526import qualified Data.Text as T
@@ -37,7 +38,7 @@ import qualified GHC
3738-- * Throws 'OrmoluException'.
3839
3940ormolu
40− :: (MonadIO m, MonadThrow m)
41+ :: MonadIO m
4142 => Config -- ^ Ormolu configuration
4243 -> FilePath -- ^ Location of source file
4344 -> String -- ^ Input to format
@@ -51,7 +52,7 @@ ormolu cfg path str = do
5152 (anns1, parsedSrc1) <-
5253 parseModule' cfg OrmoluOutputParsingFailed "<rendered>" (T.unpack txt)
5354 when (diff (anns0, parsedSrc0) (anns1, parsedSrc1)) $
54− throwM (OrmoluASTDiffers str txt)
55+ liftIO $ throwIO (OrmoluASTDiffers str txt)
5556 return txt
5657
5758-- | Load a file and format it. The file stays intact and the rendered
@@ -61,7 +62,7 @@ ormolu cfg path str = do
6162-- > liftIO (readFile path) >>= ormolu cfg path
6263
6364ormoluFile
64− :: (MonadIO m, MonadThrow m)
65+ :: MonadIO m
6566 => Config -- ^ Ormolu configuration
6667 -> FilePath -- ^ Location of source file
6768 -> m Text -- ^ Resulting rendition
@@ -71,7 +72,7 @@ ormoluFile cfg path =
7172-- | A wrapper around 'parseModule'.
7273
7374parseModule'
74− :: (MonadIO m, MonadThrow m)
75+ :: MonadIO m
7576 => Config -- ^ Ormolu configuration
7677 -> (GHC.SrcSpan -> String -> OrmoluException)
7778 -- ^ How to obtain 'OrmoluException' to throw when parsing fails
@@ -79,21 +80,7 @@ parseModule'
7980 -> String -- ^ Actual input for the parser
8081 -> m (Anns, GHC.ParsedSource) -- ^ Annotations and parsed source
8182parseModule' Config {..} mkException path str = do
82− (_, r) <- liftIO (parseModule cfgDynOptions path str)
83+ (_, r) <- parseModule cfgDynOptions path str
8384 case r of
84− Left (spn, err) -> throwM (mkException spn err)
85+ Left (spn, err) -> liftIO $ throwIO (mkException spn err)
8586 Right x -> return x
86−
87−-- | Ormolu exception representing all cases when 'ormoluFile' can fail.
88−
89−data OrmoluException
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
97− deriving (Eq, Show)
98−
99−instance Exception OrmoluException