From 42579be371e3ecfeee50450dce89ff07ed908596 Mon Sep 17 00:00:00 2001 From: Amir Abbas Date: Sat, 11 Feb 2017 18:05:21 +0330 Subject: [PATCH] Added FileProviderHTTPError protocol - All errors from local provider is now from Cocoa error domain. No URL domain anymore. - All errors from WebDAV/OneDrive/Dropbox conform to FileProviderHTTPError --- FileProvider.podspec | 2 +- FileProvider.xcodeproj/project.pbxproj | 4 +-- Sources/DropboxHelper.swift | 6 +---- Sources/LocalFileProvider.swift | 4 +-- Sources/OneDriveHelper.swift | 6 +---- Sources/RemoteSession.swift | 14 +++++++++++ Sources/WebDAVFileProvider.swift | 34 +++++++++++++------------- 7 files changed, 38 insertions(+), 32 deletions(-) diff --git a/FileProvider.podspec b/FileProvider.podspec index 9bded2d..5df38cd 100644 --- a/FileProvider.podspec +++ b/FileProvider.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |s| # s.name = "FileProvider" - s.version = "0.12.4" + s.version = "0.12.5" s.summary = "FileManager replacement for Local and Remote (WebDAV/Dropbox/OneDrive/SMB2) files on iOS and macOS." # This description is used to generate tags and improve search results. diff --git a/FileProvider.xcodeproj/project.pbxproj b/FileProvider.xcodeproj/project.pbxproj index 3890b86..f1d1602 100644 --- a/FileProvider.xcodeproj/project.pbxproj +++ b/FileProvider.xcodeproj/project.pbxproj @@ -597,7 +597,7 @@ 799396601D48B7BF00086753 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { - BUNDLE_VERSION_STRING = 0.12.4; + BUNDLE_VERSION_STRING = 0.12.5; CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_EMPTY_BODY = YES; @@ -627,7 +627,7 @@ 799396611D48B7BF00086753 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { - BUNDLE_VERSION_STRING = 0.12.4; + BUNDLE_VERSION_STRING = 0.12.5; CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_EMPTY_BODY = YES; diff --git a/Sources/DropboxHelper.swift b/Sources/DropboxHelper.swift index 2e93de0..b081033 100644 --- a/Sources/DropboxHelper.swift +++ b/Sources/DropboxHelper.swift @@ -8,14 +8,10 @@ import Foundation -public struct FileProviderDropboxError: Error, CustomStringConvertible { +public struct FileProviderDropboxError: FileProviderHTTPError { public let code: FileProviderHTTPErrorCode public let path: String public let errorDescription: String? - - public var description: String { - return code.description - } } public final class DropboxFileObject: FileObject { diff --git a/Sources/LocalFileProvider.swift b/Sources/LocalFileProvider.swift index 0b60143..0252844 100644 --- a/Sources/LocalFileProvider.swift +++ b/Sources/LocalFileProvider.swift @@ -161,7 +161,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo let opType = FileOperationType.move(source: path, destination: toPath) if !overwrite && self.fileManager.fileExists(atPath: self.url(of: toPath).path) { - completionHandler?(self.throwError(toPath, code: URLError.cannotMoveFile as FoundationErrorEnum)) + completionHandler?(self.throwError(toPath, code: CocoaError.fileWriteFileExists as FoundationErrorEnum)) return nil } @@ -173,7 +173,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo let opType = FileOperationType.copy(source: path, destination: toPath) if !overwrite && self.fileManager.fileExists(atPath: self.url(of: toPath).path) { - completionHandler?(self.throwError(toPath, code: URLError.cannotWriteToFile as FoundationErrorEnum)) + completionHandler?(self.throwError(toPath, code: CocoaError.fileWriteFileExists as FoundationErrorEnum)) return nil } diff --git a/Sources/OneDriveHelper.swift b/Sources/OneDriveHelper.swift index c813417..022c7d6 100644 --- a/Sources/OneDriveHelper.swift +++ b/Sources/OneDriveHelper.swift @@ -8,14 +8,10 @@ import Foundation -public struct FileProviderOneDriveError: Error, CustomStringConvertible { +public struct FileProviderOneDriveError: FileProviderHTTPError { public let code: FileProviderHTTPErrorCode public let path: String public let errorDescription: String? - - public var description: String { - return code.description - } } public final class OneDriveFileObject: FileObject { diff --git a/Sources/RemoteSession.swift b/Sources/RemoteSession.swift index 72cde7c..eae7b06 100644 --- a/Sources/RemoteSession.swift +++ b/Sources/RemoteSession.swift @@ -61,6 +61,20 @@ open class RemoteOperationHandle: OperationHandle { } } +public protocol FileProviderHTTPError: Error, CustomStringConvertible { + var code: FileProviderHTTPErrorCode { get } + var path: String { get } + var errorDescription: String? { get } + + var description: String { get } +} + +extension FileProviderHTTPError { + public var description: String { + return code.description + } +} + class SessionDelegate: NSObject, URLSessionDataDelegate, URLSessionDownloadDelegate { weak var fileProvider: FileProvider? diff --git a/Sources/WebDAVFileProvider.swift b/Sources/WebDAVFileProvider.swift index 1a39810..6bb2c46 100644 --- a/Sources/WebDAVFileProvider.swift +++ b/Sources/WebDAVFileProvider.swift @@ -89,7 +89,7 @@ open class WebDAVFileProvider: FileProviderBasicRemote { runDataTask(with: request, operationHandle: RemoteOperationHandle(operationType: opType, tasks: []), completionHandler: { (data, response, error) in var responseError: FileProviderWebDavError? if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) { - responseError = FileProviderWebDavError(code: rCode, url: url) + responseError = FileProviderWebDavError(code: rCode, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8), url: url) } var fileObjects = [WebDavFileObject]() if let data = data { @@ -116,7 +116,7 @@ open class WebDAVFileProvider: FileProviderBasicRemote { runDataTask(with: request, completionHandler: { (data, response, error) in var responseError: FileProviderWebDavError? if let code = (response as? HTTPURLResponse)?.statusCode, code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) { - responseError = FileProviderWebDavError(code: rCode, url: url) + responseError = FileProviderWebDavError(code: rCode, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8), url: url) } if let data = data { let xresponse = DavResponse.parse(xmlResponse: data, baseURL: self.baseURL) @@ -172,7 +172,7 @@ extension WebDAVFileProvider: FileProviderOperations { let task = session.dataTask(with: request, completionHandler: { (data, response, error) in var responseError: FileProviderWebDavError? if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) { - responseError = FileProviderWebDavError(code: rCode, url: url) + responseError = FileProviderWebDavError(code: rCode, path: url.relativePath, errorDescription: String(data: data ?? Data(), encoding: .utf8), url: url) } completionHandler?(responseError ?? error) self.delegateNotify(opType, error: responseError ?? error) @@ -194,7 +194,7 @@ extension WebDAVFileProvider: FileProviderOperations { let task = session.uploadTask(with: request, from: data, completionHandler: { (data, response, error) in var responseError: FileProviderWebDavError? if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) { - responseError = FileProviderWebDavError(code: rCode, url: url) + responseError = FileProviderWebDavError(code: rCode, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8), url: url) } completionHandler?(responseError ?? error) self.delegateNotify(opType, error: responseError ?? error) @@ -232,7 +232,8 @@ extension WebDAVFileProvider: FileProviderOperations { } func doOperation(operation opType: FileOperationType, overwrite: Bool? = nil, completionHandler: SimpleCompletionHandler) -> OperationHandle? { - let sourceURL = self.url(of:opType.source!) + let source = opType.source! + let sourceURL = self.url(of: source) var request = URLRequest(url: sourceURL) if let dest = opType.destination { request.setValue(url(of:dest).absoluteString, forHTTPHeaderField: "Destination") @@ -255,12 +256,13 @@ extension WebDAVFileProvider: FileProviderOperations { var responseError: FileProviderWebDavError? if let response = response as? HTTPURLResponse, let code = FileProviderHTTPErrorCode(rawValue: response.statusCode) { if response.statusCode >= 300 { - responseError = FileProviderWebDavError(code: code, url: sourceURL) + responseError = FileProviderWebDavError(code: code, path: source, errorDescription: String(data: data ?? Data(), encoding: .utf8), url: sourceURL) } if code == .multiStatus, let data = data { let xresponses = DavResponse.parse(xmlResponse: data, baseURL: self.baseURL) for xresponse in xresponses where (xresponse.status ?? 0) >= 300 { - completionHandler?(FileProviderWebDavError(code: code, url: sourceURL)) + let error = FileProviderWebDavError(code: code, path: source, errorDescription: String(data: data, encoding: .utf8), url: sourceURL) + completionHandler?(error) } } } @@ -288,7 +290,7 @@ extension WebDAVFileProvider: FileProviderOperations { let task = session.uploadTask(with: request, fromFile: localFile, completionHandler: { (data, response, error) in var responseError: FileProviderWebDavError? if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) { - responseError = FileProviderWebDavError(code: rCode, url: url) + responseError = FileProviderWebDavError(code: rCode, path: toPath, errorDescription: String(data: data ?? Data(), encoding: .utf8), url: url) } completionHandler?(responseError ?? error) self.delegateNotify(opType, error: responseError ?? error) @@ -309,7 +311,7 @@ extension WebDAVFileProvider: FileProviderOperations { let task = session.downloadTask(with: request, completionHandler: { (sourceFileURL, response, error) in var responseError: FileProviderWebDavError? if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) { - responseError = FileProviderWebDavError(code: rCode, url: url) + responseError = FileProviderWebDavError(code: rCode, path: path, errorDescription: nil, url: url) } if let sourceFileURL = sourceFileURL { do { @@ -351,7 +353,7 @@ extension WebDAVFileProvider: FileProviderReadWrite { runDataTask(with: request, operationHandle: handle, completionHandler: { (data, response, error) in var responseError: FileProviderWebDavError? if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) { - responseError = FileProviderWebDavError(code: rCode, url: url) + responseError = FileProviderWebDavError(code: rCode, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8), url: url) } completionHandler(data, responseError ?? error) }) @@ -374,7 +376,7 @@ extension WebDAVFileProvider: FileProviderReadWrite { let task = session.uploadTask(with: request, from: data, completionHandler: { (data, response, error) in var responseError: FileProviderWebDavError? if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) { - responseError = FileProviderWebDavError(code: rCode, url: self.url(of: path)) + responseError = FileProviderWebDavError(code: rCode, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8), url: self.url(of: path)) } defer { self.delegateNotify(opType, error: responseError ?? error) @@ -403,7 +405,7 @@ extension WebDAVFileProvider: FileProviderReadWrite { // FIXME: paginating results var responseError: FileProviderWebDavError? if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) { - responseError = FileProviderWebDavError(code: rCode, url: url) + responseError = FileProviderWebDavError(code: rCode, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8), url: url) } if let data = data { let xresponse = DavResponse.parse(xmlResponse: data, baseURL: self.baseURL) @@ -600,11 +602,9 @@ public final class WebDavFileObject: FileObject { } } -public struct FileProviderWebDavError: Error, CustomStringConvertible { +public struct FileProviderWebDavError: FileProviderHTTPError { public let code: FileProviderHTTPErrorCode + public let path: String + public let errorDescription: String? public let url: URL - - public var description: String { - return code.description - } }