如何在Haskell中记录所有异常?

时间:2019-02-18 20:39:00

标签: haskell classy-prelude

原始标题在检查所有异常时如何处理多个异常类型实例?

我具有以下导入(请注意,我的前奏实际上是ClassyPrelude,它使用UnliftIO.Exception)。请注意,System.Logger来自tinylog,它是fast-logger之上的精简库。

import           Prelude                   hiding(log)
import           System.Logger             hiding(log)
import qualified System.Logger             as TL

以及以下功能:

logExceptions :: MonadUnliftIO m => Logger -> m a -> m a
logExceptions logger program = withException program
  (\ex -> do
    logIt Warn logger ["Logging exception: ", (show ex)]
    flush logger
    )

将lambda放入具有类型的本地函数中可能会使其更加清晰:

logExceptions :: MonadUnliftIO m => Logger -> m a -> m a
logExceptions logger program = withException program logEx
  where
    logEx :: (MonadUnliftIO m, Exception e) => e -> m ()
    logEx ex = do
      logIt Warn logger ["Logging exception: ", (show ex)]
      flush logger

这将导致以下编译错误:

    * Could not deduce (Exception e0)
        arising from a use of `withException'
      from the context: MonadUnliftIO m
        bound by the type signature for:
                   logExceptions :: forall (m :: * -> *) a.
                                    MonadUnliftIO m =>
                                    Logger -> m a -> m a
        at src/FDS/Logging.hs:19:1-56
      The type variable `e0' is ambiguous
      These potential instances exist:
        instance Exception SomeException -- Defined in `GHC.Exception.Type'
        instance Exception IOException -- Defined in `GHC.IO.Exception'
        instance Exception SomeAsyncException
          -- Defined in `GHC.IO.Exception'
        ...plus four others         
        ...plus 30 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    * In the expression: withException program logEx
      In an equation for `logExceptions':
          logExceptions logger program
            = withException program logEx
            where                   
                logEx :: (MonadUnliftIO m, Exception e) => e -> m ()
                logEx ex            
                  = do logIt Warn logger ...
                       ....         
   |                                
20 | logExceptions logger program = withException program logEx
   |         

最令人担忧的是plus 30 instances involving out-of-scope types。我可以隐藏这些进口以稍微改善情况:

import           GHC.Exception.Type        hiding(SomeException)
import           GHC.IO.Exception          hiding(IOException, SomeAsyncException)

但是遍历并找到所有30多种异常类型并以这种方式掩盖所有异常类型似乎并不合理。我认为我在这里做错了什么,还是真的需要检查并掩盖所有内容?

注意

  1. 我的logIt函数只是tinylog的log函数的一个薄包装-可以随意替换为符合人体工程学的东西。

1 个答案:

答案 0 :(得分:1)

我现在理解我的问题是Exception参数需要一个具体类型,因为它是我的问题中所述的多态函数,并且没有调用站点将其范围缩小到特定类型。正确答案在全部捕获! enter image description here中进行了描述,它是使用具体的SomeException类型。结果代码为:

logExceptions :: MonadUnliftIO m => Logger -> m a -> m a
logExceptions logger program = withException program logEx
  where
    logEx :: MonadUnliftIO m => SomeException -> m ()
    logEx ex = do
      logIt Warn logger ["Logging exception: ", (show ex)]
      flush logger