【问题标题】:With yesod, how can one send defaultLayout with a custom HTTP status code?使用 yesod,如何发送带有自定义 HTTP 状态代码的 defaultLayout?
【发布时间】:2016-11-24 08:06:44
【问题描述】:

我尝试了以下方法:

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)

【问题讨论】:

    标签: haskell yesod


    【解决方案1】:

    事实证明,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 对我来说似乎更强大。

    【讨论】:

      猜你喜欢
      • 2018-05-07
      • 2012-10-06
      • 2016-08-17
      • 2014-11-11
      • 1970-01-01
      • 2014-08-01
      • 1970-01-01
      • 2023-03-06
      • 2015-09-10
      相关资源
      最近更新 更多