API  0.9.8
 All Classes Files Functions Variables Typedefs Macros Groups Pages
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 CPInvalidArgumentException = @"CPInvalidArgumentException";
25 CPUnsupportedMethodException = @"CPUnsupportedMethodException";
26 CPRangeException = @"CPRangeException";
27 CPInternalInconsistencyException = @"CPInternalInconsistencyException";
28 CPGenericException = @"CPGenericException";
29 
43 @implementation CPException : CPObject
44 {
45  id _userInfo;
48 }
49 
50 /*
51  @ignore
52 */
53 + (id)alloc
54 {
55  var result = new Error();
56  result.isa = [self class];
57  return result;
58 }
59 
65 + (void)raise:(CPString)aName reason:(CPString)aReason
66 {
67  [[self exceptionWithName:aName reason:aReason userInfo:nil] raise];
68 }
69 
76 + (void)raise:(CPString)aName format:(CPString)aFormat, ...
77 {
78  if (!aFormat)
79  [CPException raise:CPInvalidArgumentException
80  reason:"raise:format: the format can't be 'nil'"];
81 
82  var aReason = ObjectiveJ.sprintf.apply(this, Array.prototype.slice.call(arguments, 3));
83  [[self exceptionWithName:aName reason:aReason userInfo:nil] raise];
84 }
85 
93 + (CPException)exceptionWithName:(CPString)aName reason:(CPString)aReason userInfo:(CPDictionary)aUserInfo
94 {
95  return [[self alloc] initWithName:aName reason:aReason userInfo:aUserInfo];
96 }
97 
105 - (id)initWithName:(CPString)aName reason:(CPString)aReason userInfo:(CPDictionary)aUserInfo
106 {
107  self = [super init];
108 
109  if (self)
110  {
111  name = aName;
112  message = aReason;
113  _userInfo = aUserInfo;
114  }
115 
116  return self;
117 }
118 
123 {
124  return name;
125 }
126 
130 - (CPString)reason
131 {
132  return message;
133 }
134 
138 - (CPDictionary)userInfo
139 {
140  return _userInfo;
141 }
142 
146 - (CPString)description
147 {
148  return message;
149 }
150 
154 - (void)raise
155 {
156  throw self;
157 }
158 
159 - (BOOL)isEqual:(id)anObject
160 {
161  if (!anObject || !anObject.isa)
162  return NO;
163 
164  return [anObject isKindOfClass:CPException] &&
165  name === [anObject name] &&
166  message === [anObject message] &&
167  (_userInfo === [anObject userInfo] || ([_userInfo isEqual:[anObject userInfo]]));
168 }
169 
170 @end
171 
172 @implementation CPException (CPCopying)
173 
174 - (id)copy
175 {
176  return [[self class] exceptionWithName:name reason:message userInfo:_userInfo];
177 }
178 
179 @end
180 
181 var CPExceptionNameKey = @"CPExceptionNameKey",
182  CPExceptionReasonKey = @"CPExceptionReasonKey",
183  CPExceptionUserInfoKey = @"CPExceptionUserInfoKey";
184 
185 @implementation CPException (CPCoding)
186 
192 - (id)initWithCoder:(CPCoder)aCoder
193 {
194  if (self = [super init])
195  {
196  name = [aCoder decodeObjectForKey:CPExceptionNameKey];
197  message = [aCoder decodeObjectForKey:CPExceptionReasonKey];
198  _userInfo = [aCoder decodeObjectForKey:CPExceptionUserInfoKey];
199  }
200 
201  return self;
202 }
203 
208 - (void)encodeWithCoder:(CPCoder)aCoder
209 {
210  [aCoder encodeObject:name forKey:CPExceptionNameKey];
211  [aCoder encodeObject:message forKey:CPExceptionReasonKey];
212  [aCoder encodeObject:_userInfo forKey:CPExceptionUserInfoKey];
213 }
214 
215 @end
216 
217 // toll-free bridge Error to CPException
218 // [CPException alloc] uses an objj_exception, which is a subclass of Error
219 Error.prototype.isa = CPException;
220 Error.prototype._userInfo = null;
221 
223 
224 #define METHOD_CALL_STRING()\
225  ((class_isMetaClass(anObject.isa) ? "+" : "-") + "[" + [anObject className] + " " + aSelector + "]: ")
226 
227 function _CPRaiseInvalidAbstractInvocation(anObject, aSelector)
228 {
229  [CPException raise:CPInvalidArgumentException reason:@"*** -" + sel_getName(aSelector) + @" cannot be sent to an abstract object of class " + [anObject className] + @": Create a concrete instance!"];
230 }
231 
232 function _CPRaiseInvalidArgumentException(anObject, aSelector, aMessage)
233 {
234  [CPException raise:CPInvalidArgumentException
235  reason:METHOD_CALL_STRING() + aMessage];
236 }
237 
238 function _CPRaiseRangeException(anObject, aSelector, anIndex, aCount)
239 {
240  [CPException raise:CPRangeException
241  reason:METHOD_CALL_STRING() + "index (" + anIndex + ") beyond bounds (" + aCount + ")"];
242 }
243 
244 function _CPReportLenientDeprecation(/*Class*/ aClass, /*SEL*/ oldSelector, /*SEL*/ newSelector)
245 {
246  CPLog.warn("[" + CPStringFromClass(aClass) + " " + CPStringFromSelector(oldSelector) + "] is deprecated, using " + CPStringFromSelector(newSelector) + " instead.");
247 }