pEpObjCAdapter/NSError+PEP.m
author Dirk Zimmermann <dz@pep.security>
Thu, 21 Mar 2019 15:02:02 +0100
branchIOS-1475_exp_dirk_001
changeset 905 929095fe2998
parent 880 dde239ea8241
parent 902 32760267cd59
permissions -rw-r--r--
IOS-1475 Merge in refactor.
dirk@483
     1
//
dirk@483
     2
//  NSError+PEP.m
dirk@483
     3
//  pEpObjCAdapter
dirk@483
     4
//
dirk@483
     5
//  Created by Dirk Zimmermann on 20.02.18.
dirk@483
     6
//  Copyright © 2018 p≡p. All rights reserved.
dirk@483
     7
//
dirk@483
     8
dz@902
     9
#import "PEPTypes.h"
dz@833
    10
dirk@483
    11
#import "NSError+PEP.h"
dz@831
    12
#import "NSError+PEP+Internal.h"
dz@831
    13
dirk@512
    14
static NSString *s_pEpAdapterDomain = @"security.pEp.ObjCAdapter";
dirk@511
    15
dirk@483
    16
@implementation NSError (Extension)
dirk@483
    17
dz@831
    18
+ (NSError * _Nonnull)errorWithPEPStatusInternal:(PEP_STATUS)status
dz@831
    19
                                        userInfo:(NSDictionary<NSErrorUserInfoKey, id> * _Nonnull)dict
dirk@483
    20
{
dirk@513
    21
    switch (status) {
dirk@513
    22
        case PEP_STATUS_OK:
dirk@513
    23
        case PEP_DECRYPTED:
dirk@513
    24
        case PEP_UNENCRYPTED:
dirk@513
    25
        case PEP_DECRYPT_NO_KEY:
dz@783
    26
        case PEP_KEY_IMPORT_STATUS_UNKNOWN:
dirk@513
    27
            return nil;
dirk@513
    28
            break;
dirk@513
    29
dirk@513
    30
        default:
dirk@513
    31
            if (![dict objectForKey:NSLocalizedDescriptionKey]) {
dirk@513
    32
                NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithDictionary:dict];
dirk@513
    33
                [dict2 setValue:localizedErrorStringFromPEPStatus(status)
dirk@513
    34
                         forKey:NSLocalizedDescriptionKey];
dirk@513
    35
                dict = dict2;
dirk@513
    36
            }
dirk@513
    37
            return [NSError errorWithDomain:s_pEpAdapterDomain code:status userInfo:dict];
dirk@513
    38
            break;
dirk@483
    39
    }
dirk@483
    40
}
dirk@483
    41
dz@831
    42
+ (NSError * _Nonnull)errorWithPEPStatusInternal:(PEP_STATUS)status
dirk@483
    43
{
dirk@512
    44
    NSDictionary *userInfo = [NSDictionary new];
dz@831
    45
    return [self errorWithPEPStatusInternal:status userInfo:userInfo];
dirk@483
    46
}
dirk@483
    47
dirk@508
    48
+ (BOOL)setError:(NSError * _Nullable * _Nullable)error fromPEPStatus:(PEP_STATUS)status
dirk@507
    49
{
dz@831
    50
    NSError *theError = [self errorWithPEPStatusInternal:status];
dirk@507
    51
    if (theError) {
dirk@507
    52
        if (error) {
dirk@507
    53
            *error = theError;
dirk@507
    54
        }
dirk@507
    55
        return YES;
dirk@507
    56
    } else {
dirk@507
    57
        if (error) {
dirk@507
    58
            *error = nil;
dirk@507
    59
        }
dirk@507
    60
        return NO;
dirk@507
    61
    }
dirk@507
    62
}
dirk@507
    63
dirk@512
    64
/**
dirk@512
    65
 Could in theory return a fully localized version of the underlying error.
dirk@512
    66
 */
dirk@512
    67
NSString * _Nonnull localizedErrorStringFromPEPStatus(PEP_STATUS status) {
dirk@512
    68
    return stringFromPEPStatus(status);
dirk@512
    69
}
dirk@512
    70
dirk@511
    71
NSString * _Nonnull stringFromPEPStatus(PEP_STATUS status) {
dirk@511
    72
    switch (status) {
dirk@511
    73
        case PEP_STATUS_OK: return @"PEP_STATUS_OK";
dirk@511
    74
        case PEP_INIT_CANNOT_LOAD_GPGME: return @"PEP_INIT_CANNOT_LOAD_GPGME";
dirk@511
    75
        case PEP_INIT_GPGME_INIT_FAILED: return @"PEP_INIT_GPGME_INIT_FAILED";
dirk@511
    76
        case PEP_INIT_NO_GPG_HOME: return @"PEP_INIT_NO_GPG_HOME";
dirk@511
    77
        case PEP_INIT_NETPGP_INIT_FAILED: return @"PEP_INIT_NETPGP_INIT_FAILED";
dirk@511
    78
        case PEP_INIT_CANNOT_DETERMINE_GPG_VERSION: return @"PEP_INIT_CANNOT_DETERMINE_GPG_VERSION";
dirk@511
    79
        case PEP_INIT_UNSUPPORTED_GPG_VERSION: return @"PEP_INIT_UNSUPPORTED_GPG_VERSION";
dirk@511
    80
        case PEP_INIT_CANNOT_CONFIG_GPG_AGENT: return @"PEP_INIT_CANNOT_CONFIG_GPG_AGENT";
dirk@511
    81
        case PEP_INIT_SQLITE3_WITHOUT_MUTEX: return @"PEP_INIT_SQLITE3_WITHOUT_MUTEX";
dirk@511
    82
        case PEP_INIT_CANNOT_OPEN_DB: return @"PEP_INIT_CANNOT_OPEN_DB";
dirk@511
    83
        case PEP_INIT_CANNOT_OPEN_SYSTEM_DB: return @"PEP_INIT_CANNOT_OPEN_SYSTEM_DB";
dirk@511
    84
        case PEP_KEY_NOT_FOUND: return @"PEP_KEY_NOT_FOUND";
dirk@511
    85
        case PEP_KEY_HAS_AMBIG_NAME: return @"PEP_KEY_HAS_AMBIG_NAME";
dirk@511
    86
        case PEP_GET_KEY_FAILED: return @"PEP_GET_KEY_FAILED";
dirk@511
    87
        case PEP_CANNOT_EXPORT_KEY: return @"PEP_CANNOT_EXPORT_KEY";
dirk@511
    88
        case PEP_CANNOT_EDIT_KEY: return @"PEP_CANNOT_EDIT_KEY";
dirk@511
    89
        case PEP_KEY_UNSUITABLE: return @"PEP_KEY_UNSUITABLE";
dirk@511
    90
        case PEP_CANNOT_FIND_IDENTITY: return @"PEP_CANNOT_FIND_IDENTITY";
dirk@511
    91
        case PEP_CANNOT_SET_PERSON: return @"PEP_CANNOT_SET_PERSON";
dirk@511
    92
        case PEP_CANNOT_SET_PGP_KEYPAIR: return @"PEP_CANNOT_SET_PGP_KEYPAIR";
dirk@511
    93
        case PEP_CANNOT_SET_IDENTITY: return @"PEP_CANNOT_SET_IDENTITY";
dirk@511
    94
        case PEP_CANNOT_SET_TRUST: return @"PEP_CANNOT_SET_TRUST";
dirk@511
    95
        case PEP_KEY_BLACKLISTED: return @"PEP_KEY_BLACKLISTED";
dirk@511
    96
        case PEP_CANNOT_FIND_PERSON: return @"PEP_CANNOT_FIND_PERSON";
dirk@511
    97
        case PEP_CANNOT_FIND_ALIAS: return @"PEP_CANNOT_FIND_ALIAS";
dirk@511
    98
        case PEP_CANNOT_SET_ALIAS: return @"PEP_CANNOT_SET_ALIAS";
dirk@511
    99
        case PEP_UNENCRYPTED: return @"PEP_UNENCRYPTED";
dirk@511
   100
        case PEP_VERIFIED: return @"PEP_VERIFIED";
dirk@511
   101
        case PEP_DECRYPTED: return @"PEP_DECRYPTED";
dirk@511
   102
        case PEP_DECRYPTED_AND_VERIFIED: return @"PEP_DECRYPTED_AND_VERIFIED";
dirk@511
   103
        case PEP_DECRYPT_WRONG_FORMAT: return @"PEP_DECRYPT_WRONG_FORMAT";
dirk@511
   104
        case PEP_DECRYPT_NO_KEY: return @"PEP_DECRYPT_NO_KEY";
dirk@511
   105
        case PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH: return @"PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH";
dirk@511
   106
        case PEP_VERIFY_NO_KEY: return @"PEP_VERIFY_NO_KEY";
dirk@511
   107
        case PEP_VERIFIED_AND_TRUSTED: return @"PEP_VERIFIED_AND_TRUSTED";
dirk@511
   108
        case PEP_CANNOT_DECRYPT_UNKNOWN: return @"PEP_CANNOT_DECRYPT_UNKNOWN";
dirk@511
   109
        case PEP_TRUSTWORD_NOT_FOUND: return @"PEP_TRUSTWORD_NOT_FOUND";
dirk@511
   110
        case PEP_TRUSTWORDS_FPR_WRONG_LENGTH: return @"PEP_TRUSTWORDS_FPR_WRONG_LENGTH";
dirk@511
   111
        case PEP_TRUSTWORDS_DUPLICATE_FPR: return @"PEP_TRUSTWORDS_DUPLICATE_FPR";
dirk@511
   112
        case PEP_CANNOT_CREATE_KEY: return @"PEP_CANNOT_CREATE_KEY";
dirk@511
   113
        case PEP_CANNOT_SEND_KEY: return @"PEP_CANNOT_SEND_KEY";
dirk@511
   114
        case PEP_PHRASE_NOT_FOUND: return @"PEP_PHRASE_NOT_FOUND";
dirk@511
   115
        case PEP_SEND_FUNCTION_NOT_REGISTERED: return @"PEP_SEND_FUNCTION_NOT_REGISTERED";
dirk@511
   116
        case PEP_CONTRAINTS_VIOLATED: return @"PEP_CONTRAINTS_VIOLATED";
dirk@511
   117
        case PEP_CANNOT_ENCODE: return @"PEP_CANNOT_ENCODE";
dirk@511
   118
        case PEP_SYNC_NO_NOTIFY_CALLBACK: return @"PEP_SYNC_NO_NOTIFY_CALLBACK";
dirk@511
   119
        case PEP_SYNC_ILLEGAL_MESSAGE: return @"PEP_SYNC_ILLEGAL_MESSAGE";
dirk@511
   120
        case PEP_SYNC_NO_INJECT_CALLBACK: return @"PEP_SYNC_NO_INJECT_CALLBACK";
dirk@511
   121
        case PEP_CANNOT_INCREASE_SEQUENCE: return @"PEP_CANNOT_INCREASE_SEQUENCE";
dirk@511
   122
        case PEP_STATEMACHINE_INVALID_STATE: return @"PEP_STATEMACHINE_INVALID_STATE";
dirk@511
   123
        case PEP_STATEMACHINE_INVALID_EVENT: return @"PEP_STATEMACHINE_INVALID_EVENT";
dirk@511
   124
        case PEP_STATEMACHINE_INVALID_CONDITION: return @"PEP_STATEMACHINE_INVALID_CONDITION";
dirk@511
   125
        case PEP_STATEMACHINE_INVALID_ACTION: return @"PEP_STATEMACHINE_INVALID_ACTION";
dirk@511
   126
        case PEP_STATEMACHINE_INHIBITED_EVENT: return @"PEP_STATEMACHINE_INHIBITED_EVENT";
dirk@511
   127
        case PEP_COMMIT_FAILED: return @"PEP_COMMIT_FAILED";
dirk@511
   128
        case PEP_MESSAGE_CONSUME: return @"PEP_MESSAGE_CONSUME";
dirk@511
   129
        case PEP_MESSAGE_IGNORE: return @"PEP_MESSAGE_IGNORE";
dirk@511
   130
        case PEP_RECORD_NOT_FOUND: return @"PEP_RECORD_NOT_FOUND";
dirk@511
   131
        case PEP_CANNOT_CREATE_TEMP_FILE: return @"PEP_CANNOT_CREATE_TEMP_FILE";
dirk@511
   132
        case PEP_ILLEGAL_VALUE: return @"PEP_ILLEGAL_VALUE";
dirk@511
   133
        case PEP_BUFFER_TOO_SMALL: return @"PEP_BUFFER_TOO_SMALL";
dirk@511
   134
        case PEP_OUT_OF_MEMORY: return @"PEP_OUT_OF_MEMORY";
dirk@511
   135
        case PEP_UNKNOWN_ERROR: return @"PEP_UNKNOWN_ERROR";
dirk@511
   136
        case PEP_VERSION_MISMATCH: return @"PEP_VERSION_MISMATCH";
dirk@603
   137
        case PEP_CANNOT_REENCRYPT: return @"PEP_CANNOT_REENCRYPT";
dz@779
   138
        case PEP_KEY_IMPORTED: return @"PEP_KEY_IMPORTED";
dz@779
   139
        case PEP_NO_KEY_IMPORTED: return @"PEP_NO_KEY_IMPORTED";
dz@779
   140
        case PEP_KEY_IMPORT_STATUS_UNKNOWN: return @"PEP_KEY_IMPORT_STATUS_UNKNOWN";
dz@880
   141
        case PEP_SEQUENCE_VIOLATED: return @"PEP_SEQUENCE_VIOLATED";
dz@880
   142
        case PEP_CANNOT_SET_SEQUENCE_VALUE: return @"PEP_CANNOT_SET_SEQUENCE_VALUE";
dz@880
   143
        case PEP_OWN_SEQUENCE: return @"PEP_OWN_SEQUENCE";
dz@880
   144
        case PEP_SYNC_STATEMACHINE_ERROR: return @"PEP_SYNC_STATEMACHINE_ERROR";
dz@880
   145
        case PEP_SYNC_NO_TRUST: return @"PEP_SYNC_NO_TRUST";
dirk@511
   146
    }
dirk@511
   147
}
dirk@511
   148
dirk@511
   149
- (NSString * _Nullable)pEpErrorString
dirk@511
   150
{
dirk@511
   151
    if ([self.domain isEqualToString:s_pEpAdapterDomain]) {
dirk@511
   152
        return stringFromPEPStatus((PEP_STATUS) self.code);
dirk@511
   153
    } else {
dirk@511
   154
        return nil;
dirk@511
   155
    }
dirk@511
   156
}
dirk@511
   157
dz@831
   158
+ (NSError * _Nonnull)errorWithPEPStatus:(PEPStatus)status
dz@831
   159
                                userInfo:(NSDictionary<NSErrorUserInfoKey, id> * _Nonnull)dict
dz@831
   160
{
dz@833
   161
    return [self errorWithPEPStatusInternal:(PEP_STATUS) status userInfo:dict];
dz@831
   162
}
dz@831
   163
dz@831
   164
+ (NSError * _Nonnull)errorWithPEPStatus:(PEPStatus)status
dz@831
   165
{
dz@840
   166
    return [self errorWithPEPStatusInternal:(PEP_STATUS) status userInfo:[NSDictionary new]];
dz@831
   167
}
dz@831
   168
dirk@483
   169
@end