Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Introduce DioExceptionLogLevel #2296

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
96 changes: 93 additions & 3 deletions dio/lib/src/dio_exception.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:meta/meta.dart';

import 'options.dart';
import 'response.dart';

Expand Down Expand Up @@ -62,7 +64,80 @@ extension _DioExceptionTypeExtension on DioExceptionType {
}
}

/// Determine the content logging level of [DioException]s.
///
/// - Using [withRequestUrl] will log the URL of the exception.
/// The URL could be [Response.realUri] or [RequestOptions.uri].
/// - Using [withMessages] will log the available message of the exception.
///
/// To combine levels, use the binary operators to compute the desired level.
/// For example, to log both URL and messages, either use [all] or use:
/// ```dart
/// final level = DioExceptionLogLevel(
/// DioExceptionLogLevel.withRequestUrl | DioExceptionLogLevel.withRequestUrl,
/// );
/// ```
class DioExceptionLogLevel {
const DioExceptionLogLevel(this.value);

final int value;

static const withRequestUrl = 1;
static const withMessages = 1 << 2;

static const least = DioExceptionLogLevel(0);
static const common = DioExceptionLogLevel(withMessages);
static const all = DioExceptionLogLevel(withRequestUrl | withMessages);

bool get containsRequestUrl => value & withRequestUrl == withRequestUrl;

bool get containsMessages => value & withMessages == withMessages;

DioExceptionLogLevel operator +(DioExceptionLogLevel type) => this | type;

DioExceptionLogLevel operator -(DioExceptionLogLevel type) => this ^ type;

DioExceptionLogLevel operator |(DioExceptionLogLevel type) {
return DioExceptionLogLevel(value | type.value);
}

DioExceptionLogLevel operator ^(DioExceptionLogLevel type) {
return DioExceptionLogLevel(value ^ type.value);
}

DioExceptionLogLevel operator >>(int bit) {
return DioExceptionLogLevel(value >> bit);
}

DioExceptionLogLevel operator <<(int bit) {
return DioExceptionLogLevel(value << bit);
}

/// The values of [RequestType].
static const values = <DioExceptionLogLevel>[all, common];

/// Computes the request type from given types.
static DioExceptionLogLevel fromTypes(List<DioExceptionLogLevel> types) {
DioExceptionLogLevel result = const DioExceptionLogLevel(0);
for (final type in types) {
result += type;
}
return result;
}

@override
bool operator ==(Object other) =>
other is DioExceptionLogLevel && value == other.value;

@override
int get hashCode => value;

@override
String toString() => 'DioExceptionLogLevel($value)';
}

/// [DioException] describes the exception info when a request failed.
@immutable
class DioException implements Exception {
/// Prefer using one of the other constructors.
/// They're most likely better fitting.
Expand Down Expand Up @@ -182,6 +257,9 @@ class DioException implements Exception {
error: error,
);

/// Users can customize the logging level when a [DioException] was thrown.
static DioExceptionLogLevel logLevel = DioExceptionLogLevel.common;

/// The request info for the request that throws exception.
///
/// The info can be empty (e.g. `uri` equals to "")
Expand Down Expand Up @@ -226,11 +304,23 @@ class DioException implements Exception {

@override
String toString() {
String msg = 'DioException [${type.toPrettyDescription()}]: $message';
final buffer = StringBuffer('DioException [${type.toPrettyDescription()}]');
if (logLevel.containsRequestUrl) {
buffer.writeln(' ${response?.realUri ?? requestOptions.uri}');
}
if (logLevel.containsMessages) {
if (logLevel.containsRequestUrl) {
buffer.writeln();
} else {
buffer.write(': ');
}
buffer.write('$message');
}
if (error != null) {
msg += '\nError: $error';
buffer.writeln();
buffer.write('Error: $error');
}
return msg;
return buffer.toString();
}

/// Because of [ValidateStatus] we need to consider all status codes when
Expand Down