Skip to content

Commit

Permalink
[Dio] Replace failed http client adapter with interceptor (#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
ueman authored Feb 11, 2022
1 parent c2893b8 commit a3d7007
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 486 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

* [Dio] Ref: Replace FailedRequestAdapter with FailedRequestInterceptor (#728)
* Fix: Add missing return values - dart analyzer (#742)

# 6.3.0
Expand Down
11 changes: 6 additions & 5 deletions dio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ Integration for the [`dio`](https://pub.dev/packages/dio) package.

- Follow the installing instructions on [pub.dev](https://pub.dev/packages/sentry/install).

- Initialize the Sentry SDK using the DSN issued by Sentry.io:
- Initialize the Sentry SDK using the DSN issued by Sentry.io.

- Call `dio.addSentry()`. This *must* be the last initialization step of the Dio setup, otherwise your configuration of Dio might overwrite the Sentry configuration.

```dart
import 'package:sentry/sentry.dart';
Expand All @@ -39,14 +41,13 @@ Future<void> main() async {
void initDio() {
final dio = Dio();
// Make sure this is the last initialization method,
// otherwise you might override Sentrys configuration.
/// This *must* be the last initialization step of the Dio setup, otherwise
/// your configuration of Dio might overwrite the Sentry configuration.
dio.addSentry(...);
}
```

Dependending on you configuration, this can add performance tracing,
http breadcrumbs and automatic recording of bad http requests.
Depending on your configuration, this adds performance tracing and http breadcrumbs. Also, exceptions from invalid http status codes or parsing exceptions are automatically captured.

#### Resources

Expand Down
165 changes: 0 additions & 165 deletions dio/lib/src/failed_request_client_adapter.dart

This file was deleted.

21 changes: 21 additions & 0 deletions dio/lib/src/failed_request_interceptor.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// ignore_for_file: public_member_api_docs

import 'package:dio/dio.dart';
import 'package:sentry/sentry.dart';

class FailedRequestInterceptor extends Interceptor {
FailedRequestInterceptor({Hub? hub}) : _hub = hub ?? HubAdapter();

final Hub _hub;

@override
void onError(
DioError err,
ErrorInterceptorHandler handler,
) {
_hub.getSpan()?.throwable = err;
_hub.captureException(err);

handler.next(err);
}
}
14 changes: 0 additions & 14 deletions dio/lib/src/sentry_dio_client_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'dart:typed_data';
import 'package:dio/dio.dart';
import 'package:meta/meta.dart';
import 'package:sentry/sentry.dart';
import 'failed_request_client_adapter.dart';
import 'tracing_client_adapter.dart';
import 'breadcrumb_client_adapter.dart';

Expand Down Expand Up @@ -54,24 +53,11 @@ class SentryDioClientAdapter extends HttpClientAdapter {
Hub? hub,
bool recordBreadcrumbs = true,
bool networkTracing = true,
MaxRequestBodySize maxRequestBodySize = MaxRequestBodySize.never,
List<SentryStatusCode> failedRequestStatusCodes = const [],
bool captureFailedRequests = false,
bool sendDefaultPii = false,
}) {
_hub = hub ?? HubAdapter();

var innerClient = client;

innerClient = FailedRequestClientAdapter(
failedRequestStatusCodes: failedRequestStatusCodes,
captureFailedRequests: captureFailedRequests,
maxRequestBodySize: maxRequestBodySize,
sendDefaultPii: sendDefaultPii,
hub: _hub,
client: innerClient,
);

if (networkTracing) {
innerClient = TracingClientAdapter(client: innerClient, hub: _hub);
}
Expand Down
11 changes: 7 additions & 4 deletions dio/lib/src/sentry_dio_extension.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:dio/dio.dart';
import 'package:meta/meta.dart';
import 'package:sentry/sentry.dart';
import 'failed_request_interceptor.dart';
import 'sentry_transformer.dart';
import 'sentry_dio_client_adapter.dart';

Expand All @@ -19,15 +20,17 @@ extension SentryDioExtension on Dio {
bool captureFailedRequests = false,
bool sendDefaultPii = false,
}) {
if (captureFailedRequests) {
// Add FailedRequestInterceptor at index 0, so it's the first interceptor.
// This ensures that it is called and not skipped by any previous interceptor.
interceptors.insert(0, FailedRequestInterceptor());
}

// intercept http requests
httpClientAdapter = SentryDioClientAdapter(
client: httpClientAdapter,
recordBreadcrumbs: recordBreadcrumbs,
networkTracing: networkTracing,
maxRequestBodySize: maxRequestBodySize,
failedRequestStatusCodes: failedRequestStatusCodes,
captureFailedRequests: captureFailedRequests,
sendDefaultPii: sendDefaultPii,
);

// intercept transformations
Expand Down
3 changes: 1 addition & 2 deletions dio/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ dev_dependencies:
yaml: ^3.1.0 # needed for version match (code and pubspec)
coverage: ^1.0.3
mockito: ^5.0.16
http: ^0.13.0


dependency_overrides:
sentry:
path: ../dart
Loading

0 comments on commit a3d7007

Please sign in to comment.