From cf66af6f2e7c69a3e0712317f8473ab09711d426 Mon Sep 17 00:00:00 2001 From: manuroe Date: Mon, 14 Nov 2016 16:54:51 +0100 Subject: OLMKit: Replaced NSAsserts by NSErrors --- xcode/OLMKit/OLMOutboundGroupSession.m | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'xcode/OLMKit/OLMOutboundGroupSession.m') diff --git a/xcode/OLMKit/OLMOutboundGroupSession.m b/xcode/OLMKit/OLMOutboundGroupSession.m index c86fa9f..9741db1 100644 --- a/xcode/OLMKit/OLMOutboundGroupSession.m +++ b/xcode/OLMKit/OLMOutboundGroupSession.m @@ -56,7 +56,7 @@ size_t result = olm_init_outbound_group_session(session, random.mutableBytes, random.length); if (result == olm_error()) { const char *error = olm_outbound_group_session_last_error(session); - NSAssert(NO, @"olm_init_outbound_group_session error: %s", error); + NSLog(@"olm_init_outbound_group_session error: %s", error); return nil; } } @@ -72,7 +72,7 @@ size_t result = olm_outbound_group_session_id(session, idData.mutableBytes, idData.length); if (result == olm_error()) { const char *error = olm_outbound_group_session_last_error(session); - NSAssert(NO, @"olm_outbound_group_session_id error: %s", error); + NSLog(@"olm_outbound_group_session_id error: %s", error); return nil; } NSString *idString = [[NSString alloc] initWithData:idData encoding:NSUTF8StringEncoding]; @@ -92,14 +92,14 @@ size_t result = olm_outbound_group_session_key(session, sessionKeyData.mutableBytes, sessionKeyData.length); if (result == olm_error()) { const char *error = olm_outbound_group_session_last_error(session); - NSAssert(NO, @"olm_outbound_group_session_key error: %s", error); + NSLog(@"olm_outbound_group_session_key error: %s", error); return nil; } NSString *sessionKey = [[NSString alloc] initWithData:sessionKeyData encoding:NSUTF8StringEncoding]; return sessionKey; } -- (NSString *)encryptMessage:(NSString *)message { +- (NSString *)encryptMessage:(NSString *)message error:(NSError**)error { NSData *plaintextData = [message dataUsingEncoding:NSUTF8StringEncoding]; size_t ciphertextLength = olm_group_encrypt_message_length(session, plaintextData.length); NSMutableData *ciphertext = [NSMutableData dataWithLength:ciphertextLength]; @@ -108,8 +108,19 @@ } size_t result = olm_group_encrypt(session, plaintextData.bytes, plaintextData.length, ciphertext.mutableBytes, ciphertext.length); if (result == olm_error()) { - const char *error = olm_outbound_group_session_last_error(session); - NSAssert(NO, @"olm_group_encrypt error: %s", error); + const char *olm_error = olm_outbound_group_session_last_error(session); + + NSString *errorString = [NSString stringWithUTF8String:olm_error]; + NSLog(@"olm_group_encrypt error: %@", errorString); + + if (error && olm_error && errorString) { + *error = [NSError errorWithDomain:OLMErrorDomain + code:0 + userInfo:@{ + NSLocalizedDescriptionKey: [NSString stringWithFormat:@"olm_group_encrypt error: %@", errorString] + }]; + } + return nil; } return [[NSString alloc] initWithData:ciphertext encoding:NSUTF8StringEncoding]; @@ -127,7 +138,7 @@ NSParameterAssert(serializedData.length > 0); if (key.length == 0 || serializedData.length == 0) { if (error) { - *error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Bad length."}]; + *error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: @"Bad length."}]; } return nil; } @@ -137,7 +148,7 @@ const char *olm_error = olm_outbound_group_session_last_error(session); NSString *errorString = [NSString stringWithUTF8String:olm_error]; if (error && errorString) { - *error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}]; + *error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}]; } return nil; } @@ -154,7 +165,7 @@ const char *olm_error = olm_outbound_group_session_last_error(session); NSString *errorString = [NSString stringWithUTF8String:olm_error]; if (error && errorString) { - *error = [NSError errorWithDomain:@"org.matrix.olm" code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}]; + *error = [NSError errorWithDomain:OLMErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: errorString}]; } return nil; } -- cgit v1.2.3