Skip to content

Commit

Permalink
Merge pull request #230 from Ali1Ammar/master
Browse files Browse the repository at this point in the history
feat: add missing error setting for dio logger
  • Loading branch information
Frezyx authored May 23, 2024
2 parents b6340fd + 65ee1ee commit 76b4ce5
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/talker_dio_logger/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class _MyAppState extends State<MyApp> {
),
ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.red),
backgroundColor: WidgetStateProperty.all(Colors.red),
),
onPressed: () {
_dio.get('htt://jsonplaceholder.typicode.com/todos');
Expand Down
9 changes: 6 additions & 3 deletions packages/talker_dio_logger/lib/dio_logs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,16 @@ class DioErrorLog extends TalkerLog {
if (statusCode != null) {
msg += '\nStatus: ${dioException.response?.statusCode}';
}
msg += '\nMessage: $responseMessage';

if (data != null) {
if (settings.printErrorMessage && responseMessage != null) {
msg += '\nMessage: $responseMessage';
}

if (settings.printErrorData && data != null) {
final prettyData = encoder.convert(data);
msg += '\nData: $prettyData';
}
if (!(headers?.isEmpty ?? true)) {
if (settings.printErrorHeaders && !(headers?.isEmpty ?? true)) {
final prettyHeaders = encoder.convert(headers!.map);
msg += '\nHeaders: $prettyHeaders';
}
Expand Down
10 changes: 10 additions & 0 deletions packages/talker_dio_logger/lib/talker_dio_logger_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class TalkerDioLogger extends Interceptor {
bool? printResponseData,
bool? printResponseHeaders,
bool? printResponseMessage,
bool? printErrorData,
bool? printErrorHeaders,
bool? printErrorMessage,
bool? printRequestData,
bool? printRequestHeaders,
AnsiPen? requestPen,
Expand All @@ -41,6 +44,9 @@ class TalkerDioLogger extends Interceptor {
printRequestData: printRequestData,
printRequestHeaders: printRequestHeaders,
printResponseData: printResponseData,
printErrorData: printErrorData,
printErrorHeaders: printErrorHeaders,
printErrorMessage: printErrorMessage,
printResponseHeaders: printResponseHeaders,
printResponseMessage: printResponseMessage,
requestPen: requestPen,
Expand Down Expand Up @@ -95,6 +101,10 @@ class TalkerDioLogger extends Interceptor {
@override
void onError(DioException err, ErrorInterceptorHandler handler) {
super.onError(err, handler);
final accepted = settings.errorFilter?.call(err) ?? true;
if (!accepted) {
return;
}
try {
final message = '${err.requestOptions.uri}';
final httpErrorLog = DioErrorLog(
Expand Down
25 changes: 25 additions & 0 deletions packages/talker_dio_logger/lib/talker_dio_logger_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ class TalkerDioLoggerSettings {
this.printResponseData = true,
this.printResponseHeaders = false,
this.printResponseMessage = true,
this.printErrorData = true,
this.printErrorHeaders = true,
this.printErrorMessage = true,
this.printRequestData = true,
this.printRequestHeaders = false,
this.requestPen,
this.responsePen,
this.errorPen,
this.requestFilter,
this.responseFilter,
this.errorFilter,
});

/// Print [response.data] if true
Expand All @@ -25,6 +29,15 @@ class TalkerDioLoggerSettings {
/// Print [response.statusMessage] if true
final bool printResponseMessage;

/// Print [error.response.data] if true
final bool printErrorData;

/// Print [error.response.headers] if true
final bool printErrorHeaders;

/// Print [error.message] if true
final bool printErrorMessage;

/// Print [request.data] if true
final bool printRequestData;

Expand Down Expand Up @@ -72,29 +85,41 @@ class TalkerDioLoggerSettings {
/// You can add your custom logic to log only specific HTTP responses [Response].
final bool Function(Response response)? responseFilter;

/// For error filtering.
/// You can add your custom logic to log only specific Dio error [DioException].
final bool Function(DioException response)? errorFilter;

TalkerDioLoggerSettings copyWith({
bool? printResponseData,
bool? printResponseHeaders,
bool? printResponseMessage,
bool? printErrorData,
bool? printErrorHeaders,
bool? printErrorMessage,
bool? printRequestData,
bool? printRequestHeaders,
AnsiPen? requestPen,
AnsiPen? responsePen,
AnsiPen? errorPen,
bool Function(RequestOptions requestOptions)? requestFilter,
bool Function(Response response)? responseFilter,
bool Function(DioException response)? errorFilter,
}) {
return TalkerDioLoggerSettings(
printResponseData: printResponseData ?? this.printResponseData,
printResponseHeaders: printResponseHeaders ?? this.printResponseHeaders,
printResponseMessage: printResponseMessage ?? this.printResponseMessage,
printErrorData: printErrorData ?? this.printErrorData,
printErrorHeaders: printErrorHeaders ?? this.printErrorHeaders,
printErrorMessage: printErrorMessage ?? this.printErrorMessage,
printRequestData: printRequestData ?? this.printRequestData,
printRequestHeaders: printRequestHeaders ?? this.printRequestHeaders,
requestPen: requestPen ?? this.requestPen,
responsePen: responsePen ?? this.responsePen,
errorPen: errorPen ?? this.errorPen,
requestFilter: requestFilter ?? this.requestFilter,
responseFilter: responseFilter ?? this.responseFilter,
errorFilter: errorFilter ?? this.errorFilter,
);
}
}
36 changes: 36 additions & 0 deletions packages/talker_dio_logger/test/logs_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,42 @@ void main() {
'Message: Error message'));
});

test(
'generateTextMessage should not include data, header and message if disabled',
() {
final dioException = DioException(
requestOptions: RequestOptions(path: '/test', method: 'GET'),
message: 'Error message',
response: Response(
requestOptions: RequestOptions(path: '/test', method: 'GET'),
headers: Headers.fromMap(
{
'content-type': ['application/json'],
},
)));
final settings = TalkerDioLoggerSettings(
errorPen: AnsiPen()..blue(),
printErrorData: false,
printErrorHeaders: false,
printErrorMessage: false,
);
final dioErrorLog = DioErrorLog('Error title',
dioException: dioException, settings: settings);

final result = dioErrorLog.generateTextMessage();
expect(result, contains('[log] [GET] Error title'));
expect(result, isNot(contains('Message: Error message')));
expect(
result,
isNot(contains(
'Headers: {\n'
' "content-type": [\n'
' "application/json"\n'
' ]\n'
'}',
)));
});

test(
'generateTextMessage should include status if response has a status code',
() {
Expand Down
23 changes: 23 additions & 0 deletions packages/talker_dio_logger/test/settings_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ void main() {
final updatedSettings = originalSettings.copyWith(
printResponseData: false,
printRequestHeaders: true,
printErrorHeaders: false,
requestPen: AnsiPen()..yellow(),
responseFilter: null,
);

expect(updatedSettings.printResponseData, equals(false));
expect(updatedSettings.printRequestHeaders, equals(true));
expect(updatedSettings.printErrorHeaders, equals(false));
expect(
updatedSettings.requestPen, isNot(same(originalSettings.requestPen)));
expect(updatedSettings.responseFilter, isNull);
Expand Down Expand Up @@ -46,6 +48,21 @@ void main() {
expect(settings.responseFilter!(unsuccessfulResponse), equals(false));
});

test('errorFilter should return true for cancelled responses', () {
final settings = TalkerDioLoggerSettings(
errorFilter: (DioException err) =>
err.type == DioExceptionType.cancel);
final cancelledResponse = DioException(
requestOptions: RequestOptions(path: '/test'),
type: DioExceptionType.cancel);
final timeoutResponse = DioException(
requestOptions: RequestOptions(path: '/test'),
type: DioExceptionType.sendTimeout);

expect(settings.errorFilter!(cancelledResponse), equals(true));
expect(settings.errorFilter!(timeoutResponse), equals(false));
});

test(
'copyWith should create a new instance with updated values for all fields',
() {
Expand All @@ -55,6 +72,8 @@ void main() {
printResponseMessage: true,
printRequestData: true,
printRequestHeaders: false,
printErrorHeaders: false,
printErrorData: true,
requestPen: AnsiPen()..green(),
responsePen: AnsiPen()..cyan(),
errorPen: AnsiPen()..red(),
Expand All @@ -63,13 +82,17 @@ void main() {
final updatedSettings = originalSettings.copyWith(
printResponseData: false,
printRequestHeaders: true,
printErrorHeaders: true,
printErrorData: false,
requestPen: AnsiPen()..yellow(),
);

expect(updatedSettings.printResponseData, equals(false));
expect(updatedSettings.printResponseHeaders, equals(false));
expect(updatedSettings.printResponseMessage, equals(true));
expect(updatedSettings.printRequestData, equals(true));
expect(updatedSettings.printErrorHeaders, equals(true));
expect(updatedSettings.printErrorData, equals(false));
expect(
updatedSettings.printRequestHeaders,
equals(true),
Expand Down

0 comments on commit 76b4ce5

Please sign in to comment.