2004-05-17 09:24:29 +00:00
|
|
|
/** Implementation for NSError for GNUStep
|
|
|
|
Copyright (C) 2004 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
|
|
|
Date: May 2004
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2004-05-17 09:24:29 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2004-05-17 09:24:29 +00:00
|
|
|
This library is free software; you can redistribute it and/or
|
2007-09-14 11:36:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
2004-05-17 09:24:29 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
version 2 of the License, or (at your option) any later version.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2004-05-17 09:24:29 +00:00
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2019-12-09 23:36:00 +00:00
|
|
|
Lesser General Public License for more details.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2004-05-17 09:24:29 +00:00
|
|
|
License along with this library; if not, write to the Free
|
2024-11-07 13:37:59 +00:00
|
|
|
Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
|
2005-02-22 11:22:44 +00:00
|
|
|
*/
|
2004-05-17 09:24:29 +00:00
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
#import "common.h"
|
2010-02-14 10:48:10 +00:00
|
|
|
#define EXPOSE_NSError_IVARS 1
|
|
|
|
#import "Foundation/NSDictionary.h"
|
2025-01-03 16:05:09 +00:00
|
|
|
#import "Foundation/NSException.h"
|
2010-02-14 10:48:10 +00:00
|
|
|
#import "Foundation/NSError.h"
|
|
|
|
#import "Foundation/NSCoder.h"
|
2024-02-13 12:41:34 +00:00
|
|
|
#import "Foundation/NSArray.h"
|
|
|
|
|
|
|
|
#import "GSFastEnumeration.h"
|
2004-05-17 09:24:29 +00:00
|
|
|
|
|
|
|
@implementation NSError
|
|
|
|
|
2025-01-03 16:05:09 +00:00
|
|
|
/* For NSFileManager we have a private method which produces an error
|
|
|
|
* with mutable userInfo so that information can be added before the
|
|
|
|
* file manager returns the error to higher level code.
|
|
|
|
*/
|
|
|
|
+ (NSError*) _error: (NSInteger)aCode
|
|
|
|
description: (NSString*)description
|
|
|
|
{
|
|
|
|
NSError *e = [self allocWithZone: NSDefaultMallocZone()];
|
|
|
|
NSMutableDictionary *m;
|
|
|
|
|
|
|
|
e = [e initWithDomain: NSCocoaErrorDomain code: aCode userInfo: nil];
|
|
|
|
m = [NSMutableDictionary allocWithZone: NSDefaultMallocZone()];
|
|
|
|
e->_userInfo = [m initWithCapacity: 3];
|
|
|
|
[m setObject: description forKey: NSLocalizedDescriptionKey];
|
|
|
|
return AUTORELEASE(e);
|
|
|
|
}
|
|
|
|
|
2020-03-26 13:16:44 +00:00
|
|
|
+ (id) errorWithDomain: (NSErrorDomain)aDomain
|
2009-02-23 20:42:32 +00:00
|
|
|
code: (NSInteger)aCode
|
2004-05-17 09:24:29 +00:00
|
|
|
userInfo: (NSDictionary*)aDictionary
|
|
|
|
{
|
2025-01-03 16:05:09 +00:00
|
|
|
|
2004-05-17 09:24:29 +00:00
|
|
|
NSError *e = [self allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
|
|
|
e = [e initWithDomain: aDomain code: aCode userInfo: aDictionary];
|
|
|
|
return AUTORELEASE(e);
|
|
|
|
}
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
- (NSInteger) code
|
2004-05-17 09:24:29 +00:00
|
|
|
{
|
|
|
|
return _code;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
{
|
|
|
|
NSError *e = [[self class] allocWithZone: z];
|
|
|
|
|
|
|
|
e = [e initWithDomain: _domain code: _code userInfo: _userInfo];
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
DESTROY(_domain);
|
|
|
|
DESTROY(_userInfo);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2025-01-13 15:31:09 +00:00
|
|
|
- (NSString*) _fallback
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat: @"Error Domain=%@ Code=%lld",
|
|
|
|
[self domain], (long long)[self code]];
|
|
|
|
}
|
|
|
|
|
2006-10-20 10:56:27 +00:00
|
|
|
- (NSString*) description
|
|
|
|
{
|
2024-02-13 12:41:34 +00:00
|
|
|
NSMutableString *m = [NSMutableString stringWithCapacity: 200];
|
|
|
|
NSUInteger count = [_userInfo count];
|
|
|
|
NSString *loc = [self localizedDescription];
|
2025-01-13 15:31:09 +00:00
|
|
|
NSString *fallback = [self _fallback];
|
2024-02-13 12:41:34 +00:00
|
|
|
|
2025-01-13 15:31:09 +00:00
|
|
|
[m appendString: fallback];
|
|
|
|
if (NO == [fallback isEqual: loc])
|
|
|
|
{
|
|
|
|
[m appendFormat: @" \"%@\"", loc];
|
|
|
|
}
|
2024-02-13 12:41:34 +00:00
|
|
|
|
|
|
|
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={"];
|
2024-02-13 12:59:12 +00:00
|
|
|
FOR_IN(NSString*, k, keys)
|
2024-02-13 12:41:34 +00:00
|
|
|
{
|
|
|
|
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]];
|
|
|
|
}
|
|
|
|
}
|
2024-02-13 12:59:12 +00:00
|
|
|
END_FOR_IN(keys)
|
2024-02-13 12:41:34 +00:00
|
|
|
|
|
|
|
[m appendString: @"}"];
|
|
|
|
}
|
|
|
|
return m;
|
2006-10-20 10:56:27 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 13:16:44 +00:00
|
|
|
- (NSErrorDomain) domain
|
2004-05-17 09:24:29 +00:00
|
|
|
{
|
|
|
|
return _domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
|
|
|
if ([aCoder allowsKeyedCoding])
|
|
|
|
{
|
|
|
|
[aCoder encodeInt: _code forKey: @"NSCode"];
|
|
|
|
[aCoder encodeObject: _domain forKey: @"NSDomain"];
|
|
|
|
[aCoder encodeObject: _userInfo forKey: @"NSUserInfo"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(int) at: &_code];
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(id) at: &_domain];
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(id) at: &_userInfo];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
return [self initWithDomain: nil code: 0 userInfo: nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
|
|
|
if ([aCoder allowsKeyedCoding])
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
id d;
|
|
|
|
id u;
|
|
|
|
|
|
|
|
c = [aCoder decodeIntForKey: @"NSCode"];
|
|
|
|
d = [aCoder decodeObjectForKey: @"NSDomain"];
|
|
|
|
u = [aCoder decodeObjectForKey: @"NSUserInfo"];
|
|
|
|
self = [self initWithDomain: d code: c userInfo: u];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[aCoder decodeValueOfObjCType: @encode(int) at: &_code];
|
|
|
|
[aCoder decodeValueOfObjCType: @encode(id) at: &_domain];
|
|
|
|
[aCoder decodeValueOfObjCType: @encode(id) at: &_userInfo];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-03-26 13:16:44 +00:00
|
|
|
- (id) initWithDomain: (NSErrorDomain)aDomain
|
2009-02-23 20:42:32 +00:00
|
|
|
code: (NSInteger)aCode
|
2004-05-17 09:24:29 +00:00
|
|
|
userInfo: (NSDictionary*)aDictionary
|
|
|
|
{
|
|
|
|
if (aDomain == nil)
|
|
|
|
{
|
|
|
|
NSLog(@"[%@-%@] with nil domain",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd));
|
|
|
|
DESTROY(self);
|
|
|
|
}
|
|
|
|
else if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
ASSIGN(_domain, aDomain);
|
|
|
|
_code = aCode;
|
|
|
|
ASSIGN(_userInfo, aDictionary);
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2006-10-20 10:56:27 +00:00
|
|
|
- (NSString *) localizedDescription
|
2004-05-17 09:24:29 +00:00
|
|
|
{
|
2025-01-13 15:31:09 +00:00
|
|
|
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;
|
2004-05-17 09:24:29 +00:00
|
|
|
}
|
|
|
|
|
2006-10-20 10:56:27 +00:00
|
|
|
- (NSString *) localizedFailureReason
|
|
|
|
{
|
|
|
|
return [_userInfo objectForKey: NSLocalizedFailureReasonErrorKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) localizedRecoveryOptions
|
|
|
|
{
|
|
|
|
return [_userInfo objectForKey: NSLocalizedRecoveryOptionsErrorKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) localizedRecoverySuggestion
|
|
|
|
{
|
|
|
|
return [_userInfo objectForKey: NSLocalizedRecoverySuggestionErrorKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) recoveryAttempter
|
|
|
|
{
|
|
|
|
return [_userInfo objectForKey: NSRecoveryAttempterErrorKey];
|
|
|
|
}
|
|
|
|
|
2004-05-17 09:24:29 +00:00
|
|
|
- (NSDictionary*) userInfo
|
|
|
|
{
|
|
|
|
return _userInfo;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|