mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-18 19:23:43 +01:00
Fixes adding failures to NSErrors with nil localizedFailureReasons
This commit is contained in:
@@ -24,6 +24,7 @@ extension String
|
|||||||
static let testUnrecognizedFailureReason = "The alien invasion has begun."
|
static let testUnrecognizedFailureReason = "The alien invasion has begun."
|
||||||
static let testUnrecognizedRecoverySuggestion = "Find your loved ones and pray the aliens are merciful."
|
static let testUnrecognizedRecoverySuggestion = "Find your loved ones and pray the aliens are merciful."
|
||||||
|
|
||||||
|
static let testDescription = "The operation could not be completed because an error occured."
|
||||||
static let testDebugDescription = "The very specific operation could not be completed because a detailed error occured. Code=101."
|
static let testDebugDescription = "The very specific operation could not be completed because a detailed error occured. Code=101."
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -473,6 +474,44 @@ extension AltTests
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testSwiftErrorWithLocalizedFailure() async throws
|
||||||
|
{
|
||||||
|
enum MyError: Int, LocalizedError, CaseIterable
|
||||||
|
{
|
||||||
|
case strange
|
||||||
|
case nothing
|
||||||
|
|
||||||
|
var errorDescription: String? {
|
||||||
|
switch self
|
||||||
|
{
|
||||||
|
case .strange: return "A strange error occured."
|
||||||
|
case .nothing: return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var recoverySuggestion: String? {
|
||||||
|
return "Have you tried turning it off and on again?"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for error in MyError.allCases
|
||||||
|
{
|
||||||
|
let nsError = (error as NSError).withLocalizedFailure(.testLocalizedFailure)
|
||||||
|
|
||||||
|
ALTAssertErrorsEqual(nsError, error, ignoring: [NSLocalizedDescriptionKey, NSLocalizedFailureErrorKey])
|
||||||
|
ALTAssertErrorFailureAndDescription(nsError, failure: .testLocalizedFailure, baseDescription: error.localizedDescription)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testNSErrorWithLocalizedFailure() async throws
|
||||||
|
{
|
||||||
|
let error = NSError(domain: .testDomain, code: 14, userInfo: [NSLocalizedDescriptionKey: String.testDescription])
|
||||||
|
let nsError = (error as NSError).withLocalizedFailure(.testLocalizedFailure)
|
||||||
|
|
||||||
|
ALTAssertErrorsEqual(nsError, error, ignoring: [NSLocalizedDescriptionKey, NSLocalizedFailureErrorKey])
|
||||||
|
ALTAssertErrorFailureAndDescription(nsError, failure: .testLocalizedFailure, baseDescription: .testDescription)
|
||||||
|
}
|
||||||
|
|
||||||
func testReceivingAltServerError() async throws
|
func testReceivingAltServerError() async throws
|
||||||
{
|
{
|
||||||
for error in ALTServerError.testErrors
|
for error in ALTServerError.testErrors
|
||||||
|
|||||||
@@ -35,12 +35,14 @@
|
|||||||
|
|
||||||
- (NSString *)localizedDescription
|
- (NSString *)localizedDescription
|
||||||
{
|
{
|
||||||
|
NSString *localizedFailureReason = self.wrappedError.localizedFailureReason ?: self.wrappedError.localizedDescription;
|
||||||
|
|
||||||
NSString *wrappedLocalizedDescription = self.wrappedError.userInfo[NSLocalizedDescriptionKey];
|
NSString *wrappedLocalizedDescription = self.wrappedError.userInfo[NSLocalizedDescriptionKey];
|
||||||
if (wrappedLocalizedDescription != nil)
|
if (wrappedLocalizedDescription != nil)
|
||||||
{
|
{
|
||||||
NSString *localizedFailure = self.wrappedError.userInfo[NSLocalizedFailureErrorKey];
|
NSString *localizedFailure = self.wrappedError.userInfo[NSLocalizedFailureErrorKey];
|
||||||
|
|
||||||
NSString *fallbackDescription = localizedFailure != nil ? [NSString stringWithFormat:@"%@ %@", localizedFailure, self.wrappedError.localizedFailureReason] : self.wrappedError.localizedFailureReason;
|
NSString *fallbackDescription = localizedFailure != nil ? [NSString stringWithFormat:@"%@ %@", localizedFailure, localizedFailureReason] : localizedFailureReason;
|
||||||
if (![wrappedLocalizedDescription isEqualToString:fallbackDescription])
|
if (![wrappedLocalizedDescription isEqualToString:fallbackDescription])
|
||||||
{
|
{
|
||||||
return wrappedLocalizedDescription;
|
return wrappedLocalizedDescription;
|
||||||
|
|||||||
Reference in New Issue
Block a user