Skip to content

Commit

Permalink
Correct (for #486) -localizedDescription to always return a string.
Browse files Browse the repository at this point in the history
  • Loading branch information
rfm committed Jan 13, 2025
1 parent afa5035 commit 479ccce
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions Source/NSError.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,24 @@ - (void) dealloc
[super dealloc];
}

- (NSString*) _fallback
{
return [NSString stringWithFormat: @"Error Domain=%@ Code=%lld",
[self domain], (long long)[self code]];
}

- (NSString*) description
{
NSMutableString *m = [NSMutableString stringWithCapacity: 200];
NSUInteger count = [_userInfo count];
NSString *loc = [self localizedDescription];
NSString *fallback = [self _fallback];

[m appendFormat: @"Error Domain=%@ Code=%lld \"%@\"",
[self domain], (long long)[self code], loc];
[m appendString: fallback];
if (NO == [fallback isEqual: loc])
{
[m appendFormat: @" \"%@\"", loc];
}

if ([loc isEqual: [_userInfo objectForKey: NSLocalizedDescriptionKey]])
{
Expand Down Expand Up @@ -206,7 +216,21 @@ - (id) initWithDomain: (NSErrorDomain)aDomain

- (NSString *) localizedDescription
{
return [_userInfo objectForKey: NSLocalizedDescriptionKey];
NSString *s = [_userInfo objectForKey: NSLocalizedDescriptionKey];

if (nil == s)
{
s = [_userInfo objectForKey: NSLocalizedFailureReasonErrorKey];
if (s)
{
s = [NSString stringWithFormat: @"Operation failed %@", s];
}
else
{
s = [self _fallback];
}
}
return s;
}

- (NSString *) localizedFailureReason
Expand Down

0 comments on commit 479ccce

Please sign in to comment.