From 815556ddf99bbaf83510cc17c53beddb81f478b0 Mon Sep 17 00:00:00 2001 From: rfm Date: Tue, 13 Feb 2024 12:41:34 +0000 Subject: [PATCH] OSX compatibility fix --- ChangeLog | 5 ++++ Source/NSError.m | 64 ++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1ee6f64b97..104b60a962 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2024-02-13 Richard Frith-Macdonald + + * Source/NSError.m: Make -description and -localizedDescription formats + match those used by OSX (14.3.1). + 2024-02-12 Richard Frith-Macdonald * Source/NSException.m: Prefer objc_set_unexpected() to the older diff --git a/Source/NSError.m b/Source/NSError.m index b3b23f711e..f9e681bf3c 100644 --- a/Source/NSError.m +++ b/Source/NSError.m @@ -27,6 +27,9 @@ #import "Foundation/NSDictionary.h" #import "Foundation/NSError.h" #import "Foundation/NSCoder.h" +#import "Foundation/NSArray.h" + +#import "GSFastEnumeration.h" @implementation NSError @@ -62,7 +65,58 @@ - (void) dealloc - (NSString*) description { - return [self localizedDescription]; + NSMutableString *m = [NSMutableString stringWithCapacity: 200]; + NSUInteger count = [_userInfo count]; + NSString *loc = [self localizedDescription]; + + [m appendFormat: @"Error Domain=%@ Code=%lld \"%@\"", + [self domain], (long long)[self code], loc]; + + if ([loc isEqual: [_userInfo objectForKey: NSLocalizedDescriptionKey]]) + { + count--; // Don't repeat this information + } + + if (count > 0) + { + NSArray *keys = [_userInfo allKeys]; + BOOL first = YES; + + keys = [keys sortedArrayUsingSelector: @selector(compare:)]; + [m appendString: @" UserInfo={"]; + FOR_IN (NSString*, k, keys) + { + id o = [_userInfo objectForKey: k]; + + if ([k isEqualToString: NSLocalizedDescriptionKey]) + { + continue; + } + + if (first) + { + first = NO; + } + else + { + [m appendString: @", "]; + } + [m appendString: k]; + [m appendString: @"="]; + if ([k isEqualToString: NSUnderlyingErrorKey]) + { + [m appendFormat: @"%p {%@}", o, [o description]]; + } + else + { + [m appendString: [o description]]; + } + } + END_FOR_IN (enumerator) + + [m appendString: @"}"]; + } + return m; } - (NSErrorDomain) domain @@ -134,13 +188,7 @@ - (id) initWithDomain: (NSErrorDomain)aDomain - (NSString *) localizedDescription { - NSString *desc = [_userInfo objectForKey: NSLocalizedDescriptionKey]; - - if (desc == nil) - { - desc = [NSString stringWithFormat: @"%@ %d", _domain, _code]; - } - return desc; + return [_userInfo objectForKey: NSLocalizedDescriptionKey]; } - (NSString *) localizedFailureReason