clean-checkpoint-1

This commit is contained in:
Magesh K
2024-12-07 17:45:09 +05:30
parent e27c5f0b87
commit 63a3203e50
95 changed files with 1040 additions and 3761 deletions

View File

@@ -30,6 +30,7 @@ extension CodableError
case codableError(CodableError)
indirect case array([UserInfoValue])
indirect case dictionary([String: UserInfoValue])
var value: Any? {
switch self
{
@@ -42,6 +43,7 @@ extension CodableError
case .dictionary(let dictionary): return dictionary.compactMapValues { $0.value } // .compactMapValues instead of .mapValues to ensure nil values are removed.
}
}
var codableValue: Codable? {
switch self
{
@@ -52,10 +54,12 @@ extension CodableError
let sanitizedError = nsError.sanitizedForSerialization()
let data = try? NSKeyedArchiver.archivedData(withRootObject: sanitizedError, requiringSecureCoding: true)
return data
case .array(let array): return array
case .dictionary(let dictionary): return dictionary
}
}
init(_ rawValue: Any?)
{
switch rawValue
@@ -69,6 +73,7 @@ extension CodableError
default: self = .unknown(rawValue)
}
}
init(from decoder: Decoder) throws
{
let container = try decoder.singleValueContainer()
@@ -104,11 +109,11 @@ extension CodableError
self = .unknown(nil)
}
}
func encode(to encoder: Encoder) throws
{
var container = encoder.singleValueContainer()
if let value = self.codableValue
{
try container.encode(value)
@@ -127,11 +132,11 @@ struct CodableError: Codable
return self.rawError ?? NSError(domain: self.errorDomain, code: self.errorCode, userInfo: self.userInfo ?? [:])
}
private var rawError: Error?
private var errorDomain: String
private var errorCode: Int
private var userInfo: [String: Any]?
private enum CodingKeys: String, CodingKey
{
case errorDomain
@@ -143,58 +148,63 @@ struct CodableError: Codable
init(error: Error)
{
self.rawError = error
let nsError = error as NSError
self.errorDomain = nsError.domain
self.errorCode = nsError.code
if !nsError.userInfo.isEmpty
{
self.userInfo = nsError.userInfo
}
}
init(from decoder: Decoder) throws
{
let container = try decoder.container(keyedBy: CodingKeys.self)
// Assume ALTServerError.errorDomain if no explicit domain provided.
self.errorDomain = try container.decodeIfPresent(String.self, forKey: .errorDomain) ?? ALTServerError.errorDomain
self.errorCode = try container.decode(Int.self, forKey: .errorCode)
if let rawUserInfo = try container.decodeIfPresent([String: UserInfoValue].self, forKey: .errorUserInfo)
{
// Attempt decoding from .errorUserInfo first, because it will gracefully handle unknown user info values.
// Copy ALTLocalized... values to NSLocalized... if provider is nil or if error is unrecognized.
// This ensures we preserve error messages if receiving an unknown error.
var userInfo = rawUserInfo.compactMapValues { $0.value }
// Recognized == the provider returns value for NSLocalizedFailureReasonErrorKey, or error is ALTServerError.underlyingError.
let provider = NSError.userInfoValueProvider(forDomain: self.errorDomain)
let isRecognizedError = (
provider?(self.error, NSLocalizedFailureReasonErrorKey) != nil ||
(self.error._domain == ALTServerError.errorDomain && self.error._code == ALTServerError.underlyingError.rawValue)
)
if !isRecognizedError
{
// Error not recognized, so copy over NSLocalizedDescriptionKey and NSLocalizedFailureReasonErrorKey.
userInfo[NSLocalizedDescriptionKey] = userInfo[ErrorUserInfoKey.altLocalizedDescription]
userInfo[NSLocalizedFailureReasonErrorKey] = userInfo[ErrorUserInfoKey.altLocalizedFailureReason]
}
// Copy over NSLocalizedRecoverySuggestionErrorKey and NSDebugDescriptionErrorKey if provider returns nil.
if provider?(self.error, NSLocalizedRecoverySuggestionErrorKey) == nil
{
userInfo[NSLocalizedRecoverySuggestionErrorKey] = userInfo[ErrorUserInfoKey.altLocalizedRecoverySuggestion]
}
if provider?(self.error, NSDebugDescriptionErrorKey) == nil
{
userInfo[NSDebugDescriptionErrorKey] = userInfo[ErrorUserInfoKey.altDebugDescription]
}
userInfo[ErrorUserInfoKey.altLocalizedDescription] = nil
userInfo[ErrorUserInfoKey.altLocalizedFailureReason] = nil
userInfo[ErrorUserInfoKey.altLocalizedRecoverySuggestion] = nil
userInfo[ErrorUserInfoKey.altDebugDescription] = nil
self.userInfo = userInfo
}
else if let rawUserInfo = try container.decodeIfPresent([String: UserInfoValue].self, forKey: .legacyUserInfo)
@@ -204,11 +214,13 @@ struct CodableError: Codable
self.userInfo = userInfo
}
}
func encode(to encoder: Encoder) throws
{
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.errorDomain, forKey: .errorDomain)
try container.encode(self.errorCode, forKey: .errorCode)
let rawLegacyUserInfo = self.userInfo?.compactMapValues { (value) -> UserInfoValue? in
// .legacyUserInfo only supports String and NSError values.
switch value
@@ -219,19 +231,19 @@ struct CodableError: Codable
}
}
try container.encodeIfPresent(rawLegacyUserInfo, forKey: .legacyUserInfo)
let nsError = self.error as NSError
var userInfo = self.userInfo ?? [:]
userInfo[ErrorUserInfoKey.altLocalizedDescription] = nsError.localizedDescription
userInfo[ErrorUserInfoKey.altLocalizedFailureReason] = nsError.localizedFailureReason
userInfo[ErrorUserInfoKey.altLocalizedRecoverySuggestion] = nsError.localizedRecoverySuggestion
userInfo[ErrorUserInfoKey.altDebugDescription] = nsError.localizedDebugDescription
// No need to use alternate key. This is a no-op if userInfo already contains localizedFailure,
// but it caches the UserInfoProvider value if one exists.
userInfo[NSLocalizedFailureErrorKey] = nsError.localizedFailure
let rawUserInfo = userInfo.compactMapValues { UserInfoValue($0) }
try container.encodeIfPresent(rawUserInfo, forKey: .errorUserInfo)
}

View File

@@ -201,6 +201,7 @@ public struct ErrorResponse: ServerMessageProtocol
public var identifier = "ErrorResponse"
public var error: ALTServerError {
// Must be ALTServerError
return self.serverError.map { ALTServerError($0.error) } ?? ALTServerError(self.errorCode)
}
private var serverError: CodableError?