[F#] Google Drive API 오류 처리

10459 단어 F#Google Drivetech

전제 조건


응답: System.Net.Http.HttpResponseMessage [↗]

성공 여부를 판정하다


200-299


copy, create, export, generateIds, get, list, update, watch.
応答.IsSuccessStatusCode

204


delete [1] , emptyTrash [2] .
応答.StatusCode = System.Net.HttpStatusCode.NoContent

처리 가능한 오류인지 판정하기[3]


401(처음)[4]


잘못된 액세스 토큰입니다.
応答.StatusCode = System.Net.HttpStatusCode.Unauthorized

429 [5] , 500, 502, 503, 504 [6]


시간이 나면 해결될 수 있는 착오.
let retryCodes =
    Set [
        System.Net.HttpStatusCode.TooManyRequests      // 429
        System.Net.HttpStatusCode.InternalServerError  // 500
        System.Net.HttpStatusCode.BadGateway           // 502
        System.Net.HttpStatusCode.ServiceUnavailable   // 503
        System.Net.HttpStatusCode.GatewayTimeout       // 504
    ]

retryCodes |> Set.contains 応答.StatusCode

오류 처리 설치 예


지수 반환[7]


let randomNumberGenerator = System.Random()

let Backoff (retryCount: int) =
    let currentWaitMilliseconds =
        min
        <| (2. ** float retryCount) * 1000.
        <| 32000.
    let jitterMilliseconds = int currentWaitMilliseconds + randomNumberGenerator.Next(1, 1001)
    System.Threading.Tasks.Task.Delay(jitterMilliseconds).Wait()

오류 처리된 위조 코드


let rec TryXX (retryCount: int) =
    match リクエスト処理() with
    | r when r.IsSuccessStatusCode -> 成功時の処理
    | r when r.StatusCode = System.Net.HttpStatusCode.Unauthorized ->
        match retryCount with
        | 0 -> refresh()
        | _ -> failwith "アクセストークン更新処理のエラー"
    | r when retryCodes |> Set.contains r.StatusCode -> backoff retryCount
    | _ -> failwith "未定義なエラー"
and refresh () =
    アクセストークンの更新処理
    TryXX 1
and backoff (retryCount: int) =
    match retryCount with
    | 試行回数の上限 -> failwith "試行回数の上限"
    | _ -> Backoff retryCount
    TryXX (retryCount + 1)

// ...

TryXX 0

end


각주
https://developers.google.com/drive/api/v3/reference/files/delete#response ↩︎
https://developers.google.com/drive/api/v3/reference/files/emptyTrash#response ↩︎
https://developers.google.com/drive/api/v3/handle-errors ↩︎
https://developers.google.com/drive/api/v3/handle-errors#resolve_a_401_error_invalid_credentials ↩︎
https://developers.google.com/drive/api/v3/handle-errors#resolve_a_429_error_too_many_requests ↩︎
https://developers.google.com/drive/api/v3/handle-errors#resolve_a_500_error_backend_error ↩︎
https://cloud.google.com/storage/docs/retry-strategy ↩︎

좋은 웹페이지 즐겨찾기