pEpObjCAdapter/NSError+PEP.m
author Dirk Zimmermann <dirk@pep-project.org>
Mon, 23 Apr 2018 19:22:51 +0200
changeset 582 768d3beffeb8
parent 513 f729b071c451
child 585 635f5fc68671
permissions -rw-r--r--
IOSAD-92 NSError+PEP: PEP_CANNOT_REENCRYPT
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
dirk@483
     9
#import "NSError+PEP.h"
dirk@483
    10
dirk@512
    11
static NSString *s_pEpAdapterDomain = @"security.pEp.ObjCAdapter";
dirk@511
    12
dirk@483
    13
@implementation NSError (Extension)
dirk@483
    14
dirk@511
    15
+ (NSError * _Nonnull)errorWithPEPStatus:(PEP_STATUS)status
dirk@512
    16
                                userInfo:(NSDictionary<NSErrorUserInfoKey, id> * _Nonnull)dict
dirk@483
    17
{
dirk@513
    18
    switch (status) {
dirk@513
    19
        case PEP_STATUS_OK:
dirk@513
    20
        case PEP_DECRYPTED:
dirk@513
    21
        case PEP_UNENCRYPTED:
dirk@513
    22
        case PEP_DECRYPT_NO_KEY:
dirk@513
    23
            return nil;
dirk@513
    24
            break;
dirk@513
    25
dirk@513
    26
        default:
dirk@513
    27
            if (![dict objectForKey:NSLocalizedDescriptionKey]) {
dirk@513
    28
                NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithDictionary:dict];
dirk@513
    29
                [dict2 setValue:localizedErrorStringFromPEPStatus(status)
dirk@513
    30
                         forKey:NSLocalizedDescriptionKey];
dirk@513
    31
                dict = dict2;
dirk@513
    32
            }
dirk@513
    33
            return [NSError errorWithDomain:s_pEpAdapterDomain code:status userInfo:dict];
dirk@513
    34
            break;
dirk@483
    35
    }
dirk@483
    36
}
dirk@483
    37
dirk@511
    38
+ (NSError * _Nonnull)errorWithPEPStatus:(PEP_STATUS)status
dirk@483
    39
{
dirk@512
    40
    NSDictionary *userInfo = [NSDictionary new];
dirk@512
    41
    return [self errorWithPEPStatus:status userInfo:userInfo];
dirk@483
    42
}
dirk@483
    43
dirk@508
    44
+ (BOOL)setError:(NSError * _Nullable * _Nullable)error fromPEPStatus:(PEP_STATUS)status
dirk@507
    45
{
dirk@507
    46
    NSError *theError = [self errorWithPEPStatus:status];
dirk@507
    47
    if (theError) {
dirk@507
    48
        if (error) {
dirk@507
    49
            *error = theError;
dirk@507
    50
        }
dirk@507
    51
        return YES;
dirk@507
    52
    } else {
dirk@507
    53
        if (error) {
dirk@507
    54
            *error = nil;
dirk@507
    55
        }
dirk@507
    56
        return NO;
dirk@507
    57
    }
dirk@507
    58
}
dirk@507
    59
dirk@512
    60
/**
dirk@512
    61
 Could in theory return a fully localized version of the underlying error.
dirk@512
    62
 */
dirk@512
    63
NSString * _Nonnull localizedErrorStringFromPEPStatus(PEP_STATUS status) {
dirk@512
    64
    return stringFromPEPStatus(status);
dirk@512
    65
}
dirk@512
    66
dirk@511
    67
NSString * _Nonnull stringFromPEPStatus(PEP_STATUS status) {
dirk@511
    68
    switch (status) {
dirk@511
    69
        case PEP_STATUS_OK: return @"PEP_STATUS_OK";
dirk@511
    70
        case PEP_INIT_CANNOT_LOAD_GPGME: return @"PEP_INIT_CANNOT_LOAD_GPGME";
dirk@511
    71
        case PEP_INIT_GPGME_INIT_FAILED: return @"PEP_INIT_GPGME_INIT_FAILED";
dirk@511
    72
        case PEP_INIT_NO_GPG_HOME: return @"PEP_INIT_NO_GPG_HOME";
dirk@511
    73
        case PEP_INIT_NETPGP_INIT_FAILED: return @"PEP_INIT_NETPGP_INIT_FAILED";
dirk@511
    74
        case PEP_INIT_CANNOT_DETERMINE_GPG_VERSION: return @"PEP_INIT_CANNOT_DETERMINE_GPG_VERSION";
dirk@511
    75
        case PEP_INIT_UNSUPPORTED_GPG_VERSION: return @"PEP_INIT_UNSUPPORTED_GPG_VERSION";
dirk@511
    76
        case PEP_INIT_CANNOT_CONFIG_GPG_AGENT: return @"PEP_INIT_CANNOT_CONFIG_GPG_AGENT";
dirk@511
    77
        case PEP_INIT_SQLITE3_WITHOUT_MUTEX: return @"PEP_INIT_SQLITE3_WITHOUT_MUTEX";
dirk@511
    78
        case PEP_INIT_CANNOT_OPEN_DB: return @"PEP_INIT_CANNOT_OPEN_DB";
dirk@511
    79
        case PEP_INIT_CANNOT_OPEN_SYSTEM_DB: return @"PEP_INIT_CANNOT_OPEN_SYSTEM_DB";
dirk@511
    80
        case PEP_KEY_NOT_FOUND: return @"PEP_KEY_NOT_FOUND";
dirk@511
    81
        case PEP_KEY_HAS_AMBIG_NAME: return @"PEP_KEY_HAS_AMBIG_NAME";
dirk@511
    82
        case PEP_GET_KEY_FAILED: return @"PEP_GET_KEY_FAILED";
dirk@511
    83
        case PEP_CANNOT_EXPORT_KEY: return @"PEP_CANNOT_EXPORT_KEY";
dirk@511
    84
        case PEP_CANNOT_EDIT_KEY: return @"PEP_CANNOT_EDIT_KEY";
dirk@511
    85
        case PEP_KEY_UNSUITABLE: return @"PEP_KEY_UNSUITABLE";
dirk@511
    86
        case PEP_CANNOT_FIND_IDENTITY: return @"PEP_CANNOT_FIND_IDENTITY";
dirk@511
    87
        case PEP_CANNOT_SET_PERSON: return @"PEP_CANNOT_SET_PERSON";
dirk@511
    88
        case PEP_CANNOT_SET_PGP_KEYPAIR: return @"PEP_CANNOT_SET_PGP_KEYPAIR";
dirk@511
    89
        case PEP_CANNOT_SET_IDENTITY: return @"PEP_CANNOT_SET_IDENTITY";
dirk@511
    90
        case PEP_CANNOT_SET_TRUST: return @"PEP_CANNOT_SET_TRUST";
dirk@511
    91
        case PEP_KEY_BLACKLISTED: return @"PEP_KEY_BLACKLISTED";
dirk@511
    92
        case PEP_CANNOT_FIND_PERSON: return @"PEP_CANNOT_FIND_PERSON";
dirk@511
    93
        case PEP_CANNOT_FIND_ALIAS: return @"PEP_CANNOT_FIND_ALIAS";
dirk@511
    94
        case PEP_CANNOT_SET_ALIAS: return @"PEP_CANNOT_SET_ALIAS";
dirk@511
    95
        case PEP_UNENCRYPTED: return @"PEP_UNENCRYPTED";
dirk@511
    96
        case PEP_VERIFIED: return @"PEP_VERIFIED";
dirk@511
    97
        case PEP_DECRYPTED: return @"PEP_DECRYPTED";
dirk@511
    98
        case PEP_DECRYPTED_AND_VERIFIED: return @"PEP_DECRYPTED_AND_VERIFIED";
dirk@511
    99
        case PEP_DECRYPT_WRONG_FORMAT: return @"PEP_DECRYPT_WRONG_FORMAT";
dirk@511
   100
        case PEP_DECRYPT_NO_KEY: return @"PEP_DECRYPT_NO_KEY";
dirk@511
   101
        case PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH: return @"PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH";
dirk@511
   102
        case PEP_VERIFY_NO_KEY: return @"PEP_VERIFY_NO_KEY";
dirk@511
   103
        case PEP_VERIFIED_AND_TRUSTED: return @"PEP_VERIFIED_AND_TRUSTED";
dirk@511
   104
        case PEP_CANNOT_DECRYPT_UNKNOWN: return @"PEP_CANNOT_DECRYPT_UNKNOWN";
dirk@511
   105
        case PEP_TRUSTWORD_NOT_FOUND: return @"PEP_TRUSTWORD_NOT_FOUND";
dirk@511
   106
        case PEP_TRUSTWORDS_FPR_WRONG_LENGTH: return @"PEP_TRUSTWORDS_FPR_WRONG_LENGTH";
dirk@511
   107
        case PEP_TRUSTWORDS_DUPLICATE_FPR: return @"PEP_TRUSTWORDS_DUPLICATE_FPR";
dirk@511
   108
        case PEP_CANNOT_CREATE_KEY: return @"PEP_CANNOT_CREATE_KEY";
dirk@511
   109
        case PEP_CANNOT_SEND_KEY: return @"PEP_CANNOT_SEND_KEY";
dirk@511
   110
        case PEP_PHRASE_NOT_FOUND: return @"PEP_PHRASE_NOT_FOUND";
dirk@511
   111
        case PEP_SEND_FUNCTION_NOT_REGISTERED: return @"PEP_SEND_FUNCTION_NOT_REGISTERED";
dirk@511
   112
        case PEP_CONTRAINTS_VIOLATED: return @"PEP_CONTRAINTS_VIOLATED";
dirk@511
   113
        case PEP_CANNOT_ENCODE: return @"PEP_CANNOT_ENCODE";
dirk@511
   114
        case PEP_SYNC_NO_NOTIFY_CALLBACK: return @"PEP_SYNC_NO_NOTIFY_CALLBACK";
dirk@511
   115
        case PEP_SYNC_ILLEGAL_MESSAGE: return @"PEP_SYNC_ILLEGAL_MESSAGE";
dirk@511
   116
        case PEP_SYNC_NO_INJECT_CALLBACK: return @"PEP_SYNC_NO_INJECT_CALLBACK";
dirk@511
   117
        case PEP_SEQUENCE_VIOLATED: return @"PEP_SEQUENCE_VIOLATED";
dirk@511
   118
        case PEP_CANNOT_INCREASE_SEQUENCE: return @"PEP_CANNOT_INCREASE_SEQUENCE";
dirk@511
   119
        case PEP_CANNOT_SET_SEQUENCE_VALUE: return @"PEP_CANNOT_SET_SEQUENCE_VALUE";
dirk@511
   120
        case PEP_OWN_SEQUENCE: return @"PEP_OWN_SEQUENCE";
dirk@511
   121
        case PEP_SYNC_STATEMACHINE_ERROR: return @"PEP_SYNC_STATEMACHINE_ERROR";
dirk@511
   122
        case PEP_SYNC_NO_TRUST: return @"PEP_SYNC_NO_TRUST";
dirk@511
   123
        case PEP_STATEMACHINE_INVALID_STATE: return @"PEP_STATEMACHINE_INVALID_STATE";
dirk@511
   124
        case PEP_STATEMACHINE_INVALID_EVENT: return @"PEP_STATEMACHINE_INVALID_EVENT";
dirk@511
   125
        case PEP_STATEMACHINE_INVALID_CONDITION: return @"PEP_STATEMACHINE_INVALID_CONDITION";
dirk@511
   126
        case PEP_STATEMACHINE_INVALID_ACTION: return @"PEP_STATEMACHINE_INVALID_ACTION";
dirk@511
   127
        case PEP_STATEMACHINE_INHIBITED_EVENT: return @"PEP_STATEMACHINE_INHIBITED_EVENT";
dirk@511
   128
        case PEP_COMMIT_FAILED: return @"PEP_COMMIT_FAILED";
dirk@511
   129
        case PEP_MESSAGE_CONSUME: return @"PEP_MESSAGE_CONSUME";
dirk@511
   130
        case PEP_MESSAGE_IGNORE: return @"PEP_MESSAGE_IGNORE";
dirk@511
   131
        case PEP_RECORD_NOT_FOUND: return @"PEP_RECORD_NOT_FOUND";
dirk@511
   132
        case PEP_CANNOT_CREATE_TEMP_FILE: return @"PEP_CANNOT_CREATE_TEMP_FILE";
dirk@511
   133
        case PEP_ILLEGAL_VALUE: return @"PEP_ILLEGAL_VALUE";
dirk@511
   134
        case PEP_BUFFER_TOO_SMALL: return @"PEP_BUFFER_TOO_SMALL";
dirk@511
   135
        case PEP_OUT_OF_MEMORY: return @"PEP_OUT_OF_MEMORY";
dirk@511
   136
        case PEP_UNKNOWN_ERROR: return @"PEP_UNKNOWN_ERROR";
dirk@511
   137
        case PEP_VERSION_MISMATCH: return @"PEP_VERSION_MISMATCH";
dirk@582
   138
        case PEP_CANNOT_REENCRYPT: return @"PEP_CANNOT_REENCRYPT";
dirk@511
   139
    }
dirk@511
   140
}
dirk@511
   141
dirk@511
   142
- (NSString * _Nullable)pEpErrorString
dirk@511
   143
{
dirk@511
   144
    if ([self.domain isEqualToString:s_pEpAdapterDomain]) {
dirk@511
   145
        return stringFromPEPStatus((PEP_STATUS) self.code);
dirk@511
   146
    } else {
dirk@511
   147
        return nil;
dirk@511
   148
    }
dirk@511
   149
}
dirk@511
   150
dirk@483
   151
@end