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