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