From fe59b667ec7a6c97bccb32ce2dc37f47f0aca7f4 Mon Sep 17 00:00:00 2001 From: sitaram Date: Thu, 12 May 2022 10:39:04 +0530 Subject: [PATCH 1/2] Add AtExceptionStack --- at_commons/lib/at_commons.dart | 2 + .../src/exception/at_exception_manager.dart | 15 +++++ .../lib/src/exception/at_exception_stack.dart | 60 +++++++++++++++++++ .../lib/src/exception/at_exceptions.dart | 41 +++++++++++++ .../validators/at_key_validation_impl.dart | 2 +- 5 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 at_commons/lib/src/exception/at_exception_manager.dart create mode 100644 at_commons/lib/src/exception/at_exception_stack.dart diff --git a/at_commons/lib/at_commons.dart b/at_commons/lib/at_commons.dart index 98b9940a..c6d1ed31 100644 --- a/at_commons/lib/at_commons.dart +++ b/at_commons/lib/at_commons.dart @@ -6,6 +6,8 @@ export 'package:at_commons/src/buffer/at_buffer.dart'; export 'package:at_commons/src/buffer/at_buffer_impl.dart'; export 'package:at_commons/src/compaction/at_compaction_config.dart'; export 'package:at_commons/src/exception/at_client_exceptions.dart'; +export 'package:at_commons/src/exception/at_exception_manager.dart'; +export 'package:at_commons/src/exception/at_exception_stack.dart'; export 'package:at_commons/src/exception/at_exception_utils.dart'; export 'package:at_commons/src/exception/at_exceptions.dart' hide diff --git a/at_commons/lib/src/exception/at_exception_manager.dart b/at_commons/lib/src/exception/at_exception_manager.dart new file mode 100644 index 00000000..24d97cba --- /dev/null +++ b/at_commons/lib/src/exception/at_exception_manager.dart @@ -0,0 +1,15 @@ +import 'package:at_commons/at_commons.dart'; + +/// AtExceptionManager is responsible for creating instances of the appropriate exception +/// classes for a given exception scenario. +class AtExceptionManager { + static AtException createException(AtException atException) { + // If the instance of atException is AtClientException. return as is. + if (atException is AtClientException) { + return atException; + } + // Else wrap the atException into AtClientException and return. + return (AtClientException.message(atException.message))..fromException(atException); + + } +} diff --git a/at_commons/lib/src/exception/at_exception_stack.dart b/at_commons/lib/src/exception/at_exception_stack.dart new file mode 100644 index 00000000..7fe10fb2 --- /dev/null +++ b/at_commons/lib/src/exception/at_exception_stack.dart @@ -0,0 +1,60 @@ +import 'dart:collection'; + +import 'package:at_commons/src/exception/at_exceptions.dart'; + +/// Class to maintain stack of exceptions to form a chained exception. +class AtExceptionStack implements Comparable { + final _exceptionList = Queue(); + + void add(AtChainedException atChainedException) { + if (compareTo(atChainedException) == 0) { + return; + } + _exceptionList.addFirst(atChainedException); + } + + /// Concatenate the error messages in the exceptionList and returns a trace message + String getTraceMessage() { + var size = _exceptionList.length; + String fullMessage = ''; + fullMessage = + '${getIntentMessage(_exceptionList.first.intent)} caused by\n'; + for (AtChainedException element in _exceptionList) { + size--; + fullMessage += '${element.atException.message}'; + if (size != 0) { + fullMessage += ' caused by\n'; + } + } + return fullMessage; + } + + /// Accepts the Intent and returns a message + String getIntentMessage(Intent intent) { + if (intent == Intent.fetchData) { + return 'Failed to fetch data'; + } + if (intent == Intent.shareData) { + return 'Failed to share data'; + } + return 'Failed to notify data'; + } + + @override + int compareTo(AtChainedException atChainedException) { + for (var element in _exceptionList) { + if (element.atException == atChainedException.atException) { + return 0; + } + } + return 1; + } +} + +class AtChainedException { + late Intent intent; + late ExceptionScenario exceptionScenario; + late AtException atException; + + AtChainedException(this.intent, this.exceptionScenario, this.atException); +} diff --git a/at_commons/lib/src/exception/at_exceptions.dart b/at_commons/lib/src/exception/at_exceptions.dart index 2f342a7c..4ad28aa3 100644 --- a/at_commons/lib/src/exception/at_exceptions.dart +++ b/at_commons/lib/src/exception/at_exceptions.dart @@ -1,3 +1,4 @@ +import 'package:at_commons/src/exception/at_exception_stack.dart'; import 'package:at_commons/src/keystore/at_key.dart'; /// The class [AtException] and its subclasses represents various exceptions that can arise @@ -7,12 +8,27 @@ class AtException implements Exception { /// Represents error message that details the cause of the exception var message; + AtExceptionStack _traceStack = AtExceptionStack(); + AtException(this.message); + AtException fromException(AtException atException) { + _traceStack = atException._traceStack; + return atException; + } + @override String toString() { return 'Exception: $message'; } + + void stack(AtChainedException atChainedException) { + _traceStack.add(atChainedException); + } + + String getTraceMessage() { + return _traceStack.getTraceMessage(); + } } /// The class [AtConnectException] and its subclasses represent any issues that prevents an connection to the root or the secondary server @@ -157,3 +173,28 @@ class InvalidRequestException extends AtException { class InvalidResponseException extends AtException { InvalidResponseException(message) : super(message); } + +enum ExceptionScenario { + noNetworkConnectivity, + rootServerNotReachable, + secondaryServerNotReachable, + invalidValueProvided, + valueExceedingBufferLimit, + noNamespaceProvided, + invalidKeyFormed, + invalidMetadataProvided, + keyNotFound, + encryptionFailed, + decryptionFailed, + remoteVerbExecutionFailed, + localVerbExecutionFailed, + atSignDoesNotExist +} + +enum Intent { + shareData, + fetchData, + validateKey, + validateAtSign, + remoteVerbExecution +} diff --git a/at_commons/lib/src/validators/at_key_validation_impl.dart b/at_commons/lib/src/validators/at_key_validation_impl.dart index 53da6bd6..1221e6a7 100644 --- a/at_commons/lib/src/validators/at_key_validation_impl.dart +++ b/at_commons/lib/src/validators/at_key_validation_impl.dart @@ -198,7 +198,7 @@ class KeyOwnershipValidation extends Validation { if ((type != KeyType.cachedPublicKey && type != KeyType.cachedSharedKey) && owner != atSign) { return ValidationResult( - 'Owner of the key $owner should be same as current @sign atSign'); + 'Owner of the key $owner should be same as current @sign $atSign'); } return ValidationResult.noFailure(); } From 52ba744c5c76d67ae34ca820c014e273f3c17f21 Mon Sep 17 00:00:00 2001 From: sitaram Date: Wed, 18 May 2022 18:22:28 +0530 Subject: [PATCH 2/2] Update AtExceptionStack --- .../src/exception/at_client_exceptions.dart | 50 ++++++++++++++++--- .../lib/src/exception/at_exception_stack.dart | 11 ++-- .../lib/src/exception/at_exceptions.dart | 21 ++++++-- 3 files changed, 68 insertions(+), 14 deletions(-) diff --git a/at_commons/lib/src/exception/at_client_exceptions.dart b/at_commons/lib/src/exception/at_client_exceptions.dart index 02c73a05..24b5a082 100644 --- a/at_commons/lib/src/exception/at_client_exceptions.dart +++ b/at_commons/lib/src/exception/at_client_exceptions.dart @@ -5,29 +5,65 @@ class AtClientException extends AtException { AtClientException(errorCode, message) : super(message); /// The named constructor that takes only message - AtClientException.message(message) : super(message); + AtClientException.message(message, + {Intent? intent, ExceptionScenario? exceptionScenario}) + : super(message, intent: intent, exceptionScenario: exceptionScenario); } class AtKeyException extends AtClientException { - AtKeyException(message) : super.message(message); + AtKeyException(message, + {Intent? intent, ExceptionScenario? exceptionScenario}) + : super.message(message, + intent: intent, exceptionScenario: exceptionScenario); } class AtValueException extends AtClientException { - AtValueException(message) : super.message(message); + AtValueException(message, + {Intent? intent, ExceptionScenario? exceptionScenario}) + : super.message(message, + intent: intent, exceptionScenario: exceptionScenario); } class AtEncryptionException extends AtClientException { - AtEncryptionException(message) : super.message(message); + AtEncryptionException(message, + {Intent? intent, ExceptionScenario? exceptionScenario}) + : super.message(message, + intent: intent, exceptionScenario: exceptionScenario); } class AtPublicKeyChangeException extends AtEncryptionException { - AtPublicKeyChangeException(message) : super(message); + AtPublicKeyChangeException(message, + {Intent? intent, ExceptionScenario? exceptionScenario}) + : super(message, intent: intent, exceptionScenario: exceptionScenario); } class AtPublicKeyNotFoundException extends AtEncryptionException { - AtPublicKeyNotFoundException(message) : super(message); + AtPublicKeyNotFoundException(message, + {Intent? intent, ExceptionScenario? exceptionScenario}) + : super(message, intent: intent, exceptionScenario: exceptionScenario); } class AtDecryptionException extends AtClientException { - AtDecryptionException(message) : super.message(message); + AtDecryptionException(message, + {Intent? intent, ExceptionScenario? exceptionScenario}) + : super.message(message, + intent: intent, exceptionScenario: exceptionScenario); +} + +class AtPrivateKeyNotFoundException extends AtDecryptionException { + AtPrivateKeyNotFoundException(message, + {Intent? intent, ExceptionScenario? exceptionScenario}) + : super(message, intent: intent, exceptionScenario: exceptionScenario); +} + +class SharedKeyNotFoundException extends AtDecryptionException { + SharedKeyNotFoundException(message, + {Intent? intent, ExceptionScenario? exceptionScenario}) + : super(message, intent: intent, exceptionScenario: exceptionScenario); +} + +class SelfKeyNotFoundException extends AtDecryptionException { + SelfKeyNotFoundException(message, + {Intent? intent, ExceptionScenario? exceptionScenario}) + : super(message, intent: intent, exceptionScenario: exceptionScenario); } diff --git a/at_commons/lib/src/exception/at_exception_stack.dart b/at_commons/lib/src/exception/at_exception_stack.dart index 7fe10fb2..14ac8ad8 100644 --- a/at_commons/lib/src/exception/at_exception_stack.dart +++ b/at_commons/lib/src/exception/at_exception_stack.dart @@ -21,7 +21,7 @@ class AtExceptionStack implements Comparable { '${getIntentMessage(_exceptionList.first.intent)} caused by\n'; for (AtChainedException element in _exceptionList) { size--; - fullMessage += '${element.atException.message}'; + fullMessage += element.message; if (size != 0) { fullMessage += ' caused by\n'; } @@ -37,13 +37,16 @@ class AtExceptionStack implements Comparable { if (intent == Intent.shareData) { return 'Failed to share data'; } + if(intent == Intent.decryptData) { + return 'Failed to decrypt the data'; + } return 'Failed to notify data'; } @override int compareTo(AtChainedException atChainedException) { for (var element in _exceptionList) { - if (element.atException == atChainedException.atException) { + if (element.message == atChainedException.message) { return 0; } } @@ -54,7 +57,7 @@ class AtExceptionStack implements Comparable { class AtChainedException { late Intent intent; late ExceptionScenario exceptionScenario; - late AtException atException; + late String message; - AtChainedException(this.intent, this.exceptionScenario, this.atException); + AtChainedException(this.intent, this.exceptionScenario, this.message); } diff --git a/at_commons/lib/src/exception/at_exceptions.dart b/at_commons/lib/src/exception/at_exceptions.dart index 4ad28aa3..faf8c86a 100644 --- a/at_commons/lib/src/exception/at_exceptions.dart +++ b/at_commons/lib/src/exception/at_exceptions.dart @@ -8,9 +8,17 @@ class AtException implements Exception { /// Represents error message that details the cause of the exception var message; + Intent? intent; + + ExceptionScenario? exceptionScenario; + AtExceptionStack _traceStack = AtExceptionStack(); - AtException(this.message); + AtException(this.message, {this.intent, this.exceptionScenario}) { + if (intent != null && exceptionScenario != null) { + _traceStack.add(AtChainedException(intent!, exceptionScenario!, message)); + } + } AtException fromException(AtException atException) { _traceStack = atException._traceStack; @@ -188,7 +196,8 @@ enum ExceptionScenario { decryptionFailed, remoteVerbExecutionFailed, localVerbExecutionFailed, - atSignDoesNotExist + atSignDoesNotExist, + fetchEncryptionKeys } enum Intent { @@ -196,5 +205,11 @@ enum Intent { fetchData, validateKey, validateAtSign, - remoteVerbExecution + remoteVerbExecution, + notifyData, + decryptData, + fetchEncryptionPublicKey, + fetchEncryptionPrivateKey, + fetchEncryptionSharedKey, + fetchSelfEncryptionKey }