Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Move logs out from the adapter (#2260)
Browse files Browse the repository at this point in the history
Resolves #2255
  • Loading branch information
AlexV525 authored Jul 1, 2024
1 parent 224db94 commit eb1684e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 18 deletions.
1 change: 1 addition & 0 deletions dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ See the [Migration Guide][] for the complete breaking changes list.**
- If the `CancelToken` got canceled before making requests,
throws the exception directly rather than cut actual HTTP requests afterward.
- Catch `MediaType` parse exception in `Transformer.isJsonMimeType`.
- Improves warning logs on the Web platform.

## 5.4.3+1

Expand Down
2 changes: 1 addition & 1 deletion dio/lib/src/interceptors/imply_content_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ImplyContentTypeInterceptor extends Interceptor {
} else if (data is List<Map> || data is Map || data is String) {
contentType = Headers.jsonContentType;
} else {
debugLog(
warningLog(
'${data.runtimeType} cannot be used '
'to imply a default content-type, '
'please set a proper content-type in the request.',
Expand Down
14 changes: 14 additions & 0 deletions dio/lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,20 @@ class Options {
ProgressCallback? onReceiveProgress,
StackTrace? sourceStackTrace,
}) {
if (data != null && kIsWeb) {
if (sendTimeout != null && sendTimeout! > Duration.zero) {
warningLog(
'sendTimeout cannot be used without a request body to send on Web',
StackTrace.current,
);
}
if (onSendProgress != null) {
warningLog(
'onSendProgress cannot be used without a request body to send on Web',
StackTrace.current,
);
}
}
final query = <String, dynamic>{};
query.addAll(baseOpt.queryParameters);
if (queryParameters != null) {
Expand Down
4 changes: 2 additions & 2 deletions dio/lib/src/transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ abstract class Transformer {
mediaType.mimeType == 'text/json' ||
mediaType.subtype.endsWith('+json');
} catch (e, s) {
debugLog(
warningLog(
'Failed to parse the media type: $contentType, '
'thus it is not a JSON MIME type.',
s,
Expand All @@ -104,7 +104,7 @@ abstract class Transformer {
if (data is Map<String, dynamic>) {
return Transformer.urlEncodeMap(data, options.listFormat);
}
debugLog(
warningLog(
'The data is a type of `Map` (${data.runtimeType}), '
'but the transformer can only encode `Map<String, dynamic>`.\n'
'If you are writing maps using `{}`, '
Expand Down
2 changes: 1 addition & 1 deletion dio/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Map<String, V> caseInsensitiveKeyMap<V>([Map<String, V>? value]) {
}

// TODO(Alex): Provide a configurable property on the Dio class once https://github.com/cfug/dio/discussions/1982 has made some progress.
void debugLog(String message, StackTrace stackTrace) {
void warningLog(String message, StackTrace stackTrace) {
if (!kReleaseMode) {
dev.log(
message,
Expand Down
15 changes: 1 addition & 14 deletions plugins/web_adapter/lib/src/html/adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,6 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
}
});
}
} else {
if (sendTimeout > Duration.zero) {
debugLog(
'sendTimeout cannot be used without a request body to send',
StackTrace.current,
);
}
if (options.onSendProgress != null) {
debugLog(
'onSendProgress cannot be used without a request body to send',
StackTrace.current,
);
}
}

final receiveStopwatch = Stopwatch();
Expand Down Expand Up @@ -268,7 +255,7 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {

if (requestStream != null) {
if (options.method == 'GET') {
debugLog(
warningLog(
'GET request with a body data are not support on the '
'web platform. Use POST/PUT instead.',
StackTrace.current,
Expand Down

0 comments on commit eb1684e

Please sign in to comment.