mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-17 04:00:42 +00:00
New: Toll-free bridged CPError with CFError
This commit changes CPError to use a toll-free bridge with CFError, matching Cocoa's implementation of this class.
This commit is contained in:
1 parent
0038eab6ab
commit
96135c9875
1 file changed
+109
-43
+109
-43
@@ -24,31 +24,52 @@
|
||||
@import "CPObject.j"
|
||||
@import "CPString.j"
|
||||
|
||||
CPCappuccinoErrorDomain = CPCocoaErrorDomain = @"CPCappuccinoErrorDomain";
|
||||
// CPPOSIXErrorDomain = @"CPPOSIXErrorDomain";
|
||||
// CPOSStatusErrorDomain = @"CPOSStatusErrorDomain";
|
||||
CPCappuccinoErrorDomain = kCFErrorDomainCappuccino;
|
||||
CPCocoaErrorDomain = kCFErrorDomainCappuccino; // compat
|
||||
|
||||
CPUnderlyingErrorKey = @"CPUnderlyingErrorKey";
|
||||
CPUnderlyingErrorKey = kCFErrorUnderlyingErrorKey;
|
||||
|
||||
CPLocalizedDescriptionKey = @"CPLocalizedDescriptionKey";
|
||||
CPLocalizedFailureReasonErrorKey = @"CPLocalizedFailureReasonErrorKey";
|
||||
CPLocalizedRecoverySuggestionErrorKey = @"CPLocalizedRecoverySuggestionErrorKey";
|
||||
CPLocalizedDescriptionKey = kCFErrorLocalizedDescriptionKey;
|
||||
CPLocalizedFailureReasonErrorKey = kCFErrorLocalizedFailureReasonKey;
|
||||
CPLocalizedRecoverySuggestionErrorKey = kCFErrorLocalizedRecoverySuggestionKey;
|
||||
CPLocalizedRecoveryOptionsErrorKey = @"CPLocalizedRecoveryOptionsErrorKey";
|
||||
CPRecoveryAttempterErrorKey = @"CPRecoveryAttempterErrorKey";
|
||||
CPHelpAnchorErrorKey = @"CPHelpAnchorErrorKey";
|
||||
|
||||
CPStringEncodingErrorKey = @"CPStringEncodingErrorKey";
|
||||
CPURLErrorKey = @"CPURLErrorKey";
|
||||
CPFilePathErrorKey = @"CPFilePathErrorKey";
|
||||
CPURLErrorKey = kCFErrorURLKey;
|
||||
CPFilePathErrorKey = kCFErrorFilePathKey;
|
||||
|
||||
/*!
|
||||
@class CPError
|
||||
@ingroup foundation
|
||||
@brief Used for encapsulating, presenting, and recovery from errors.
|
||||
|
||||
CPError is toll-free bridged with CFError() methods.
|
||||
|
||||
An example of initializing a CPError:
|
||||
<pre>
|
||||
|
||||
var userInfo = @{CPLocalizedDescriptionKey: @"A localized error description",
|
||||
CPLocalizedFailureReasonErrorKey: @"A localized failure reason",
|
||||
CPUnderlyingErrorKey: @"An underlying error message"},
|
||||
|
||||
err = [CPError errorWithDomain:CPCappuccinoErrorDomain code:-10 userInfo:userInfo];
|
||||
</pre>
|
||||
*/
|
||||
@implementation CPError : CPObject
|
||||
{
|
||||
CPInteger _code @accessors(property=code, readonly);
|
||||
CPString _domain @accessors(property=domain, readonly);
|
||||
CPDictionary _userInfo @accessors(property=userInfo, readonly);
|
||||
}
|
||||
|
||||
+ (id)alloc
|
||||
{
|
||||
var obj = new CFError();
|
||||
obj.isa = [self class];
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
|
||||
+ (id)errorWithDomain:(CPString)aDomain code:(CPInteger)aCode userInfo:(CPDictionary)aDict
|
||||
{
|
||||
return [[CPError alloc] initWithDomain:aDomain code:aCode userInfo:aDict];
|
||||
@@ -56,44 +77,89 @@ CPFilePathErrorKey = @"CPFilePathErrorKey";
|
||||
|
||||
- (id)initWithDomain:(CPString)aDomain code:(CPInteger)aCode userInfo:(CPDictionary)aDict
|
||||
{
|
||||
if (self = [super init])
|
||||
{
|
||||
_domain = aDomain;
|
||||
_code = aCode;
|
||||
_userInfo = aDict;
|
||||
}
|
||||
|
||||
return self;
|
||||
var result = new CFError(aDomain, aCode, aDict);
|
||||
result.isa = [self class];
|
||||
return result;
|
||||
}
|
||||
|
||||
- (CPString)localizedDescription
|
||||
- (CPInteger)code
|
||||
{
|
||||
return [_userInfo objectForKey:CPLocalizedDescriptionKey];
|
||||
}
|
||||
|
||||
- (CPString)localizedFailureReason
|
||||
{
|
||||
return [_userInfo objectForKey:CPLocalizedFailureReasonErrorKey];
|
||||
}
|
||||
|
||||
- (CPArray)localizedRecoveryOptions
|
||||
{
|
||||
return [_userInfo objectForKey:CPLocalizedRecoveryOptionsErrorKey];
|
||||
}
|
||||
|
||||
- (CPString)localizedRecoverySuggestion
|
||||
{
|
||||
return [_userInfo objectForKey:CPLocalizedRecoverySuggestionErrorKey];
|
||||
}
|
||||
|
||||
- (id)recoveryAttempter
|
||||
{
|
||||
return [_userInfo objectForKey:CPRecoveryAttempterErrorKey];
|
||||
return self.code();
|
||||
}
|
||||
|
||||
- (CPString)description
|
||||
{
|
||||
return [CPString stringWithFormat:@"Error Domain=%@ Code=%d UserInfo=%p %@", _domain, _code, _userInfo, [self localizedDescription]];
|
||||
return self.description();
|
||||
}
|
||||
|
||||
- (CPString)domain
|
||||
{
|
||||
return self.domain();
|
||||
}
|
||||
|
||||
- (CPString)localizedDescription
|
||||
{
|
||||
return self.description();
|
||||
}
|
||||
|
||||
- (CPString)localizedFailureReason
|
||||
{
|
||||
return self.failureReason();
|
||||
}
|
||||
|
||||
- (CPArray)localizedRecoveryOptions
|
||||
{
|
||||
var userInfo = self.userInfo(),
|
||||
recoveryOptions = userInfo.valueForKey(CPLocalizedRecoveryOptionsErrorKey);
|
||||
|
||||
return recoveryOptions;
|
||||
}
|
||||
|
||||
- (CPString)localizedRecoverySuggestion
|
||||
{
|
||||
return self.recoverySuggestion();
|
||||
}
|
||||
|
||||
- (id)recoveryAttempter
|
||||
{
|
||||
var userInfo = self.userInfo(),
|
||||
recoveryAttempter = userInfo.valueForKey(CPRecoveryAttempterErrorKey);
|
||||
|
||||
return recoveryAttempter;
|
||||
}
|
||||
|
||||
- (CPString)description
|
||||
{
|
||||
return [CPString stringWithFormat:@"Error Domain=%@ Code=%d \"%@\" UserInfo=%@", self.domain(), self.code(), self.description(), self.userInfo()];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
var CPErrorCodeKey = @"CPErrorCodeKey",
|
||||
CPErrorDomainKey = @"CPErrorDomainKey",
|
||||
CPErrorUserInfoKey = @"CPErrorUserInfoKey";
|
||||
|
||||
@implementation CPError (CPCoding)
|
||||
|
||||
- (id)initWithCoder:(CPCoder)aCoder
|
||||
{
|
||||
var code = [aCoder decodeIntForKey:CPErrorCodeKey],
|
||||
domain = [aCoder decodeObjectForKey:CPErrorDomainKey],
|
||||
userInfo = [aCoder decodeObjectForKey:CPErrorUserInfoKey];
|
||||
|
||||
return [self initWithDomain:domain
|
||||
code:code
|
||||
userInfo:userInfo];
|
||||
}
|
||||
|
||||
- (void)encodeWithCoder:(CPCoder)aCoder
|
||||
{
|
||||
[aCoder encodeObject:self.domain() forKey:CPErrorDomainKey];
|
||||
[aCoder encodeObject:self.code() forKey:CPErrorCodeKey];
|
||||
[aCoder encodeObject:self.userInfo() forKey:CPErrorUserInfoKey];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
CFError.prototype.isa = CPError;
|
||||
|
||||
Reference in new issue
Block a user