Skip to content

Instantly share code, notes, and snippets.

@temoki
Last active July 21, 2020 05:56
Show Gist options
  • Save temoki/2239eba5eab10e0d6236621e65b35f84 to your computer and use it in GitHub Desktop.
Save temoki/2239eba5eab10e0d6236621e65b35f84 to your computer and use it in GitHub Desktop.
Error as NSError
import Foundation
struct StructError: Error {
var localizedDescription: String { "StructError.localizedDescription" }
}
class ClassError: Error {
var localizedDescription: String { "ClassError.localizedDescription" }
}
enum EnumError: Error {
case case1
case case2
case case3
var localizedDescription: String { "EnumError.localizedDescription" }
}
enum IntEnumError: String, Error {
case case1
case case2
case case3
var localizedDescription: String { "IntEnumError.localizedDescription" }
}
enum StringEnumError: String, Error {
case case1
case case2
case case3
var localizedDescription: String { "StringEnumError.localizedDescription" }
}
struct StructLocalizedError: LocalizedError {
var errorDescription: String? { "StructLocalizedError.errorDescription" }
var failureReason: String? { "StructLocalizedError.failureReason" }
var recoverySuggestion: String? { "StructLocalizedError.recoverySuggestion" }
var helpAnchor: String? { "StructLocalizedError.helpAnchor" }
}
struct StructRecoverableError: RecoverableError {
var recoveryOptions: [String] { ["StructRecoverableError.recoveryOptions.1", "StructRecoverableError.recoveryOptions.2"] }
func attemptRecovery(optionIndex recoveryOptionIndex: Int, resultHandler handler: @escaping (Bool) -> Void) {
handler(true)
}
func attemptRecovery(optionIndex recoveryOptionIndex: Int) -> Bool {
return true
}
}
struct StructCustomNSError: CustomNSError {
static var errorDomain: String { "StructCustomNSError.errorDomain" }
var errorCode: Int { 123 }
var errorUserInfo: [String : Any] { ["StructCustomNSError.UserInfo.Key1": 456, "StructCustomNSError.UserInfo.Key2": 789] }
}
func printErrorAsNSError(_ error: Error) {
print("")
print(String(describing: type(of: error)))
let nsError = error as NSError
print("domain ", nsError.domain)
print("code ", nsError.code)
print("userInfo ", nsError.userInfo)
print("localizedDescription ", nsError.localizedDescription)
print("localizedFailureReason ", nsError.localizedFailureReason ?? "(nil)")
print("localizedRecoverySuggestion", nsError.localizedRecoverySuggestion ?? "(nil)")
print("localizedRecoveryOptions ", nsError.localizedRecoveryOptions ?? "(nil)")
print("recoveryAttempter ", nsError.recoveryAttempter ?? "(nil)")
print("helpAnchor ", nsError.helpAnchor ?? "(nil)")
}
printErrorAsNSError(StructError())
printErrorAsNSError(ClassError())
printErrorAsNSError(EnumError.case3)
printErrorAsNSError(StringEnumError.case3)
printErrorAsNSError(IntEnumError.case3)
printErrorAsNSError(StructLocalizedError())
printErrorAsNSError(StructRecoverableError())
printErrorAsNSError(StructCustomNSError())
StructError
domain __lldb_expr_7.StructError
code 1
userInfo [:]
localizedDescription The operation couldn’t be completed. (__lldb_expr_7.StructError error 1.)
localizedFailureReason (nil)
localizedRecoverySuggestion (nil)
localizedRecoveryOptions (nil)
recoveryAttempter (nil)
helpAnchor (nil)
ClassError
domain __lldb_expr_7.ClassError
code 1
userInfo [:]
localizedDescription The operation couldn’t be completed. (__lldb_expr_7.ClassError error 1.)
localizedFailureReason (nil)
localizedRecoverySuggestion (nil)
localizedRecoveryOptions (nil)
recoveryAttempter (nil)
helpAnchor (nil)
EnumError
domain __lldb_expr_7.EnumError
code 2
userInfo [:]
localizedDescription The operation couldn’t be completed. (__lldb_expr_7.EnumError error 2.)
localizedFailureReason (nil)
localizedRecoverySuggestion (nil)
localizedRecoveryOptions (nil)
recoveryAttempter (nil)
helpAnchor (nil)
StringEnumError
domain __lldb_expr_7.StringEnumError
code 2
userInfo [:]
localizedDescription The operation couldn’t be completed. (__lldb_expr_7.StringEnumError error 2.)
localizedFailureReason (nil)
localizedRecoverySuggestion (nil)
localizedRecoveryOptions (nil)
recoveryAttempter (nil)
helpAnchor (nil)
IntEnumError
domain __lldb_expr_7.IntEnumError
code 2
userInfo [:]
localizedDescription The operation couldn’t be completed. (__lldb_expr_7.IntEnumError error 2.)
localizedFailureReason (nil)
localizedRecoverySuggestion (nil)
localizedRecoveryOptions (nil)
recoveryAttempter (nil)
helpAnchor (nil)
StructLocalizedError
domain __lldb_expr_7.StructLocalizedError
code 1
userInfo [:]
localizedDescription StructLocalizedError.errorDescription
localizedFailureReason StructLocalizedError.failureReason
localizedRecoverySuggestion StructLocalizedError.recoverySuggestion
localizedRecoveryOptions (nil)
recoveryAttempter (nil)
helpAnchor StructLocalizedError.helpAnchor
StructRecoverableError
domain __lldb_expr_7.StructRecoverableError
code 1
userInfo [:]
localizedDescription The operation couldn’t be completed. (__lldb_expr_7.StructRecoverableError error 1.)
localizedFailureReason (nil)
localizedRecoverySuggestion (nil)
localizedRecoveryOptions ["StructRecoverableError.recoveryOptions.1", "StructRecoverableError.recoveryOptions.2"]
recoveryAttempter Foundation.__NSErrorRecoveryAttempter
helpAnchor (nil)
StructCustomNSError
domain StructCustomNSError.errorDomain
code 123
userInfo ["StructCustomNSError.UserInfo.Key2": 789, "StructCustomNSError.UserInfo.Key1": 456]
localizedDescription The operation couldn’t be completed. (StructCustomNSError.errorDomain error 123.)
localizedFailureReason (nil)
localizedRecoverySuggestion (nil)
localizedRecoveryOptions (nil)
recoveryAttempter (nil)
helpAnchor (nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment