使用yesod,如何使用自定义HTTP状态代码发送defaultLayout?

时间:2016-11-24 00:06:45

标签: haskell yesod

我尝试了以下内容:

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)

1 个答案:

答案 0 :(得分: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对我来说似乎有点强大。