-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSError.m
More file actions
171 lines (143 loc) · 4.01 KB
/
NSError.m
File metadata and controls
171 lines (143 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/** Implementation for NSError for GNUStep
Copyright (C) 2004 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <rfm@gnu.org>
Date: May 2004
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
*/
#import "common.h"
#define EXPOSE_NSError_IVARS 1
#import "Foundation/NSDictionary.h"
#import "Foundation/NSError.h"
#import "Foundation/NSCoder.h"
@implementation NSError
+ (id) errorWithDomain: (NSErrorDomain)aDomain
code: (NSInteger)aCode
userInfo: (NSDictionary*)aDictionary
{
NSError *e = [self allocWithZone: NSDefaultMallocZone()];
e = [e initWithDomain: aDomain code: aCode userInfo: aDictionary];
return AUTORELEASE(e);
}
- (NSInteger) code
{
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];
}
- (NSString*) description
{
return [self localizedDescription];
}
- (NSErrorDomain) domain
{
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;
}
- (id) initWithDomain: (NSErrorDomain)aDomain
code: (NSInteger)aCode
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;
}
- (NSString *) localizedDescription
{
NSString *desc = [_userInfo objectForKey: NSLocalizedDescriptionKey];
if (desc == nil)
{
desc = [NSString stringWithFormat: @"%@ %d", _domain, _code];
}
return desc;
}
- (NSString *) localizedFailureReason
{
return [_userInfo objectForKey: NSLocalizedFailureReasonErrorKey];
}
- (NSArray *) localizedRecoveryOptions
{
return [_userInfo objectForKey: NSLocalizedRecoveryOptionsErrorKey];
}
- (NSString *) localizedRecoverySuggestion
{
return [_userInfo objectForKey: NSLocalizedRecoverySuggestionErrorKey];
}
- (id) recoveryAttempter
{
return [_userInfo objectForKey: NSRecoveryAttempterErrorKey];
}
- (NSDictionary*) userInfo
{
return _userInfo;
}
@end