如何使用长颈鹿处理失败的发布操作?
对于使用长颈鹿失败的后期操作,建议的做法是什么?
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
}
具体来说,如果服务器无法将数据写入某个数据库,我应该将什么返回给客户端(将编译)。
处理程序必须返回某些内容,但它并不总是相同的序列化对象。我只是快速浏览了长颈鹿,但在这里使用Suave的类似方法和长颈鹿的例子: 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
}