如何使用 Giraffe 处理失败的 post 操作?
How do I handle a failed post operation using Giraffe?
如何使用 Giraffe 处理失败的 post 操作?
对于使用 Giraffe 失败的 post 操作,建议的做法是什么?
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
}
具体来说,如果服务器无法将数据写入某些数据库,我应该return 给客户端什么(会编译)。
处理程序必须 return 一些东西,但它并不总是必须是相同的序列化对象。我只快速浏览了 Giraffe,但使用 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
}
如何使用 Giraffe 处理失败的 post 操作?
对于使用 Giraffe 失败的 post 操作,建议的做法是什么?
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
}
具体来说,如果服务器无法将数据写入某些数据库,我应该return 给客户端什么(会编译)。
处理程序必须 return 一些东西,但它并不总是必须是相同的序列化对象。我只快速浏览了 Giraffe,但使用 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
}