-
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a way to extract stacktraces from custom exception types (#1335)
- Loading branch information
Showing
11 changed files
with
227 additions
and
4 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,35 @@ | ||
import 'protocol.dart'; | ||
import 'sentry_options.dart'; | ||
|
||
/// Sentry handles [Error.stackTrace] by default. For other cases | ||
/// extend this abstract class and return a custom [StackTrace] of your | ||
/// exceptions. | ||
/// | ||
/// Implementing an extractor and providing it through | ||
/// [SentryOptions.addExceptionStackTraceExtractor] will enable the framework to | ||
/// extract the inner stacktrace and add it to [SentryException] when no other | ||
/// stacktrace was provided while capturing the event. | ||
/// | ||
/// For an example on how to use the API refer to dio/DioStackTraceExtractor or the | ||
/// code below: | ||
/// | ||
/// ```dart | ||
/// class ExceptionWithInner { | ||
/// ExceptionWithInner(this.innerException, this.innerStackTrace); | ||
/// Object innerException; | ||
/// dynamic innerStackTrace; | ||
/// } | ||
/// | ||
/// class ExceptionWithInnerStackTraceExtractor extends ExceptionStackTraceExtractor<ExceptionWithInner> { | ||
/// @override | ||
/// dynamic cause(ExceptionWithInner error) { | ||
/// return error.innerStackTrace; | ||
/// } | ||
/// } | ||
/// | ||
/// options.addExceptionStackTraceExtractor(ExceptionWithInnerStackTraceExtractor()); | ||
/// ``` | ||
abstract class ExceptionStackTraceExtractor<T> { | ||
dynamic stackTrace(T error); | ||
Type get exceptionType => T; | ||
} |
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
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,10 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'package:sentry/sentry.dart'; | ||
|
||
/// Extracts the inner stacktrace from [DioError] | ||
class DioStackTraceExtractor extends ExceptionStackTraceExtractor<DioError> { | ||
@override | ||
StackTrace? stackTrace(DioError error) { | ||
return error.stackTrace; | ||
} | ||
} |
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,49 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'package:sentry_dio/src/dio_stacktrace_extractor.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
late Fixture fixture; | ||
|
||
setUp(() { | ||
fixture = Fixture(); | ||
}); | ||
|
||
group(DioStackTraceExtractor, () { | ||
test('extracts stacktrace', () { | ||
final sut = fixture.getSut(); | ||
final exception = Exception('foo bar'); | ||
final stacktrace = StackTrace.current; | ||
|
||
final dioError = DioError( | ||
error: exception, | ||
requestOptions: RequestOptions(path: '/foo/bar'), | ||
stackTrace: stacktrace, | ||
); | ||
|
||
final result = sut.stackTrace(dioError); | ||
|
||
expect(result, stacktrace); | ||
}); | ||
|
||
test('extracts nothing with missing stacktrace', () { | ||
final sut = fixture.getSut(); | ||
final exception = Exception('foo bar'); | ||
|
||
final dioError = DioError( | ||
error: exception, | ||
requestOptions: RequestOptions(path: '/foo/bar'), | ||
); | ||
|
||
final result = sut.stackTrace(dioError); | ||
|
||
expect(result, isNull); | ||
}); | ||
}); | ||
} | ||
|
||
class Fixture { | ||
DioStackTraceExtractor getSut() { | ||
return DioStackTraceExtractor(); | ||
} | ||
} |
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