2017-05-02 63 views
0

当我尝试:如何在`Yesod Handler'中捕捉异常?

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,不是吗?

+0

尝试使用[来自提升基础包的'catch'版本](https://hackage.haskell.org/package/lifted-base-0.2.3.10/docs/Control-Exception-Lifted.html#五:抓)。 –

+0

@AlexisKing谢谢,试试看。 – Wizek

+0

@AlexisKing我得到了完全相同的类型错误:'无法匹配类型'( - >)e0'与'IO'' – Wizek

回答

2

我只是缺少论据catch

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

太糟糕了,这不是对我来说更明显的错误类型。