Skip to content

Commit

Permalink
OSX compatibility fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rfm committed Feb 13, 2024
1 parent 98a8e6c commit 815556d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2024-02-13 Richard Frith-Macdonald <[email protected]>

* Source/NSError.m: Make -description and -localizedDescription formats
match those used by OSX (14.3.1).

2024-02-12 Richard Frith-Macdonald <[email protected]>

* Source/NSException.m: Prefer objc_set_unexpected() to the older
Expand Down
64 changes: 56 additions & 8 deletions Source/NSError.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

1 comment on commit 815556d

@triplef
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s helpful, thank you for this!

Please sign in to comment.