2016-11-24 29 views
0

我已經試過如下:在yesod中,如何發送帶有自定義HTTP狀態碼的defaultLayout?

sendResponseStatus status403 $ (defaultLayout [whamlet|Foo|] :: Handler Html) 

這給了我這種類型的錯誤:

<interactive>:1:1: Warning: 
    Could not deduce (ToTypedContent (Handler Html)) 
     arising from a use of ‘sendResponseStatus’ 
    from the context (MonadHandler m) 
     bound by the inferred type of it :: MonadHandler m => m a 
     at <interactive>:1:1 
    In the expression: sendResponseStatus status403 
    In the expression: 
     sendResponseStatus status403 
     $ (defaultLayout 
      ((asWidgetT . toWidget) 
       ((blaze-markup-0.7.0.3:Text.Blaze.Internal.preEscapedText 
       . Data.Text.pack) 
       "Foo")) :: 
      Handler Html) 

回答

2

事實證明,sendResponseStatus沒有預料Handler Html,但普通Html可代替:

html <- defaultLayout [whamlet|Foo|] 
sendResponseStatus status403 html 

按照我的預期編譯和執行。

它也可能是有意義的封裝這個邏輯,例如:

sendResponseStatusHandler :: (ToTypedContent c, MonadHandler m) => Status -> m c -> m b 
sendResponseStatusHandler status handler = do 
    response <- handler 
    sendResponseStatus status response 

由於能夠在Handler通過似乎更強大了不少給我。

相關問題