88module Cardano.Api.Error
99 ( Error (.. )
1010 , throwErrorAsException
11+ , liftEitherError
1112 , failEitherError
1213 , ErrorAsException (.. )
1314 , FileError (.. )
2021import Cardano.Api.Monad.Error
2122import Cardano.Api.Pretty
2223
23- import Control.Exception (Exception (.. ), IOException , throwIO )
24+ import Control.Exception.Safe
25+ import GHC.Stack
2426import System.Directory (doesFileExist )
2527import System.IO (Handle )
2628
@@ -32,26 +34,46 @@ instance Error () where
3234
3335-- | The preferred approach is to use 'Except' or 'ExceptT', but you can if
3436-- necessary use IO exceptions.
35- throwErrorAsException :: Error e => e -> IO a
36- throwErrorAsException e = throwIO (ErrorAsException e)
37-
38- failEitherError :: MonadFail m => Error e => Either e a -> m a
37+ throwErrorAsException
38+ :: HasCallStack
39+ => MonadThrow m
40+ => Typeable e
41+ => Error e
42+ => e
43+ -> m a
44+ throwErrorAsException e = withFrozenCallStack $ throwM $ ErrorAsException e
45+
46+ failEitherError
47+ :: MonadFail m
48+ => Error e
49+ => Either e a
50+ -> m a
3951failEitherError = failEitherWith displayError
4052
53+ liftEitherError
54+ :: HasCallStack
55+ => MonadThrow m
56+ => Typeable e
57+ => Error e
58+ => Either e a
59+ -> m a
60+ liftEitherError = withFrozenCallStack $ either throwErrorAsException pure
61+
4162data ErrorAsException where
42- ErrorAsException :: Error e => e -> ErrorAsException
63+ ErrorAsException :: ( HasCallStack , Typeable e , Error e ) => e -> ErrorAsException
4364
65+ instance Exception ErrorAsException
66+
67+ -- | Pretty print the error inside the exception
4468instance Error ErrorAsException where
4569 prettyError (ErrorAsException e) =
4670 prettyError e
4771
72+ -- | Pretty print the error inside the exception followed by the call stack pointing to the place where 'Error e' was
73+ -- wrapped in 'ErrorAsException'
4874instance Show ErrorAsException where
4975 show (ErrorAsException e) =
50- docToString $ prettyError e
51-
52- instance Exception ErrorAsException where
53- displayException (ErrorAsException e) =
54- docToString $ prettyError e
76+ docToString (prettyError e) <> " \n " <> prettyCallStack callStack
5577
5678displayError :: Error a => a -> String
5779displayError = docToString . prettyError
0 commit comments