Skip to content

Commit 3ee824a

Browse files
martinhaintzmar-haivivianyentran
authored
add 'runWithClient' example to http-integration.mdx (#10979)
* 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]>
1 parent 83fcfb6 commit 3ee824a

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

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

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,37 @@ description: "Learn more about the Sentry HTTP integration for the Dart SDK."
44
sidebar_order: 2
55
---
66

7+
## Using the `SentryHttpClient` function
8+
9+
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).
10+
11+
### Usage
12+
13+
```dart {filename:http_client.dart}{tabTitle: default}
14+
import 'package:sentry/sentry.dart';
15+
16+
final client = SentryHttpClient();
17+
18+
try {
19+
final response = await client.get(Uri.https('www.example.com', ''));
20+
print(response.body);
21+
} finally {
22+
client.close();
23+
}
24+
```
25+
26+
```dart {filename:http_client.dart}{tabTitle: runWithClient}
27+
import 'package:http/http.dart';
28+
import 'package:sentry/sentry.dart';
29+
30+
final sentryHttpClient = SentryHttpClient();
31+
32+
await runWithClient(() async {
33+
final response = await get(Uri.https('www.example.com', ''));
34+
print(response.body);
35+
}, () => sentryHttpClient);
36+
```
37+
738
## Reporting Bad HTTP Requests as Errors
839

940
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).
@@ -13,11 +44,11 @@ import 'package:sentry/sentry.dart';
1344
1445
var client = SentryHttpClient();
1546
try {
16-
var uriResponse = await client.post('https://example.com/whatsit/create',
17-
body: {'name': 'doodle', 'color': 'blue'});
18-
print(await client.get(uriResponse.bodyFields['uri']));
47+
var uriResponse = await client.post('https://example.com/whatsit/create',
48+
body: {'name': 'doodle', 'color': 'blue'});
49+
print(await client.get(uriResponse.bodyFields['uri']));
1950
} finally {
20-
client.close();
51+
client.close();
2152
}
2253
```
2354

@@ -54,11 +85,11 @@ var client = SentryHttpClient(
5485
);
5586
5687
try {
57-
var uriResponse = await client.post('https://example.com/whatsit/create',
58-
body: {'name': 'doodle', 'color': 'blue'});
59-
print(await client.get(uriResponse.bodyFields['uri']));
88+
var uriResponse = await client.post('https://example.com/whatsit/create',
89+
body: {'name': 'doodle', 'color': 'blue'});
90+
print(await client.get(uriResponse.bodyFields['uri']));
6091
} finally {
61-
client.close();
92+
client.close();
6293
}
6394
```
6495

@@ -83,11 +114,11 @@ final transaction = Sentry.startTransaction(
83114
84115
var client = SentryHttpClient();
85116
try {
86-
var uriResponse = await client.post('https://example.com/whatsit/create',
87-
body: {'name': 'doodle', 'color': 'blue'});
88-
print(await client.get(uriResponse.bodyFields['uri']));
117+
var uriResponse = await client.post('https://example.com/whatsit/create',
118+
body: {'name': 'doodle', 'color': 'blue'});
119+
print(await client.get(uriResponse.bodyFields['uri']));
89120
} finally {
90-
client.close();
121+
client.close();
91122
}
92123
93124
await transaction.finish(status: SpanStatus.ok());

0 commit comments

Comments
 (0)