API 0.9.5
Foundation/CPException.j
Go to the documentation of this file.
00001 /*
00002  * CPException.j
00003  * Foundation
00004  *
00005  * Created by Francisco Tolmasky.
00006  * Copyright 2008, 280 North, Inc.
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 
00024 CPInvalidArgumentException          = @"CPInvalidArgumentException";
00025 CPUnsupportedMethodException        = @"CPUnsupportedMethodException";
00026 CPRangeException                    = @"CPRangeException";
00027 CPInternalInconsistencyException    = @"CPInternalInconsistencyException";
00028 
00043 @implementation CPException : CPObject
00044 {
00045     id          _userInfo;
00046 }
00047 
00048 /*
00049     @ignore
00050 */
00051 + (id)alloc
00052 {
00053     var result = new Error();
00054     result.isa = [self class];
00055     return result;
00056 }
00057 
00063 + (void)raise:(CPString)aName reason:(CPString)aReason
00064 {
00065     [[self exceptionWithName:aName reason:aReason userInfo:nil] raise];
00066 }
00067 
00075 + (CPException)exceptionWithName:(CPString)aName reason:(CPString)aReason userInfo:(CPDictionary)aUserInfo
00076 {
00077     return [[self alloc] initWithName:aName reason:aReason userInfo:aUserInfo];
00078 }
00079 
00087 - (id)initWithName:(CPString)aName reason:(CPString)aReason userInfo:(CPDictionary)aUserInfo
00088 {
00089     self = [super init];
00090 
00091     if (self)
00092     {
00093         name = aName;
00094         message = aReason;
00095         _userInfo = aUserInfo;
00096     }
00097 
00098     return self;
00099 }
00100 
00104 - (CPString)name
00105 {
00106     return name;
00107 }
00108 
00112 - (CPString)reason
00113 {
00114     return message;
00115 }
00116 
00120 - (CPDictionary)userInfo
00121 {
00122     return _userInfo;
00123 }
00124 
00128 - (CPString)description
00129 {
00130     return message;
00131 }
00132 
00136 - (void)raise
00137 {
00138     throw self;
00139 }
00140 
00141 - (BOOL)isEqual:(id)anObject
00142 {
00143     if (!anObject || !anObject.isa)
00144         return NO;
00145 
00146     return [anObject isKindOfClass:CPException] &&
00147            name === [anObject name] &&
00148            message === [anObject message] &&
00149            (_userInfo === [anObject userInfo] || ([_userInfo isEqual:[anObject userInfo]]));
00150 }
00151 
00152 @end
00153 
00154 @implementation CPException (CPCopying)
00155 
00156 - (id)copy
00157 {
00158     return [[self class] exceptionWithName:name reason:message userInfo:_userInfo];
00159 }
00160 
00161 @end
00162 
00163 var CPExceptionNameKey      = @"CPExceptionNameKey",
00164     CPExceptionReasonKey    = @"CPExceptionReasonKey",
00165     CPExceptionUserInfoKey  = @"CPExceptionUserInfoKey";
00166 
00167 @implementation CPException (CPCoding)
00168 
00174 - (id)initWithCoder:(CPCoder)aCoder
00175 {
00176     if (self = [super init])
00177     {
00178         name = [aCoder decodeObjectForKey:CPExceptionNameKey];
00179         message = [aCoder decodeObjectForKey:CPExceptionReasonKey];
00180         _userInfo = [aCoder decodeObjectForKey:CPExceptionUserInfoKey];
00181     }
00182 
00183     return self;
00184 }
00185 
00190 - (void)encodeWithCoder:(CPCoder)aCoder
00191 {
00192     [aCoder encodeObject:name forKey:CPExceptionNameKey];
00193     [aCoder encodeObject:message forKey:CPExceptionReasonKey];
00194     [aCoder encodeObject:_userInfo forKey:CPExceptionUserInfoKey];
00195 }
00196 
00197 @end
00198 
00199 // toll-free bridge Error to CPException
00200 // [CPException alloc] uses an objj_exception, which is a subclass of Error
00201 Error.prototype.isa = CPException;
00202 Error.prototype._userInfo = null;
00203 
00204 [CPException initialize];
00205 
00206 #define METHOD_CALL_STRING()\
00207     ((class_isMetaClass(anObject.isa) ? "+" : "-") + "[" + [anObject className] + " " + aSelector + "]: ")
00208 
00209 function _CPRaiseInvalidAbstractInvocation(anObject, aSelector)
00210 {
00211     [CPException raise:CPInvalidArgumentException reason:@"*** -" + sel_getName(aSelector) + @" cannot be sent to an abstract object of class " + [anObject className] + @": Create a concrete instance!"];
00212 }
00213 
00214 function _CPRaiseInvalidArgumentException(anObject, aSelector, aMessage)
00215 {
00216     [CPException raise:CPInvalidArgumentException
00217                 reason:METHOD_CALL_STRING() + aMessage];
00218 }
00219 
00220 function _CPRaiseRangeException(anObject, aSelector, anIndex, aCount)
00221 {
00222     [CPException raise:CPRangeException
00223                 reason:METHOD_CALL_STRING() + "index (" + anIndex + ") beyond bounds (" + aCount + ")"];
00224 }
00225 
00226 function _CPReportLenientDeprecation(/*Class*/ aClass, /*SEL*/ oldSelector, /*SEL*/ newSelector)
00227 {
00228     CPLog.warn("[" + CPStringFromClass(aClass) + " " + CPStringFromSelector(oldSelector) + "] is deprecated, using " + CPStringFromSelector(newSelector) + " instead.");
00229 }
 All Classes Files Functions Variables Defines