<commit_before>/*
 * CPError.j
 * Foundation
 *
 * Created by <NAME>.
 * Copyright 2013, SlevenBits Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

@import "CPDictionary.j"
@import "CPObject.j"
@import "CPString.j"

CPCappuccinoErrorDomain = CPCocoaErrorDomain = @"CPCappuccinoErrorDomain";
// CPPOSIXErrorDomain      = @"CPPOSIXErrorDomain";
// CPOSStatusErrorDomain   = @"CPOSStatusErrorDomain";

CPUnderlyingErrorKey                    = @"CPUnderlyingErrorKey";

CPLocalizedDescriptionKey               = @"CPLocalizedDescriptionKey";
CPLocalizedFailureReasonErrorKey        = @"CPLocalizedFailureReasonErrorKey";
CPLocalizedRecoverySuggestionErrorKey   = @"CPLocalizedRecoverySuggestionErrorKey";
CPLocalizedRecoveryOptionsErrorKey      = @"CPLocalizedRecoveryOptionsErrorKey";
CPRecoveryAttempterErrorKey             = @"CPRecoveryAttempterErrorKey";
CPHelpAnchorErrorKey                    = @"CPHelpAnchorErrorKey";

CPStringEncodingErrorKey                = @"CPStringEncodingErrorKey";
CPURLErrorKey                           = @"CPURLErrorKey";
CPFilePathErrorKey                      = @"CPFilePathErrorKey";


@implementation CPError : CPObject
{
    CPInteger       _code @accessors(property=code, readonly);
    CPString        _domain @accessors(property=domain, readonly);
    CPDictionary    _userInfo @accessors(property=userInfo, readonly);
}

+ (id)errorWithDomain:(CPString)aDomain code:(CPInteger)aCode userInfo:(CPDictionary)aDict
{
    return [[CPError alloc] initWithDomain:aDomain code:aCode userInfo:aDict];
}

- (id)initWithDomain:(CPString)aDomain code:(CPInteger)aCode userInfo:(CPDictionary)aDict
{
    if (self = [super init])
    {
        _domain = aDomain;
        _code = aCode;
        _userInfo = aDict;
    }

    return self;
}

- (CPString)localizedDescription
{
   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];
}

- (CPString)description
{
   return [CPString stringWithFormat:@"Error Domain=%@ Code=%d UserInfo=%p %@", _domain, _code, _userInfo, [self localizedDescription]];
}

@end<commit_msg>New: Toll-free bridged CPError with CFError<commit_after>/*
 * CPError.j
 * Foundation
 *
 * Created by <NAME>.
 * Copyright 2013, SlevenBits Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

@import "CPDictionary.j"
@import "CPObject.j"
@import "CPString.j"

CPCappuccinoErrorDomain                 = kCFErrorDomainCappuccino;
CPCocoaErrorDomain                      = kCFErrorDomainCappuccino; // compat

CPUnderlyingErrorKey                    = kCFErrorUnderlyingErrorKey;

CPLocalizedDescriptionKey               = kCFErrorLocalizedDescriptionKey;
CPLocalizedFailureReasonErrorKey        = kCFErrorLocalizedFailureReasonKey;
CPLocalizedRecoverySuggestionErrorKey   = kCFErrorLocalizedRecoverySuggestionKey;
CPLocalizedRecoveryOptionsErrorKey      = @"CPLocalizedRecoveryOptionsErrorKey";
CPRecoveryAttempterErrorKey             = @"CPRecoveryAttempterErrorKey";
CPHelpAnchorErrorKey                    = @"CPHelpAnchorErrorKey";

CPStringEncodingErrorKey                = @"CPStringEncodingErrorKey";
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
{
}

+ (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];
}

- (id)initWithDomain:(CPString)aDomain code:(CPInteger)aCode userInfo:(CPDictionary)aDict
{
    var result = new CFError(aDomain, aCode, aDict);
    result.isa = [self class];
    return result;
}

- (CPInteger)code
{
    return self.code();
}

- (CPString)description
{
    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;

{'fraction_non_alphanumeric': 0.09130490459725957, 'fraction_numerical': 0.004610065309258548, 'mean_word_length': 3.996801023672425, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '726444', 'n_tokens_mistral': 2357, 'n_tokens_neox': 2290, 'n_words': 563}