happstack-lite を使用して Web アプリケーションを作成しています。ServerPart Response ハンドラが純粋な関数を呼び出し、この関数がerrorを呼び出すと、ハンドラ全体が失敗し、happstack がデフォルトのエラー メッセージを出力します。
handler :: ServerPart Response
handler = do
head [] `seq` ok $ toResponse $ H.html "mymessage"
サーバー エラー: Prelude.head: 空のリスト
この例外をキャッチしてカスタム メッセージを返すことは可能でしょうか。残念ながら、ServerPart モナドでcatchを使用する方法がわかりません。
理想的には、ハンドラーは次のコードのようになります。
handler :: ServerPart Response
handler = do
catch
(head [] `seq` ok $ toResponse $ H.html "my message")
(ok $ toResponse $ H.html "my error")
更新:履歴用にソリューションを保存する
handler :: ServerPart Response
handler = do
let good = ok $ toResponse $ H.html "my message"
let bad = ok $ toResponse $ H.html "my error"
join (liftIO $ catch
(do
let n = head (tail [1])
_ <- print n
return good
)
(\(E.ErrorCall _) -> return bad))