-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement some sort of simple exception chaining.
- Loading branch information
Showing
6 changed files
with
95 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
## 1.3.0-dev.1 | ||
|
||
* Support for exception chaining. | ||
|
||
## 1.2.0+1 | ||
|
||
* Graylog: Send timestamp with decimal places. | ||
|
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,35 @@ | ||
// based on https://github.com/dart-lang/language/issues/2119#issuecomment-1042365842 | ||
|
||
final Expando<ErrorStack> _causedBy = Expando(); | ||
T _wasCausedBy<T extends Exception>( | ||
T target, Object cause, StackTrace causeStackTrace) { | ||
_causedBy[target] = (error: cause, stack: causeStackTrace); | ||
return target; | ||
} | ||
|
||
typedef ErrorStack = ({Object error, StackTrace stack}); | ||
|
||
/// Allow exception chaining. | ||
/// example: | ||
/// ``` | ||
/// try { | ||
/// ... | ||
/// } on Exception (e, stackTrace) { | ||
/// throw | ||
/// } | ||
extension CausedByException<E extends Exception> on E { | ||
E causedBy(Object chainedException, StackTrace stackTrace) => | ||
_wasCausedBy(this, chainedException, stackTrace); | ||
ErrorStack? getCausedByException() => _causedBy[this]; | ||
|
||
String toStringWithCause() { | ||
final cause = this.getCausedByException(); | ||
if (cause == null) { | ||
return toString(); | ||
} | ||
final error = cause.error; | ||
final causeToString = | ||
error is Exception ? error.toStringWithCause() : error.toString(); | ||
return '${toString()} cause: {$causeToString}'; | ||
} | ||
} |
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,26 @@ | ||
import 'package:logging_appenders/logging_appenders.dart'; | ||
import 'package:logging_appenders/src/exception_chain.dart'; | ||
import 'package:test/expect.dart'; | ||
import 'package:test/scaffolding.dart'; | ||
|
||
import 'package:logging/logging.dart'; | ||
|
||
final _logger = Logger('exception_chain'); | ||
|
||
void main() { | ||
PrintAppender.setupLogging(); | ||
test('Simple chained exception', () { | ||
try { | ||
try { | ||
int.parse('a'); | ||
} catch (e, stackTrace) { | ||
throw Exception('unable to parse').causedBy(e, stackTrace); | ||
} | ||
fail('unreachable'); | ||
} on Exception catch (e, stackTrace) { | ||
expect(e.getCausedByException(), isNotNull); | ||
expect(e.getCausedByException()?.error, isFormatException); | ||
_logger.finer('catched exception.', e, stackTrace); | ||
} | ||
}); | ||
} |