Advanced Usage
Requirements
- For the usage of the Dart SDK, the minimal required Dart SDK version is
2.12.0
.
Automatic Breadcrumbs
To track automatic Breadcrumbs for HTTP requests, you can add a Sentry wrapper that does it automatically for you.
This is only possible if you are using the http.Client class from the http library.
The SentryHttpClient
can be used as a standalone client like this:
import 'package:http/http.dart' as http;
import 'package:sentry/sentry.dart';
final client = SentryHttpClient();
try {
final url = 'https://example.com/whatsit/create';
final response = await client.post(url, body: {
'name': 'doodle',
'color': 'blue',
});
print('Response body: ${response.body}');
} finally {
client.close();
}
The SentryHttpClient
can also be used as a wrapper for your own HTTP Client
:
import 'package:http/http.dart' as http;
import 'package:sentry/sentry.dart';
final myClient = http.Client();
final client = SentryHttpClient(client: myClient);
try {
final url = 'https://example.com/whatsit/create';
final response = await client.post(url, body: {
'name': 'doodle',
'color': 'blue',
});
print('Response body: ${response.body}');
} finally {
client.close();
}
Reporting Bad HTTP Requests as Errors
The SentryHttpClient
can also catch exceptions that may occur during requests — for example SocketException
.
This is currently an opt-in feature. The following example shows how to enable it.
import 'package:sentry/sentry.dart';
var client = SentryHttpClient(captureFailedRequests: true);
try {
var uriResponse = await client.post('https://example.com/whatsit/create',
body: {'name': 'doodle', 'color': 'blue'});
print(await client.get(uriResponse.bodyFields['uri']));
} finally {
client.close();
}
Furthermore you can track HTTP requests that you consider bad. In the following example, exceptions are captured for each request with a status code within the range of 400 to 404, and also for 500.
import 'package:sentry/sentry.dart';
var client = SentryHttpClient(
failedRequestStatusCodes: [
SentryStatusCode.range(400, 404),
SentryStatusCode(500),
],
);
try {
var uriResponse = await client.post('https://example.com/whatsit/create',
body: {'name': 'doodle', 'color': 'blue'});
print(await client.get(uriResponse.bodyFields['uri']));
} finally {
client.close();
}
Performance Monitoring for HTTP Requests
The SentryHttpClient
starts a span out of the active span bound to the scope for each HTTP Request.
This is currently an opt-in feature. The following example shows how to enable it.
import 'package:sentry/sentry.dart';
final transaction = Sentry.startTransaction(
'webrequest',
'request',
bindToScope: true,
);
var client = SentryHttpClient(networkTracing: true);
try {
var uriResponse = await client.post('https://example.com/whatsit/create',
body: {'name': 'doodle', 'color': 'blue'});
print(await client.get(uriResponse.bodyFields['uri']));
} finally {
client.close();
}
await transaction.finish(status: SpanStatus.ok());
Tips for Catching Errors
- Use a
try/catch
block - Use a
catchError
block forFutures
Isolate
errors on thecurrent
Isolate which is the equivalent of a main/UI thread, e.g. usingIsolate.current.addErrorListener
, are captured automatically (Only for non-Web Apps).- For your own
Isolates
, add anErrorListener
and callSentry.captureException
- Package:
- pub:sentry
- Version:
- 8.6.0
- Repository:
- https://github.com/getsentry/sentry-dart