Skip to content

Commit

Permalink
add 'runWithClient' example to http-integration.mdx (#10979)
Browse files Browse the repository at this point in the history
* add 'runWithClient' example to http-integration.mdx

* move runWithClient to the top

* add default code snippet

* Update docs/platforms/dart/integrations/http-integration.mdx

Co-authored-by: vivianyentran <[email protected]>

* rephrased the `runWithClient` section in dart

* Update docs/platforms/dart/integrations/http-integration.mdx

Co-authored-by: vivianyentran <[email protected]>

---------

Co-authored-by: Martin Haintz <[email protected]>
Co-authored-by: vivianyentran <[email protected]>
  • Loading branch information
3 people committed Aug 27, 2024
1 parent 83fcfb6 commit 3ee824a
Showing 1 changed file with 43 additions and 12 deletions.
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

You can use the `SentryHttpClient` and call its methods directly, or you can use it with the [`runWithClient`](https://pub.dev/documentation/http/latest/http/runWithClient.html) function. If you need to use a fresh instance of the `client` (so that other instances are not affected) or to run in a separate [`Zone`](https://api.dart.dev/stable/3.5.0/dart-async/Zone-class.html), which allows you to override the functionality of the existing [`Zone`](https://api.dart.dev/stable/3.5.0/dart-async/Zone-class.html), then use the `runWithClient` function (see the `runWithClient` tab). Otherwise, just use `SentryHttpClient` directly and call its methods (see the `default` tab).

### 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

0 comments on commit 3ee824a

Please sign in to comment.