Skip to content

Allow attachments to be bound to exception more easily #683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
20 changes: 17 additions & 3 deletions dart/lib/src/hub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ class Hub {
);
} else {
final item = _peek();
final scope = await _cloneAndRunWithScope(item.scope, withScope);
var scope = await _cloneAndRunWithScope(item.scope, withScope);

try {
if (_options.isTracingEnabled()) {
event = _assignTraceContext(event);
}

scope = await _applyOptionsBeforeCaptureWithScope(scope, event);

sentryId = await item.client.captureEvent(
event,
stackTrace: stackTrace,
Expand Down Expand Up @@ -129,7 +131,7 @@ class Hub {
);
} else {
final item = _peek();
final scope = await _cloneAndRunWithScope(item.scope, withScope);
var scope = await _cloneAndRunWithScope(item.scope, withScope);

try {
var event = SentryEvent(
Expand All @@ -141,6 +143,8 @@ class Hub {
event = _assignTraceContext(event);
}

scope = await _applyOptionsBeforeCaptureWithScope(scope, event);

sentryId = await item.client.captureEvent(
event,
stackTrace: stackTrace,
Expand Down Expand Up @@ -185,7 +189,8 @@ class Hub {
);
} else {
final item = _peek();
final scope = await _cloneAndRunWithScope(item.scope, withScope);
var scope = await _cloneAndRunWithScope(item.scope, withScope);
scope = await _applyOptionsBeforeCaptureWithScope(scope, null);

try {
sentryId = await item.client.captureMessage(
Expand Down Expand Up @@ -248,6 +253,15 @@ class Hub {
return scope;
}

Future<Scope> _applyOptionsBeforeCaptureWithScope(
Scope scope, SentryEvent? event) {
return _cloneAndRunWithScope(
scope,
_options.beforeCaptureWithScope == null
? null
: (scope) => _options.beforeCaptureWithScope!(scope, event));
}

/// Adds a breacrumb to the current Scope
Future<void> addBreadcrumb(Breadcrumb crumb, {dynamic hint}) async {
if (!_isEnabled) {
Expand Down
5 changes: 5 additions & 0 deletions dart/lib/src/sentry_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ class SentryOptions {
/// to be sent to Sentry.
TracesSamplerCallback? tracesSampler;

/// callback before capturing exceptions or events
BeforeCaptureWithScopeCallback? beforeCaptureWithScope;

/// Send statistics to sentry when the client drops events.
bool sendClientReports = true;

Expand Down Expand Up @@ -376,6 +379,8 @@ typedef SentryLogger = void Function(
typedef TracesSamplerCallback = double? Function(
SentrySamplingContext samplingContext);

typedef BeforeCaptureWithScopeCallback = void Function(Scope, SentryEvent?);

/// A NoOp logger that does nothing
void noOpLogger(
SentryLevel level,
Expand Down