API  0.9.6
 All Classes Files Functions Variables 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 
43 @implementation CPException : CPObject
44 {
45  id _userInfo;
46 }
47 
48 /*
49  @ignore
50 */
51 + (id)alloc
52 {
53  var result = new Error();
54  result.isa = [self class];
55  return result;
56 }
57 
63 + (void)raise:(CPString)aName reason:(CPString)aReason
64 {
65  [[self exceptionWithName:aName reason:aReason userInfo:nil] raise];
66 }
67 
75 + (CPException)exceptionWithName:(CPString)aName reason:(CPString)aReason userInfo:(CPDictionary)aUserInfo
76 {
77  return [[self alloc] initWithName:aName reason:aReason userInfo:aUserInfo];
78 }
79 
87 - (id)initWithName:(CPString)aName reason:(CPString)aReason userInfo:(CPDictionary)aUserInfo
88 {
89  self = [super init];
90 
91  if (self)
92  {
93  name = aName;
94  message = aReason;
95  _userInfo = aUserInfo;
96  }
97 
98  return self;
99 }
100 
104 - (CPString)name
105 {
106  return name;
107 }
108 
112 - (CPString)reason
113 {
114  return message;
115 }
116 
120 - (CPDictionary)userInfo
121 {
122  return _userInfo;
123 }
124 
128 - (CPString)description
129 {
130  return message;
131 }
132 
136 - (void)raise
137 {
138  throw self;
139 }
140 
141 - (BOOL)isEqual:(id)anObject
142 {
143  if (!anObject || !anObject.isa)
144  return NO;
145 
146  return [anObject isKindOfClass:CPException] &&
147  name === [anObject name] &&
148  message === [anObject message] &&
149  (_userInfo === [anObject userInfo] || ([_userInfo isEqual:[anObject userInfo]]));
150 }
151 
152 @end
153 
154 @implementation CPException (CPCopying)
155 
156 - (id)copy
157 {
158  return [[self class] exceptionWithName:name reason:message userInfo:_userInfo];
159 }
160 
161 @end
162 
163 var CPExceptionNameKey = @"CPExceptionNameKey",
164  CPExceptionReasonKey = @"CPExceptionReasonKey",
165  CPExceptionUserInfoKey = @"CPExceptionUserInfoKey";
166 
167 @implementation CPException (CPCoding)
168 
174 - (id)initWithCoder:(CPCoder)aCoder
175 {
176  if (self = [super init])
177  {
178  name = [aCoder decodeObjectForKey:CPExceptionNameKey];
179  message = [aCoder decodeObjectForKey:CPExceptionReasonKey];
180  _userInfo = [aCoder decodeObjectForKey:CPExceptionUserInfoKey];
181  }
182 
183  return self;
184 }
185 
190 - (void)encodeWithCoder:(CPCoder)aCoder
191 {
192  [aCoder encodeObject:name forKey:CPExceptionNameKey];
193  [aCoder encodeObject:message forKey:CPExceptionReasonKey];
194  [aCoder encodeObject:_userInfo forKey:CPExceptionUserInfoKey];
195 }
196 
197 @end
198 
199 // toll-free bridge Error to CPException
200 // [CPException alloc] uses an objj_exception, which is a subclass of Error
201 Error.prototype.isa = CPException;
202 Error.prototype._userInfo = null;
203 
205 
206 #define METHOD_CALL_STRING()\
207  ((class_isMetaClass(anObject.isa) ? "+" : "-") + "[" + [anObject className] + " " + aSelector + "]: ")
208 
209 function _CPRaiseInvalidAbstractInvocation(anObject, aSelector)
210 {
211  [CPException raise:CPInvalidArgumentException reason:@"*** -" + sel_getName(aSelector) + @" cannot be sent to an abstract object of class " + [anObject className] + @": Create a concrete instance!"];
212 }
213 
214 function _CPRaiseInvalidArgumentException(anObject, aSelector, aMessage)
215 {
216  [CPException raise:CPInvalidArgumentException
217  reason:METHOD_CALL_STRING() + aMessage];
218 }
219 
220 function _CPRaiseRangeException(anObject, aSelector, anIndex, aCount)
221 {
222  [CPException raise:CPRangeException
223  reason:METHOD_CALL_STRING() + "index (" + anIndex + ") beyond bounds (" + aCount + ")"];
224 }
225 
226 function _CPReportLenientDeprecation(/*Class*/ aClass, /*SEL*/ oldSelector, /*SEL*/ newSelector)
227 {
228  CPLog.warn("[" + CPStringFromClass(aClass) + " " + CPStringFromSelector(oldSelector) + "] is deprecated, using " + CPStringFromSelector(newSelector) + " instead.");
229 }