Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
support all response body types
  • Loading branch information
denrase committed Jul 18, 2023
commit f9bdb8f9a5839f2776f36c5b7cb823d90cdae18e
74 changes: 63 additions & 11 deletions dio/lib/src/dio_event_processor.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// ignore_for_file: deprecated_member_use

import 'dart:convert';

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

Expand Down Expand Up @@ -87,26 +89,76 @@ class DioEventProcessor implements EventProcessor {

return SentryResponse(
headers: _options.sendDefaultPii ? headers : null,
bodySize: dioError.response?.data?.length as int?,
bodySize: _getBodySize(
dioError.response?.data,
dioError.requestOptions.responseType,
),
statusCode: response?.statusCode,
data: _getResponseData(dioError.response?.data),
data: _getResponseData(
dioError.response?.data,
dioError.requestOptions.responseType,
),
);
}

/// Returns the response data, if possible according to the users settings.
Object? _getResponseData(Object? data) {
Object? _getResponseData(Object? data, ResponseType responseType) {
if (!_options.sendDefaultPii) {
return null;
}
if (data is String) {
if (_options.maxResponseBodySize.shouldAddBody(data.codeUnits.length)) {
return data;
}
} else if (data is List<int>) {
if (_options.maxResponseBodySize.shouldAddBody(data.length)) {
return data;
}
if (data == null) {
return null;
}
switch (responseType) {
case ResponseType.json:
final js = json.encode(data);
if (_options.maxResponseBodySize.shouldAddBody(js.codeUnits.length)) {
return data;
}
break;
case ResponseType.stream:
break; // No support for logging stream body.
case ResponseType.plain:
if (data is String &&
_options.maxResponseBodySize.shouldAddBody(data.codeUnits.length)) {
return data;
}
break;
case ResponseType.bytes:
if (data is List<int> &&
_options.maxResponseBodySize.shouldAddBody(data.length)) {
return data;
}
break;
}
return null;
}

int? _getBodySize(Object? data, ResponseType responseType) {
if (data == null) {
return null;
}
switch (responseType) {
case ResponseType.json:
return json.encode(data).codeUnits.length;
case ResponseType.stream:
if (data is String) {
return data.length;
} else {
return null;
}
case ResponseType.plain:
if (data is String) {
return data.codeUnits.length;
} else {
return null;
}
case ResponseType.bytes:
if (data is List<int>) {
return data.length;
} else {
return null;
}
}
}
}
Loading