Wizek
Wizek

Reputation: 5003

With yesod, how can one send defaultLayout with a custom HTTP status code?

I've tried the following:

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

Which gives me this type error:

<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)

Upvotes: 1

Views: 165

Answers (1)

Wizek
Wizek

Reputation: 5003

It turns out that sendResponseStatus is not expecting Handler Html, but plain Html works instead:

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

Which compiles and executes as expected for me.

It may also make sense to encapsulate this logic as such:

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

Since being able to pass in a Handler seems quite a bit more powerful to me.

Upvotes: 2

Related Questions