2017-10-12 79 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 
     }