diff --git a/ChangeLog b/ChangeLog index 1ee6f64b9..104b60a96 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 b3b23f711..f9e681bf3 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