Skip to content

Commit

Permalink
support all response body types
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase committed Jul 18, 2023
1 parent d72d286 commit f9bdb8f
Show file tree
Hide file tree
Showing 2 changed files with 233 additions and 127 deletions.
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

0 comments on commit f9bdb8f

Please sign in to comment.