Skip to content

Commit

Permalink
Refactoring and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
iammajid committed Oct 14, 2024
1 parent 6b63b7c commit 0c1a348
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Sources/CryptomatorCloudAccess/Box/BoxCloudProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ public class BoxCloudProvider: CloudProvider {

private func convertStandardError(_ error: Error) -> Error {
switch error {
case let error as BoxAPIError where error.responseInfo.statusCode == 400 || error.responseInfo.statusCode == 401:
case let error as BoxAPIError where error.responseInfo.statusCode == 401 || error.description.contains("Invalid refresh token"):
return CloudProviderError.unauthorized
case let error as BoxAPIError where error.responseInfo.statusCode == 404:
return CloudProviderError.itemNotFound
Expand Down
51 changes: 22 additions & 29 deletions Sources/CryptomatorCloudAccess/Dropbox/DropboxCloudProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public class DropboxCloudProvider: CloudProvider {
switch dropboxError {
case .tooManyWriteOperations, .internalServerError, .rateLimitError:
return true
case .authError:
return false
default:
return false
}
Expand Down Expand Up @@ -193,12 +191,8 @@ public class DropboxCloudProvider: CloudProvider {
return
}
if let networkError = networkError {
CloudAccessDDLogDebug("DropboxCloudProvider: fetchItemMetadata(at: \(cloudPath.path)) failed with networkError: \(networkError)")
if networkError.isAuthError() {
reject(CloudProviderError.unauthorized)
} else {
reject(self.convertRequestErrorToDropboxError(networkError))
}
CloudAccessDDLogDebug("DropboxCloudProvider: fetchItemMetadata failed with networkError: \(networkError)")
reject(self.convertRequestError(networkError))
return
}
guard let result = result else {
Expand Down Expand Up @@ -247,7 +241,7 @@ public class DropboxCloudProvider: CloudProvider {
}
if let networkError = networkError {
CloudAccessDDLogDebug("DropboxCloudProvider: fetchItemList(at: \(cloudPath.path)) failed with networkError: \(networkError)")
reject(self.convertRequestErrorToDropboxError(networkError))
reject(self.convertRequestError(networkError))
return
}
guard let result = result else {
Expand Down Expand Up @@ -290,7 +284,7 @@ public class DropboxCloudProvider: CloudProvider {
if networkError.isBadInputError(), let errorContent = networkError.errorContent, errorContent.contains("invalidPageToken") {
reject(CloudProviderError.pageTokenInvalid)
} else {
reject(self.convertRequestErrorToDropboxError(networkError))
reject(self.convertRequestError(networkError))
}
return
}
Expand Down Expand Up @@ -337,7 +331,7 @@ public class DropboxCloudProvider: CloudProvider {
if networkError.isClientError(), case CocoaError.fileWriteFileExists = networkError.asClientError().nsError {
reject(CloudProviderError.itemAlreadyExists)
} else {
reject(self.convertRequestErrorToDropboxError(networkError))
reject(self.convertRequestError(networkError))
}
return
}
Expand Down Expand Up @@ -401,13 +395,13 @@ public class DropboxCloudProvider: CloudProvider {
guard let requestError = fileUrlsToRequestErrors[localURL] else {
return DropboxError.unexpectedError
}
return convertRequestErrorToDropboxError(requestError)
return convertRequestError(requestError)
} else if let finishBatchRouteError = finishBatchRouteError {
CloudAccessDDLogDebug("DropboxCloudProvider: handleBatchUploadMissingResult(for: \(localURL)) failed with finishBatchRouteError: \(finishBatchRouteError)")
return DropboxError.asyncPollError
} else if let finishBatchRequestError = finishBatchRequestError {
CloudAccessDDLogDebug("DropboxCloudProvider: handleBatchUploadMissingResult(for: \(localURL)) failed with finishBatchRequestError: \(finishBatchRequestError)")
return convertRequestErrorToDropboxError(finishBatchRequestError)
return convertRequestError(finishBatchRequestError)
} else {
CloudAccessDDLogDebug("DropboxCloudProvider: handleBatchUploadMissingResult(for: \(localURL)) failed with missingResult")
return DropboxError.missingResult
Expand Down Expand Up @@ -443,7 +437,7 @@ public class DropboxCloudProvider: CloudProvider {
}
if let networkError = networkError {
CloudAccessDDLogDebug("DropboxCloudProvider: uploadSmallFile(from: \(localURL), to: \(cloudPath.path), mode: \(mode?.description() ?? "nil")) failed with networkError: \(networkError)")
reject(self.convertRequestErrorToDropboxError(networkError))
reject(self.convertRequestError(networkError))
return
}
guard let result = result else {
Expand Down Expand Up @@ -479,7 +473,7 @@ public class DropboxCloudProvider: CloudProvider {
}
if let networkError = networkError {
CloudAccessDDLogDebug("DropboxCloudProvider: createFolder(at: \(cloudPath.path)) failed with networkError: \(networkError)")
reject(self.convertRequestErrorToDropboxError(networkError))
reject(self.convertRequestError(networkError))
return
}
guard let result = result else {
Expand Down Expand Up @@ -512,7 +506,7 @@ public class DropboxCloudProvider: CloudProvider {
}
if let networkError = networkError {
CloudAccessDDLogDebug("DropboxCloudProvider: deleteItem(at: \(cloudPath.path)) failed with networkError: \(networkError)")
reject(self.convertRequestErrorToDropboxError(networkError))
reject(self.convertRequestError(networkError))
return
}
guard let result = result else {
Expand Down Expand Up @@ -551,7 +545,7 @@ public class DropboxCloudProvider: CloudProvider {
}
if let networkError = networkError {
CloudAccessDDLogDebug("DropboxCloudProvider: moveItem(from: \(sourceCloudPath.path), to: \(targetCloudPath.path)) failed with networkError: \(networkError)")
reject(self.convertRequestErrorToDropboxError(networkError))
reject(self.convertRequestError(networkError))
return
}
guard let result = result else {
Expand All @@ -571,11 +565,10 @@ public class DropboxCloudProvider: CloudProvider {
if parentCloudPath == CloudPath("/") {
return Promise(())
}
return checkForItemExistence(at: parentCloudPath).then { itemExists in
return checkForItemExistence(at: parentCloudPath).then { itemExists -> Void in
guard itemExists else {
throw CloudProviderError.parentFolderDoesNotExist
}
return Promise(())
}
}

Expand Down Expand Up @@ -649,26 +642,26 @@ public class DropboxCloudProvider: CloudProvider {
}
}

func convertRequestErrorToDropboxError(_ error: DBRequestError) -> DropboxError {
func convertRequestError(_ error: DBRequestError) -> Error {
if error.isHttpError() {
return .httpError
return DropboxError.httpError
} else if error.isBadInputError() {
return .badInputError
return DropboxError.badInputError
} else if error.isAuthError() {
return .authError
return CloudProviderError.unauthorized
} else if error.isAccessError() {
return .accessError
return DropboxError.accessError
} else if error.isPathRootError() {
return .pathRootError
return DropboxError.pathRootError
} else if error.isRateLimitError() {
let rateLimitError = error.asRateLimitError()
return .rateLimitError(retryAfter: rateLimitError.backoff.intValue)
return DropboxError.rateLimitError(retryAfter: rateLimitError.backoff.intValue)
} else if error.isInternalServerError() {
return .internalServerError
return DropboxError.internalServerError
} else if error.isClientError() {
return .clientError
return DropboxError.clientError
} else {
return .unexpectedError
return DropboxError.unexpectedError
}
}
}
1 change: 0 additions & 1 deletion Sources/CryptomatorCloudAccess/Dropbox/DropboxError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public enum DropboxError: Error {

case httpError
case badInputError
case authError
case accessError
case pathRootError
case rateLimitError(retryAfter: Int)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright © 2020 Skymatic GmbH. All rights reserved.
//

import AppAuthCore
import Foundation
import GoogleAPIClientForREST_Drive
import GRDB
Expand Down Expand Up @@ -532,7 +533,7 @@ public class GoogleDriveCloudProvider: CloudProvider {
CloudAccessDDLogDebug("GoogleDriveCloudProvider: executeQuery(\(query.requestID)) failed with error: \(error)")
if error.domain == NSURLErrorDomain, error.code == NSURLErrorNotConnectedToInternet || error.code == NSURLErrorCannotConnectToHost || error.code == NSURLErrorNetworkConnectionLost || error.code == NSURLErrorDNSLookupFailed || error.code == NSURLErrorResourceUnavailable || error.code == NSURLErrorInternationalRoamingOff {
reject(CloudProviderError.noInternetConnection)
} else if error.domain == kGTLRErrorObjectDomain || error.domain == "org.openid.appauth.oauth_token", error.code == 401 || error.code == 403 || error.code == -10 {
} else if ((error.domain == kGTLRErrorObjectDomain && (error.code == 401 || error.code == 403)) || (error.domain == OIDOAuthTokenErrorDomain && error.code == -10)) {
reject(CloudProviderError.unauthorized)
} else if error.domain == kGTLRErrorObjectDomain, error.code == 404 {
reject(CloudProviderError.itemNotFound)
Expand Down

0 comments on commit 0c1a348

Please sign in to comment.