Skip to content

Commit

Permalink
[Release 0.5.3]
Browse files Browse the repository at this point in the history
* Reduced repeated failed lookup printout
* Added unit tests
* Fixed 2.10 analyzer errors
  • Loading branch information
=Zachary Merritt committed Feb 6, 2022
1 parent f5eac5d commit c73f312
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 29 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions lib/aws_cloudwatch_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class CloudWatchHandler {
if (cw != null) {
return CloudWatch._(cw);
}
return null;
}

/// Creates a CloudWatch instance.
Expand Down
36 changes: 21 additions & 15 deletions lib/src/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.',
Expand All @@ -360,7 +367,6 @@ class Logger {
throw error;
}
}
return false; // continue execution
}

/// Queues [sendLogs] until all logs are sent or error occurs
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
79 changes: 69 additions & 10 deletions test/src/logger_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io';

import 'package:aws_cloudwatch/src/logger.dart';
Expand Down Expand Up @@ -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<Exception>());
expect(e.toString(), 'Exception: General Exception');
expect(cloudWatch.shouldPrintFailedLookup, true);
return;
}
fail('Exception was not thrown');
});
test('raiseFailedLookups', () {
cloudWatch.raiseFailedLookups = true;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit c73f312

Please sign in to comment.