diff --git a/CHANGELOG.md b/CHANGELOG.md index 86116b0..0da9eb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [0.5.3] - 2022/02/06 + +* Reduced repeated failed lookup printout +* Added unit tests +* Fixed 2.10 analyzer errors + ## [0.5.2] - 2022/01/25 * Fixed issue where message bytes exceeded max in rare cases with many small messages diff --git a/example/pubspec.lock b/example/pubspec.lock index 78dc748..9db86f3 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -14,14 +14,14 @@ packages: name: aws_cloudwatch url: "https://pub.dartlang.org" source: hosted - version: "0.5.0" + version: "0.5.2" aws_request: dependency: transitive description: name: aws_request url: "https://pub.dartlang.org" source: hosted - version: "0.3.0+1" + version: "0.4.0+1" boolean_selector: dependency: transitive description: diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 18364d4..79e5fa3 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -10,7 +10,7 @@ dependencies: flutter: sdk: flutter - aws_cloudwatch: ^0.5.0 # Send cloudwatch logs + aws_cloudwatch: ^0.5.2 # Send cloudwatch logs dev_dependencies: diff --git a/lib/aws_cloudwatch_handler.dart b/lib/aws_cloudwatch_handler.dart index 3865c39..9c9831e 100644 --- a/lib/aws_cloudwatch_handler.dart +++ b/lib/aws_cloudwatch_handler.dart @@ -128,6 +128,7 @@ class CloudWatchHandler { if (cw != null) { return CloudWatch._(cw); } + return null; } /// Creates a CloudWatch instance. diff --git a/lib/src/logger.dart b/lib/src/logger.dart index ab1f208..99f1b28 100644 --- a/lib/src/logger.dart +++ b/lib/src/logger.dart @@ -148,6 +148,10 @@ class Logger { set maxMessagesPerRequest(int val) => logStack.maxMessagesPerRequest = val; + /// Holds whether the last request succeeded or not. + /// Used to stop excessive printing about failed lookups + bool shouldPrintFailedLookup = true; + /// Private version of delay Duration _delay; @@ -329,28 +333,31 @@ class Logger { 'CloudWatch INFO: Added messages to log stack', ); dynamic error; - await sendAllLogs().catchError((e) { + try { + await sendAllLogs(); + } catch (e) { error = e; - }); - if (checkError(error)) { - return; } + checkError(error); } /// Checks info about [error] and returns whether execution should stop - bool checkError(dynamic error) { - if (error != null) { + void checkError(dynamic error) { + if (error == null) { + shouldPrintFailedLookup = true; + } else { if (!raiseFailedLookups && (error.toString().contains('XMLHttpRequest error') || error.toString().contains('Failed host lookup'))) { - print( - 'CloudWatch: Failed host lookup! This usually means internet ' - 'is unavailable but could also indicate a problem with the ' - 'region $region.', - ); - return true; // stop execution - } - if (error is TimeoutException) { + if (shouldPrintFailedLookup) { + print( + 'CloudWatch: Failed host lookup! This usually means internet ' + 'is unavailable but could also indicate a problem with the ' + 'region $region.', + ); + shouldPrintFailedLookup = false; + } + } else if (error is TimeoutException) { throw CloudWatchException( message: 'A timeout occurred while trying to upload logs. Consider ' 'increasing requestTimeout.', @@ -360,7 +367,6 @@ class Logger { throw error; } } - return false; // continue execution } /// Queues [sendLogs] until all logs are sent or error occurs diff --git a/pubspec.yaml b/pubspec.yaml index 32de79d..3254938 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: aws_cloudwatch description: An easy, lightweight, and convenient way to reliably send logs to AWS CloudWatch. -version: 0.5.2 +version: 0.5.3 homepage: https://github.com/Zsmerritt/Flutter_AWS_CloudWatch environment: diff --git a/test/src/logger_test.dart b/test/src/logger_test.dart index 3f83cf8..3b26eeb 100644 --- a/test/src/logger_test.dart +++ b/test/src/logger_test.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:io'; import 'package:aws_cloudwatch/src/logger.dart'; @@ -665,18 +666,49 @@ void main() { mockCloudWatch: true, ); }); - test('error null', () { - final bool res = cloudWatch.checkError(null); - expect(res, false); + test('error XMLHttpRequest error', () { + cloudWatch.checkError(Exception('XMLHttpRequest error')); + expect(cloudWatch.shouldPrintFailedLookup, false); }); - test('error null', () { - final bool res = - cloudWatch.checkError(Exception('XMLHttpRequest error')); - expect(res, true); + test('error Failed host lookup', () { + cloudWatch.checkError(Exception('Failed host lookup')); + expect(cloudWatch.shouldPrintFailedLookup, false); }); - test('error null', () { - final bool res = cloudWatch.checkError(Exception('Failed host lookup')); - expect(res, true); + test('error Failed host lookup then null', () { + cloudWatch.checkError(Exception('Failed host lookup')); + expect(cloudWatch.shouldPrintFailedLookup, false); + cloudWatch.checkError(null); + expect(cloudWatch.shouldPrintFailedLookup, true); + }); + test('error timeout', () { + cloudWatch.shouldPrintFailedLookup = true; + try { + cloudWatch.checkError(TimeoutException('')); + } on CloudWatchException catch (e) { + expect( + e.message, + 'A timeout occurred while trying to upload logs. ' + 'Consider increasing requestTimeout.'); + expect(cloudWatch.shouldPrintFailedLookup, true); + return; + } catch (e) { + fail('Wrong exception was thrown'); + } + fail('Exception was not thrown'); + }); + test('error general exception', () { + cloudWatch.shouldPrintFailedLookup = true; + try { + cloudWatch.checkError(Exception('General Exception')); + } on CloudWatchException { + fail('Wrong exception was thrown'); + } catch (e) { + expect(e, isA()); + expect(e.toString(), 'Exception: General Exception'); + expect(cloudWatch.shouldPrintFailedLookup, true); + return; + } + fail('Exception was not thrown'); }); test('raiseFailedLookups', () { cloudWatch.raiseFailedLookups = true; @@ -834,6 +866,33 @@ void main() { expect(res, true); expect(cloudWatch.sequenceToken, 'def'); }); + test('InvalidParameterException', () async { + final AwsResponse awsResponse = + await AwsResponse.parseResponse(Response( + '{"__type": "InvalidParameterException", "message": "this is a test"}', + 400, + )); + try { + final bool res = await cloudWatch.handleError(awsResponse); + expect(res, true); + expect(cloudWatch.sequenceToken, 'def'); + } on CloudWatchException catch (e) { + expect( + e.message, + 'An InvalidParameterException occurred! This is probably a bug! ' + 'Please report it at\n' + 'https://github.com/Zsmerritt/Flutter_AWS_CloudWatch/issues/new \n' + 'and it will be addressed promptly. \n' + 'Message: this is a test Raw: {__type: InvalidParameterException, message: this is a test}'); + expect(awsResponse.raw, + '{__type: InvalidParameterException, message: this is a test}'); + expect(e.type, 'InvalidParameterException'); + return; + } catch (e) { + fail('Wrong exception thrown!'); + } + fail('Exception not thrown!'); + }); test('unknown type', () async { final AwsResponse awsResponse = await AwsResponse.parseResponse(Response(