Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 19 additions & 7 deletions packages/datadog_flutter_plugin/lib/src/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ import 'internal_logger.dart';

typedef WrappedCall<T> = FutureOr<T?> Function();

void _handleError(Object error, StackTrace stackTrace, String methodName,
bool _willHandleError(Object e) {
return e is ArgumentError || e is PlatformException;
}

// Returns true if the error was handled, false if the error should be re-thrown
bool _handleError(Object error, StackTrace stackTrace, String methodName,
InternalLogger logger, Map<String, Object?>? serializedAttributes) {
if (error is ArgumentError) {
logger.warn(InternalLogger.argumentWarning(
methodName, error, serializedAttributes));
return true;
} else if (error is PlatformException) {
logger.error('Datadog experienced a PlatformException - ${error.message}');
logger.error(
Expand All @@ -25,9 +31,10 @@ void _handleError(Object error, StackTrace stackTrace, String methodName,
stackTrace,
'PlatformException',
);
} else {
throw error;
return true;
}

return false;
}

/// Wraps a call to a platform channel with common error handling and telemetry.
Expand All @@ -40,11 +47,14 @@ void wrap(
try {
var result = call();
if (result is Future) {
result.catchError((dynamic e, StackTrace st) =>
_handleError(e, st, methodName, logger, attributes));
result.catchError((dynamic e, StackTrace st) {
_handleError(e, st, methodName, logger, attributes);
}, test: _willHandleError);
}
} catch (e, st) {
_handleError(e, st, methodName, logger, attributes);
if (!_handleError(e, st, methodName, logger, attributes)) {
rethrow;
}
}
}

Expand All @@ -57,7 +67,9 @@ Future<T?> wrapAsync<T>(String methodName, InternalLogger logger,
try {
result = await call();
} catch (e, st) {
_handleError(e, st, methodName, logger, attributes);
if (!_handleError(e, st, methodName, logger, attributes)) {
rethrow;
}
}

return result;
Expand Down
4 changes: 4 additions & 0 deletions packages/datadog_tracking_http_client/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Fix rethrown execptions on `close` not having the correct stack trace.

## 1.2.1

* Fix an invalid assertion when processing stream errors. See [#355]

## 1.2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class _DatadogTrackingHttpRequest implements HttpClientRequest {
return innerContext.close().then((value) {
return _DatadogTrackingHttpResponse(
client.datadogSdk, value, rumKey, _tracingContext);
}, onError: (Object e, StackTrace? st) async {
}, onError: (Object e, StackTrace? st) {
_onStreamError(e, st);
throw e;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,89 @@ void main() {
expect(capturedAttributes[DatadogRumPlatformAttributeKey.spanID], isNull);
expect(capturedAttributes[DatadogRumPlatformAttributeKey.rulePsr], 0.12);
});

test('error on openUrl stops resource with error', () async {
const error = SocketException('Mock socket exception');
when(() => mockClient.openUrl(any(), any())).thenThrow(error);
final client = DatadogTrackingHttpClient(
mockDatadog,
DdHttpTrackingPluginConfiguration(),
mockClient,
);

var url = Uri.parse('https://test_url/path');

await expectLater(() async => await client.openUrl('get', url),
throwsA(predicate((e) => e == error)));
var capturedKey = verify(() => mockRum.startResourceLoading(
captureAny(), RumHttpMethod.get, url.toString(), any()))
.captured[0] as String;
verify(() => mockRum.stopResourceLoadingWithErrorInfo(
capturedKey,
error.toString(),
error.runtimeType.toString(),
));
});

test('error on close stops resource with error preserving stack', () async {
const error = SocketException('Mock socket exception');
var url = Uri.parse('https://test_url/path');
final completer = setupMockRequest(url);

when(() => mockRequest.uri).thenReturn(url);

var request = await client.openUrl('get', url);
verify(() => mockClient.openUrl('get', url));
var capturedKey = verify(
() => mockRum.startResourceLoading(
captureAny(), RumHttpMethod.get, url.toString(), any()),
).captured[0] as String;

final stack = StackTrace.current;
completer.completeError(error, stack);
try {
await request.close();
} catch (e, st) {
expect(st, stack);
expect(e, error);
}
verify(() => mockRum.stopResourceLoadingWithErrorInfo(
capturedKey,
error.toString(),
error.runtimeType.toString(),
any(),
));
});

test('error on done stops resource with error preserving stack', () async {
const error = SocketException('Mock socket exception');
var url = Uri.parse('https://test_url/path');
final completer = setupMockRequest(url);

when(() => mockRequest.uri).thenReturn(url);

var request = await client.openUrl('get', url);
verify(() => mockClient.openUrl('get', url));
var capturedKey = verify(
() => mockRum.startResourceLoading(
captureAny(), RumHttpMethod.get, url.toString(), any()),
).captured[0] as String;

final stack = StackTrace.current;
completer.completeError(error, stack);
try {
await request.done;
} catch (e, st) {
expect(st, stack);
expect(e, error);
}
verify(() => mockRum.stopResourceLoadingWithErrorInfo(
capturedKey,
error.toString(),
error.runtimeType.toString(),
any(),
));
});
});

for (final headerType in TracingHeaderType.values) {
Expand Down Expand Up @@ -603,29 +686,6 @@ void main() {
verifyNever(() => requestHeaders.add(header, any()));
}
});

test('error on openUrl stops resource with error', () async {
const error = SocketException('Mock socket exception');
when(() => mockClient.openUrl(any(), any())).thenThrow(error);
final client = DatadogTrackingHttpClient(
mockDatadog,
DdHttpTrackingPluginConfiguration(),
mockClient,
);

var url = Uri.parse('https://test_url/path');

await expectLater(() async => await client.openUrl('get', url),
throwsA(predicate((e) => e == error)));
var capturedKey = verify(() => mockRum.startResourceLoading(
captureAny(), RumHttpMethod.get, url.toString(), any()))
.captured[0] as String;
verify(() => mockRum.stopResourceLoadingWithErrorInfo(
capturedKey,
error.toString(),
error.runtimeType.toString(),
));
});
});
}

Expand Down