如何使用Giraffe处理失败的后期操作?
使用长颈鹿进行失败后操作的推荐做法是什么?
let private registrationHandler =
fun(context: HttpContext) ->
async {
let! data = context.BindJson<RegistrationRequest>()
let response = register data |> function
| Success profile -> profile
| Failure -> ???
return! json response context
}
具体来说,如果服务器无法将数据写入某个数据库,我应该将什么返回给客户端(将编译)。
答案 0 :(得分:4)
处理程序必须返回一些东西,但它并不总是必须是同一个序列化对象。我只是快速浏览了一下长颈鹿,但是使用Suave和Giraffe的例子类似的方法:https://github.com/dustinmoris/Giraffe#setstatuscode,我会做这样的事情:
type ErrorResponse = { message: string; ... }
let private registrationHandler =
fun(context: HttpContext) ->
async {
let! data = context.BindJson<RegistrationRequest>()
match register data with
| Success profile ->
return! json profile context
| Failure ->
let response = { message = "registration failed"; ... }
return! (setStatusCode 500 >=> json response) context
}