-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from atsign-foundation/exception_handling_cha…
…nges Add AtExceptionStack
- Loading branch information
Showing
6 changed files
with
182 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
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<AtChainedException> { | ||
final _exceptionList = Queue<AtChainedException>(); | ||
|
||
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.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'; | ||
} | ||
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.message == atChainedException.message) { | ||
return 0; | ||
} | ||
} | ||
return 1; | ||
} | ||
} | ||
|
||
class AtChainedException { | ||
late Intent intent; | ||
late ExceptionScenario exceptionScenario; | ||
late String message; | ||
|
||
AtChainedException(this.intent, this.exceptionScenario, this.message); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters