Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry on -1005 once when requesting token #159

Merged
merged 2 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions LineSDK/LineSDK/Login/LoginProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,28 +256,44 @@ public class LoginProcess {
// It seems that plan A in the comment above also works great (even when the background execution time
// expired). But I cannot explain why the `URLSession` can retry the request even when background task ends.
// Maybe it is some internal implementation. Delay the request now works fine so we choose it as a workaround.
//
// In some edge cases, the network would be still lost after 0.3 sec of delay. But it should be very rare.
// So an auto retry for NSURLErrorNetworkConnectionLost (-1005) is applied to make sure the error not happen.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
do {
let response = try LoginProcessURLResponse(from: url, validatingWith: self.processID)
let tokenExchangeRequest = PostExchangeTokenRequest(
channelID: self.configuration.channelID,
code: response.requestToken,
codeVerifier: self.pkce.codeVerifier,
redirectURI: Constant.thirdPartyAppReturnURL,
optionalRedirectURI: self.configuration.universalLinkURL?.absoluteString)
Session.shared.send(tokenExchangeRequest) { tokenResult in
switch tokenResult {
case .success(let token): self.invokeSuccess(result: token, response: response)
case .failure(let error): self.invokeFailure(error: error)
}
}
self.exchangeToken(response: response, canRetryOnNetworkLost: true)
} catch {
self.invokeFailure(error: error)
}
}

return true
}

private func exchangeToken(response: LoginProcessURLResponse, canRetryOnNetworkLost: Bool) {

let tokenExchangeRequest = PostExchangeTokenRequest(
channelID: self.configuration.channelID,
code: response.requestToken,
codeVerifier: self.pkce.codeVerifier,
redirectURI: Constant.thirdPartyAppReturnURL,
optionalRedirectURI: self.configuration.universalLinkURL?.absoluteString)
Session.shared.send(tokenExchangeRequest) { tokenResult in
switch tokenResult {
case .success(let token): self.invokeSuccess(result: token, response: response)
case .failure(let error):
if error.isURLSessionErrorCode(sessionErrorCode: NSURLErrorNetworkConnectionLost) && canRetryOnNetworkLost {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
self.exchangeToken(response: response, canRetryOnNetworkLost: false)
}
} else {
self.invokeFailure(error: error)
}
}
}

}

private var canUseLineAuthV2: Bool {
return Constant.isLINEInstalled
Expand Down
6 changes: 6 additions & 0 deletions LineSDK/LineSDKTests/LineSDKErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class LineSDKErrorTests: XCTestCase {
XCTAssertTrue(error.isResponseError(statusCode: 123))
XCTAssertFalse(error.isResponseError(statusCode: 321))
}

func testURLSessionError() {
let networkLostError = NSError(domain: NSURLErrorDomain, code: -1005, userInfo: nil)
let error = LineSDKError.responseFailed(reason: .URLSessionError(networkLostError))
XCTAssertTrue(error.isURLSessionErrorCode(sessionErrorCode: NSURLErrorNetworkConnectionLost))
}

func testIsBadRequest() {
let err = APIError(InternalAPIError(message: "Bad request"))
Expand Down