31 lines
1.0 KiB
Haskell
31 lines
1.0 KiB
Haskell
module Utils (renderCommonmarkViaHtml, responseToServerError, throwToHandler) where
|
|
|
|
import Relude
|
|
|
|
import Commonmark
|
|
import Commonmark.Extensions.PipeTable
|
|
import Commonmark.Extensions.Strikethrough
|
|
import Control.Monad.Except (throwError)
|
|
import Network.HTTP.Types (statusCode)
|
|
import Polysemy (Final, Member, Sem, embedFinal)
|
|
import Servant.Client
|
|
import Servant.Server (Handler, ServerError (..))
|
|
|
|
renderCommonmarkViaHtml :: Text -> Text
|
|
renderCommonmarkViaHtml content =
|
|
commonmarkWith (defaultSyntaxSpec <> strikethroughSpec <> pipeTableSpec) "<message>" content
|
|
& runIdentity
|
|
& either (const content) (toStrict . renderHtml @())
|
|
|
|
responseToServerError :: Response -> ServerError
|
|
responseToServerError resp =
|
|
ServerError
|
|
{ errHTTPCode = resp.responseStatusCode.statusCode
|
|
, errReasonPhrase = "4rum.i2p returned error"
|
|
, errBody = resp.responseBody
|
|
, errHeaders = toList resp.responseHeaders
|
|
}
|
|
|
|
throwToHandler :: Member (Final Handler) rs => ServerError -> Sem rs a
|
|
throwToHandler = embedFinal @Handler . throwError
|