721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott/*
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott * The contents of this file are subject to the terms of the Common Development and
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott * Distribution License (the License). You may not use this file except in compliance with the
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott * License.
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott *
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott * specific language governing permission and limitations under the License.
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott *
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott * When distributing Covered Software, include this CDDL Header Notice in each file and include
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott * Header, with the fields enclosed by brackets [] replaced by your own identifying
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott * information: "Portions copyright [year] [name of copyright owner]".
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott *
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott * Copyright 2016 ForgeRock AS.
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott */
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott#import "FRAError.h"
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott#import "FMDatabase.h"
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott@implementation FRAError
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshottstatic NSString * const FRAErrorDomain = @"ForgeRockErrorDomain";
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni+ (NSError *)createErrorForLastFailure:(FMDatabase *) database {
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni NSString* description = ([database.lastErrorMessage length] > 0) ? database.lastErrorMessage : @"nil";
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni int code = (database) ? database.lastErrorCode : -1;
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni NSLog(@"Database error: Last:%@ Code:%d", description, code);
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni return [self createErrorWithCode:code userInfo:@{ NSLocalizedDescriptionKey : description }];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott}
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni+ (NSError *)createErrorForFilePath:(NSString *)path reason:(NSString *)reason {
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni NSLog(@"File error: Reason:%@ Path:%@", path, reason);
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni return [self createErrorWithCode:FRAFileError userInfo:@{ NSLocalizedDescriptionKey : reason, NSFilePathErrorKey : path }];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni}
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni+ (NSError *)createError:(NSString *)reason {
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni return [self createError:reason code:FRAApplicationError];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni}
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni+ (NSError *)createError:(NSString *)reason code:(enum FRAErrorCodes)code {
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni return [self createErrorWithCode:code userInfo:@{ NSLocalizedDescriptionKey : reason }];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott}
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni+ (NSError *)createError:(NSString *)reason code:(enum FRAErrorCodes)code userInfo:(NSDictionary *)userInfo {
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni NSMutableDictionary *info = [userInfo mutableCopy];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni [info setObject:reason forKey:NSLocalizedDescriptionKey];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni return [self createErrorWithCode:code userInfo:info];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott}
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni+ (NSError *)createError:(NSString *)reason code:(enum FRAErrorCodes)code underlyingError:(NSError *)underlyingError {
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni if (underlyingError) {
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni NSDictionary *userInfo = @{NSLocalizedDescriptionKey : reason, NSUnderlyingErrorKey : underlyingError};
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni return [self createErrorWithCode:code userInfo:userInfo];
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni }
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni return [self createError:reason code:code];
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni}
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott+ (NSException *)createIllegalStateException:(NSString *)reason {
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott return [NSException exceptionWithName:@"IllegalStateException" reason:reason userInfo:nil];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott}
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni+ (NSError *)createErrorWithCode:(int)code userInfo:(NSDictionary *)errorDictionary {
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni return [[NSError alloc] initWithDomain:FRAErrorDomain code:code userInfo:errorDictionary];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott}
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott@end