Skip to content
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

add 'runWithClient' example to http-integration.mdx #10979

Merged
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions docs/platforms/dart/integrations/http-integration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,37 @@ description: "Learn more about the Sentry HTTP integration for the Dart SDK."
sidebar_order: 2
---

## Using the `SentryHttpClient` function

Depending on your use case, you can either use the `SentryHttpClient` directly and call its methods (see the `default` tab), or you can use the [`runWithClient`](https://pub.dev/documentation/http/latest/http/runWithClient.html) function (see the `runWithClient` tab). With [`runWithClient`](https://pub.dev/documentation/http/latest/http/runWithClient.html), a fresh instance of the `client` is used and the other instances are not affected. It also runs in a separate [`Zone`](https://api.dart.dev/stable/3.5.0/dart-async/Zone-class.html), allowing you to override the functionality of the existing [`Zone`](https://api.dart.dev/stable/3.5.0/dart-async/Zone-class.html).
martinhaintz marked this conversation as resolved.
Show resolved Hide resolved

### Usage

```dart {filename:http_client.dart}{tabTitle: default}
import 'package:sentry/sentry.dart';

final client = SentryHttpClient();

try {
final response = await client.get(Uri.https('www.example.com', ''));
print(response.body);
} finally {
client.close();
}
```

```dart {filename:http_client.dart}{tabTitle: runWithClient}
import 'package:http/http.dart';
import 'package:sentry/sentry.dart';

final sentryHttpClient = SentryHttpClient();

await runWithClient(() async {
final response = await get(Uri.https('www.example.com', ''));
print(response.body);
}, () => sentryHttpClient);
```

## Reporting Bad HTTP Requests as Errors

The `SentryHttpClient` can also catch exceptions that may occur during requests — for example [`SocketException`](https://api.dart.dev/stable/2.13.4/dart-io/SocketException-class.html).
Expand All @@ -13,11 +44,11 @@ import 'package:sentry/sentry.dart';

var client = SentryHttpClient();
try {
var uriResponse = await client.post('https://example.com/whatsit/create',
body: {'name': 'doodle', 'color': 'blue'});
print(await client.get(uriResponse.bodyFields['uri']));
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();
client.close();
}
```

Expand Down Expand Up @@ -54,11 +85,11 @@ var client = SentryHttpClient(
);

try {
var uriResponse = await client.post('https://example.com/whatsit/create',
body: {'name': 'doodle', 'color': 'blue'});
print(await client.get(uriResponse.bodyFields['uri']));
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();
client.close();
}
```

Expand All @@ -83,11 +114,11 @@ final transaction = Sentry.startTransaction(

var client = SentryHttpClient();
try {
var uriResponse = await client.post('https://example.com/whatsit/create',
body: {'name': 'doodle', 'color': 'blue'});
print(await client.get(uriResponse.bodyFields['uri']));
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();
client.close();
}

await transaction.finish(status: SpanStatus.ok());
Expand Down
Loading