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

问题描述 投票:1回答:1

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

我有以下导入(注意我的前奏实际上是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功能只是围绕着tinylx的log功能的薄包装 - 随意用任何感觉符合人体工程学的替代品。
haskell classy-prelude
1个回答
1
投票

我现在明白我的问题是Exception论证需要一个具体的类型,因为它是我的问题中所述的多态函数,并且没有调用站点将其缩小到特定类型。 Catch'em all中描述了正确的答案! 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
© www.soinside.com 2019 - 2024. All rights reserved.