forked from wordpress-mobile/WordPress-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the migration error handling logic to include the underlying…
… error (wordpress-mobile#20651)
- Loading branch information
1 parent
aa92c34
commit 591a5ed
Showing
6 changed files
with
124 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
WordPress/Jetpack/Classes/Utility/DataMigrationError.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import Foundation | ||
|
||
enum DataMigrationError { | ||
case databaseImportError(underlyingError: Error) | ||
case databaseExportError(underlyingError: Error) | ||
case backupLocationNil | ||
case sharedUserDefaultsNil | ||
case dataNotReadyToImport | ||
} | ||
|
||
extension DataMigrationError: LocalizedError, CustomNSError { | ||
|
||
var errorDescription: String? { | ||
switch self { | ||
case .backupLocationNil: return "Database shared directory not found" | ||
case .sharedUserDefaultsNil: return "Shared user defaults not found" | ||
case .dataNotReadyToImport: return "The data wasn't ready to import" | ||
case .databaseImportError(let error): return "Import Failed: \(error.localizedDescription)" | ||
case .databaseExportError(let error): return "Export Failed: \(error.localizedDescription)" | ||
} | ||
} | ||
|
||
static var errorDomain: String { | ||
return String(describing: DataMigrationError.self) | ||
} | ||
|
||
var errorCode: Int { | ||
switch self { | ||
case .dataNotReadyToImport: return 100 | ||
case .backupLocationNil: return 200 | ||
case .sharedUserDefaultsNil: return 201 | ||
case .databaseImportError(let error): return 1000 + (error as NSError).code | ||
case .databaseExportError(let error): return 2000 + (error as NSError).code | ||
} | ||
} | ||
|
||
var errorUserInfo: [String: Any] { | ||
switch self { | ||
case .databaseExportError(let error), .databaseImportError(let error): | ||
let nsError = error as NSError | ||
return ["underlying-error-domain": nsError.domain, | ||
"underlying-error-code": nsError.code, | ||
"underlying-error-message": nsError.localizedDescription, | ||
"underlying-error-user-info": nsError.userInfo] | ||
default: | ||
return [:] | ||
} | ||
} | ||
} | ||
|
||
extension DataMigrationError: CustomDebugStringConvertible { | ||
var debugDescription: String { | ||
return "[\(Self.errorDomain)] \(localizedDescription)" | ||
} | ||
} | ||
|
||
extension DataMigrationError: Equatable { | ||
|
||
static func ==(left: DataMigrationError, right: DataMigrationError) -> Bool { | ||
let leftNSError = left as NSError | ||
let rightNSError = right as NSError | ||
return leftNSError == rightNSError | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters