API  0.9.10
CPException.j
Go to the documentation of this file.
1 /*
2  * CPException.j
3  * Foundation
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 
25 CPInvalidArgumentException = @"CPInvalidArgumentException";
26 CPUnsupportedMethodException = @"CPUnsupportedMethodException";
27 CPRangeException = @"CPRangeException";
28 CPInternalInconsistencyException = @"CPInternalInconsistencyException";
29 CPGenericException = @"CPGenericException";
30 
44 @implementation CPException : CPObject
45 {
46  id _userInfo;
49 }
50 
51 /*
52  @ignore
53 */
54 + (id)alloc
55 {
56  var result = new Error();
57  result.isa = [self class];
58  return result;
59 }
60 
66 + (void)raise:(CPString)aName reason:(CPString)aReason
67 {
68  [[self exceptionWithName:aName reason:aReason userInfo:nil] raise];
69 }
70 
77 + (void)raise:(CPString)aName format:(CPString)aFormat, ...
78 {
79  if (!aFormat)
80  [CPException raise:CPInvalidArgumentException
81  reason:"raise:format: the format can't be 'nil'"];
82 
83  var aReason = ObjectiveJ.sprintf.apply(this, Array.prototype.slice.call(arguments, 3));
84  [[self exceptionWithName:aName reason:aReason userInfo:nil] raise];
85 }
86 
94 + (CPException)exceptionWithName:(CPString)aName reason:(CPString)aReason userInfo:(CPDictionary)aUserInfo
95 {
96  return [[self alloc] initWithName:aName reason:aReason userInfo:aUserInfo];
97 }
98 
106 - (id)initWithName:(CPString)aName reason:(CPString)aReason userInfo:(CPDictionary)aUserInfo
107 {
108  self = [super init];
109 
110  if (self)
111  {
112  name = aName;
113  message = aReason;
114  _userInfo = aUserInfo;
115  }
116 
117  return self;
118 }
119 
123 - (CPString)name
124 {
125  return name;
126 }
127 
132 {
133  return message;
134 }
135 
140 {
141  return _userInfo;
142 }
143 
148 {
149  return message;
150 }
151 
155 - (void)raise
156 {
157  throw self;
158 }
159 
160 - (BOOL)isEqual:(id)anObject
161 {
162  if (!anObject || !anObject.isa)
163  return NO;
164 
165  return [anObject isKindOfClass:CPException] &&
166  name === [anObject name] &&
167  message === [anObject message] &&
168  (_userInfo === [anObject userInfo] || ([_userInfo isEqual:[anObject userInfo]]));
169 }
170 
171 @end
172 
173 @implementation CPException (CPCopying)
174 
175 - (id)copy
176 {
177  return [[self class] exceptionWithName:name reason:message userInfo:_userInfo];
178 }
179 
180 @end
181 
182 var CPExceptionNameKey = @"CPExceptionNameKey",
183  CPExceptionReasonKey = @"CPExceptionReasonKey",
184  CPExceptionUserInfoKey = @"CPExceptionUserInfoKey";
185 
186 @implementation CPException (CPCoding)
187 
193 - (id)initWithCoder:(CPCoder)aCoder
194 {
195  if (self = [super init])
196  {
197  name = [aCoder decodeObjectForKey:CPExceptionNameKey];
198  message = [aCoder decodeObjectForKey:CPExceptionReasonKey];
199  _userInfo = [aCoder decodeObjectForKey:CPExceptionUserInfoKey];
200  }
201 
202  return self;
203 }
204 
209 - (void)encodeWithCoder:(CPCoder)aCoder
210 {
211  [aCoder encodeObject:name forKey:CPExceptionNameKey];
212  [aCoder encodeObject:message forKey:CPExceptionReasonKey];
213  [aCoder encodeObject:_userInfo forKey:CPExceptionUserInfoKey];
214 }
215 
216 @end
217 
218 // toll-free bridge Error to CPException
219 // [CPException alloc] uses an objj_exception, which is a subclass of Error
220 Error.prototype.isa = CPException;
221 Error.prototype._userInfo = null;
222 
224 
225 #define METHOD_CALL_STRING()\
226  ((class_isMetaClass(anObject.isa) ? "+" : "-") + "[" + [anObject className] + " " + aSelector + "]: ")
227 
228 function _CPRaiseInvalidAbstractInvocation(anObject, aSelector)
229 {
230  [CPException raise:CPInvalidArgumentException reason:@"*** -" + sel_getName(aSelector) + @" cannot be sent to an abstract object of class " + [anObject className] + @": Create a concrete instance!"];
231 }
232 
233 function _CPRaiseInvalidArgumentException(anObject, aSelector, aMessage)
234 {
235  [CPException raise:CPInvalidArgumentException
236  reason:METHOD_CALL_STRING() + aMessage];
237 }
238 
239 function _CPRaiseRangeException(anObject, aSelector, anIndex, aCount)
240 {
241  [CPException raise:CPRangeException
242  reason:METHOD_CALL_STRING() + "index (" + anIndex + ") beyond bounds (" + aCount + ")"];
243 }
244 
245 function _CPReportLenientDeprecation(/*Class*/ aClass, /*SEL*/ oldSelector, /*SEL*/ newSelector)
246 {
247  CPLog.warn("[" + CPStringFromClass(aClass) + " " + CPStringFromSelector(oldSelector) + "] is deprecated, using " + CPStringFromSelector(newSelector) + " instead.");
248 }
Used to implement exception handling (creating & raising).
Definition: CPException.h:2
var CPExceptionNameKey
Definition: CPException.j:182
function CPStringFromClass(aClass)
Definition: CPObjJRuntime.j:38
CPGenericException
Definition: CPException.j:29
var isEqual
var CPExceptionUserInfoKey
Definition: CPException.j:184
void raise:reason:(CPString aName, [reason] CPString aReason)
Definition: CPException.j:66
void initialize()
Definition: CPObject.j:113
CPString name
Definition: CPException.h:5
CPInvalidArgumentException
Definition: CPException.j:25
A mutable key-value pair collection.
Definition: CPDictionary.h:2
CPString message
Definition: CPException.h:6
var CPExceptionReasonKey
Definition: CPException.j:183
CPDictionary userInfo()
Definition: CPException.j:139
An immutable string (collection of characters).
Definition: CPString.h:2
CPString reason()
Definition: CPException.j:131
CPString description()
Definition: CPException.j:147
CPRangeException
Definition: CPException.j:27
CPInternalInconsistencyException
Definition: CPException.j:28
CPUnsupportedMethodException
Definition: CPException.j:26
Defines methods for use when archiving & restoring (enc/decoding).
Definition: CPCoder.h:2
id init()
Definition: CPObject.j:145
id initWithName:reason:userInfo:(CPString aName, [reason] CPString aReason, [userInfo] CPDictionary aUserInfo)
Definition: CPException.j:106
function CPStringFromSelector(aSelector)
Definition: CPObjJRuntime.j:23
Class class()
Definition: CPObject.j:179
CPException exceptionWithName:reason:userInfo:(CPString aName, [reason] CPString aReason, [userInfo] CPDictionary aUserInfo)
Definition: CPException.j:94