2017-05-02 100 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) 

太糟糕了,這不是對我來說更明顯的錯誤類型。