Skip to content

Commit

Permalink
fix: error type for message decryption failure
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitpubnub committed Nov 27, 2023
1 parent b91b128 commit 315664c
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions pubnub/lib/src/dx/_endpoints/history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ class BatchHistoryResultEntry {
/// Otherwise, it will be `null`.
Map<String, dynamic>? meta;

/// Error message. this will contain information if message decryption is failed
/// This field will contain PubNubException if message decryption is failed
/// for given `message`.
String? error;
PubNubException? error;

BatchHistoryResultEntry._(this.message, this.timetoken, this.uuid,
this.messageType, this.actions, this.meta, this.error);
Expand All @@ -165,24 +165,26 @@ class BatchHistoryResultEntry {
factory BatchHistoryResultEntry.fromJson(Map<String, dynamic> object,
{CipherKey? cipherKey, Function? decryptFunction}) {
var message;
String? errorMessage;
PubNubException? error;
if (cipherKey == null && decryptFunction is decryptWithKey) {
message = object['message'];
} else {
try {
if (!(object['message'] is String)) {
throw FormatException('not a base64 string.');
}
message = decryptFunction is decryptWithKey
? json.decode(utf8.decode(decryptFunction(cipherKey!,
base64.decode(object['message'] as String).toList())))
: json.decode(utf8.decode(decryptFunction!(
base64.decode(object['message'] as String).toList())));
} on CryptoException catch (e) {
message = object['message'];
errorMessage =
'Can not decrypt the message payload. Please check keyset or crypto configuration \n ${e.message}';
} catch (e) {
error = e;
} on FormatException catch (e) {
message = object['message'];
errorMessage =
'Can not decrypt the message payload. Please check keyset or crypto configuration';
error = PubNubException(
'Can not decrypt the message payload. Please check keyset or crypto configuration. ${e.message}');
}
}

Expand All @@ -193,7 +195,7 @@ class BatchHistoryResultEntry {
MessageTypeExtension.fromInt(object['message_type']),
object['actions'],
object['meta'] == '' ? null : object['meta'],
errorMessage);
error);
}
}

Expand Down

0 comments on commit 315664c

Please sign in to comment.