2017-10-12 121 views
3

如何处理使用长颈鹿的失败后操作?如何处理使用长颈鹿失败的后期操作?

使用长颈鹿失败后操作的建议做法是什么?

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 
     } 

具体来说,如果服务器无法将数据写入到一些数据库,我应该返回给客户端(这将编译)。

回答

4

处理程序必须返回某些内容,但并不总是必须是相同的序列化对象。我只能快速浏览长颈鹿,但使用类似的方法从长滩与长颈鹿的例子在这里: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 
     }