I have some cases in which I am cancelling the token for certain API requests. This is treated by Sentry as an error and being logged into Sentry.
I would like cancel token errors to not be logged.
I tried the following:
await SentryFlutter.init(
(options) {
const sentryDsn = String.fromEnvironment('SENTRYDSN',
defaultValue:
'[READACTED]');
options.dsn = sentryDsn;
options.tracesSampleRate = 1.0;
options.reportPackages = false;
options.beforeSend = (event, hint) {
if (hint is DioException && isCancelTokenError(hint)) {
return null;
}
return event;
};
},
This has not worked. Any help is appreciated.
SentryEvent keeps a copy of the originating error at SentryEvent.throwable, so a more correct beforeSend callback would look like:
(event, hint) {
if (event.throwable is DioException && CancelToken.isCancel(event.throwable)) {
return null;
}
return event;
}
where CancelToken.isCancel is provided by Dio.
Putting this together into a minimal demonstration:
import 'package:dio/dio.dart';
import 'package:sentry/sentry.dart';
import 'package:sentry_dio/sentry_dio.dart';
void main() {
Sentry.init((options) {
options.beforeSend = (event, hint) {
if (event.throwable is DioException &&
CancelToken.isCancel(event.throwable)) {
return null;
}
return event;
};
options.dsn = 'https://example@sentry.io/example';
options.diagnosticLevel = SentryLevel.debug;
options.log = (level, message, {exception, logger, stackTrace}) {
print(message);
};
}, appRunner: () {
final dio = Dio();
dio.addSentry();
final token = CancelToken();
dio.get<String>('https://wrong-url.dev/', cancelToken: token);
token.cancel();
});
}
which logs, among other things, the following, showing that the event is dropped due to beforeSend returning null:
SentryEvent was dropped by beforeSend callback