如何在Yesod处理程序中“捕获”异常?

时间:2017-05-02 23:07:06

标签: haskell yesod monad-transformers

当我尝试时:

foos <- (return $ map (encode .> cs .> jsonToFoo body) `catch` r400

我收到以下类型错误:

/path/to/File.hs:47:78: error:
    • Couldn't match type ‘(->) e0’ with ‘IO’
        arising from a functional dependency between:
          constraint ‘MonadBase IO ((->) e0)’ arising from a use of ‘r400’
          instance ‘MonadBase ((->) r) ((->) r)’ at <no location info>
    • In the second argument of ‘catch’, namely ‘r400’
      In a stmt of a 'do' block:
        foos <- (return $ map (encode .> cs .> jsonToFoo) body)
                `catch` r400
      In the expression:
        do { (body :: [Value]) <- requireJsonBody;
             foos <- (return $ map (encode .> cs .> jsonToFoo) body)
                     `catch` r400;
             .... }

如果有办法,它涉及liftIO,不是吗?

1 个答案:

答案 0 :(得分:2)

我只是错过了catch

的论点
foos <- (return $ map (encode .> cs .> jsonToFoo body)
  `catch` (\(e :: SomeException) -> r400)

太糟糕了,从类型错误来看,这对我来说并不是很明显。