Commit e7b5aff0 authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Introduce panicTrace and errorTrace

parent fec7427b
...@@ -36,6 +36,7 @@ library ...@@ -36,6 +36,7 @@ library
Gargantext.Prelude.Crypto.Share Gargantext.Prelude.Crypto.Share
Gargantext.Prelude.Crypto.Symmetric Gargantext.Prelude.Crypto.Symmetric
Gargantext.Prelude.Database Gargantext.Prelude.Database
Gargantext.Prelude.Error
Gargantext.Prelude.Fibonacci Gargantext.Prelude.Fibonacci
Gargantext.Prelude.Mail Gargantext.Prelude.Mail
Gargantext.Prelude.Mail.Types Gargantext.Prelude.Mail.Types
......
...@@ -18,6 +18,7 @@ module Gargantext.Prelude ...@@ -18,6 +18,7 @@ module Gargantext.Prelude
, module Protolude , module Protolude
, module Data.String.Conversions , module Data.String.Conversions
, MonadBase(..) , MonadBase(..)
, module Gargantext.Prelude.Error
) )
where where
...@@ -34,14 +35,27 @@ import Data.Semigroup (Semigroup, (<>)) ...@@ -34,14 +35,27 @@ import Data.Semigroup (Semigroup, (<>))
import Data.Set (Set) import Data.Set (Set)
import Data.Set qualified as Set import Data.Set qualified as Set
import Data.String.Conversions (ConvertibleStrings, cs) import Data.String.Conversions (ConvertibleStrings, cs)
import Data.Text (Text)
import Data.Typeable (Typeable) import Data.Typeable (Typeable)
import Data.Vector qualified as V import Data.Vector qualified as V
import GHC.Exts (sortWith) import GHC.Exts (sortWith)
import GHC.Real (round) import GHC.Real (round)
import Protolude import Protolude hiding (panic)
import Protolude qualified as Proto
import Prelude qualified as GHCPrelude
import Gargantext.Prelude.Error
import System.Directory (createDirectoryIfMissing) import System.Directory (createDirectoryIfMissing)
import System.FilePath.Posix (takeDirectory) import System.FilePath.Posix (takeDirectory)
-- Version of panic and error with an explicit warning.
panic :: Text -> a
panic = Proto.panic
{-# DEPRECATED panic "'panic' doesn't attach a stacktrace to the error. Please use 'panicTrace'" #-}
error :: GHCPrelude.String -> a
error = GHCPrelude.error
{-# DEPRECATED error "'error' doesn't attach a stacktrace to the error. Please use 'errorTrace'" #-}
printDebug :: (Show a, MonadBase IO m) => Text -> a -> m () printDebug :: (Show a, MonadBase IO m) => Text -> a -> m ()
printDebug msg x = liftBase . putStrLn $ msg <> " " <> show x printDebug msg x = liftBase . putStrLn $ msg <> " " <> show x
......
module Gargantext.Prelude.Error (
-- * Attaching callstacks to exceptions
WithStacktrace(..)
, UnexpectedPanic(..)
, withStacktrace
-- * Drop-in replacement for panic/error
, panicTrace
, errorTrace
) where
import Control.Exception
import Data.Text qualified as T
import GHC.Stack
import Prelude
-- | A 'WithStacktrace' carries an error alongside its
-- 'CallStack', to be able to print the correct source location
-- of where the error originated.
data WithStacktrace e =
WithStacktrace {
ct_callStack :: !CallStack
, ct_error :: !e
} deriving Show
instance Exception e => Exception (WithStacktrace e) where
displayException WithStacktrace{..}
= displayException ct_error <> "\n" <> prettyCallStack ct_callStack
withStacktrace :: HasCallStack => e -> WithStacktrace e
withStacktrace = withFrozenCallStack . WithStacktrace callStack
newtype UnexpectedPanic = UnexpectedPanic T.Text
deriving Show
instance Exception UnexpectedPanic
panicTrace :: HasCallStack => T.Text -> x
panicTrace = throw . withFrozenCallStack . WithStacktrace callStack . UnexpectedPanic
-- | Drop-in replacement for Prelude's 'error'.
errorTrace :: HasCallStack => String -> x
errorTrace = panicTrace . T.pack
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment