From e9b22236c47a0c454e5e3ccff353bd80444e8228 Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Fri, 13 Oct 2023 15:08:00 -0400 Subject: [PATCH 1/5] Add more info into the `AnalyticsException` --- pkgs/unified_analytics/lib/src/asserts.dart | 62 +++++++++++++++++---- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/asserts.dart b/pkgs/unified_analytics/lib/src/asserts.dart index 715c3c48a..4712a227e 100644 --- a/pkgs/unified_analytics/lib/src/asserts.dart +++ b/pkgs/unified_analytics/lib/src/asserts.dart @@ -43,14 +43,22 @@ void checkBody(Map body) { // alpha-numeric characters and underscores, and must start // with an alphabetic character if (eventName.length > 40) { - throw AnalyticsException('Limit event names to 40 chars or less'); + throw AnalyticsException( + 'Limit event names to 40 chars or less', + moreInfo: 'Event name: $eventName is too long', + ); } if (!alphaNumericPattern.hasMatch(eventName)) { throw AnalyticsException( - 'Event name can only have alphanumeric chars and underscores'); + 'Event name can only have alphanumeric chars and underscores', + moreInfo: 'Event name: $eventName contains invalid characters', + ); } if (!alphabeticPattern.hasMatch(eventName[0])) { - throw AnalyticsException('Event name first char must be alphabetic char'); + throw AnalyticsException( + 'Event name first char must be alphabetic char', + moreInfo: 'Event name: $eventName must begin with a valid character', + ); } final eventParams = eventMap['params'] as Map; @@ -58,7 +66,10 @@ void checkBody(Map body) { // GA4 Limitation: // Events can have a maximum of 25 parameters if (eventParams.length > 25) { - throw AnalyticsException('Limit params for each event to less than 25'); + throw AnalyticsException( + 'Limit params for each event to less than 25', + moreInfo: 'Event: $eventName has too many parameters', + ); } // Loop through each of the event parameters @@ -75,7 +86,9 @@ void checkBody(Map body) { value is double || value is bool)) { throw AnalyticsException( - 'Values for event params have to be String, int, double, or bool'); + 'Values for event params have to be String, int, double, or bool', + moreInfo: 'Value for $key is not a valid type for event: $eventName', + ); } // GA4 Limitation: @@ -83,16 +96,24 @@ void checkBody(Map body) { // or fewer, may only contain alpha-numeric characters and underscores, // and must start with an alphabetic character if (key.length > 40) { - throw AnalyticsException('Limit event param names to 40 chars or less'); + throw AnalyticsException( + 'Limit event param names to 40 chars or less', + moreInfo: 'The key: $key under the event: $eventName is too long', + ); } if (!alphaNumericPattern.hasMatch(key)) { throw AnalyticsException( 'Event param name can only have alphanumeric chars and underscores', + moreInfo: 'The key: $key under the event: $eventName contains ' + 'invalid characters', ); } if (!alphabeticPattern.hasMatch(key[0])) { throw AnalyticsException( - 'Event param name first char must be alphabetic char'); + 'Event param name first char must be alphabetic char', + moreInfo: 'The key: $key under the event: $eventName must begin ' + 'in a valid character', + ); } // GA4 Limitation: @@ -102,7 +123,9 @@ void checkBody(Map body) { value as String; if (value.length > 100) { throw AnalyticsException( - 'Limit characters in event param value to 100 chars or less'); + 'Limit characters in event param value to 100 chars or less', + moreInfo: 'Value for $key is too long, value=$value', + ); } } } @@ -122,7 +145,8 @@ void checkBody(Map body) { // GA4 Limitation: // User property names must be 24 characters or fewer if (key.length > 24) { - throw AnalyticsException('Limit user property names to 24 chars or less'); + throw AnalyticsException('Limit user property names to 24 chars or less', + moreInfo: 'The user property: $key has a value that is too long'); } // GA4 Limitation: @@ -130,7 +154,10 @@ void checkBody(Map body) { final userPropValue = value['value']; if (userPropValue is String && userPropValue.length > 36) { throw AnalyticsException( - 'Limit user property values to 36 chars or less'); + 'Limit user property values to 36 chars or less', + moreInfo: 'The value for the user property: $key which has ' + 'a value: $value is too long', + ); } } } @@ -138,5 +165,18 @@ void checkBody(Map body) { class AnalyticsException implements Exception { final String message; - AnalyticsException(this.message); + /// Optional field to pass if there is specific details + /// that can be included with the error message. + final String? moreInfo; + + AnalyticsException(this.message, {this.moreInfo}); + + @override + String toString() { + if (moreInfo == null) { + return 'AnalyticsException: $message'; + } + + return 'AnalyticsException: $message\nDetails:\n$moreInfo'; + } } From c8169b7b68a2bf20a4fa8b108358ad0ceb2f12a4 Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Fri, 13 Oct 2023 15:55:59 -0400 Subject: [PATCH 2/5] Use quotes to identify values --- pkgs/unified_analytics/lib/src/asserts.dart | 25 +++++++++++---------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/asserts.dart b/pkgs/unified_analytics/lib/src/asserts.dart index 4712a227e..6334feecf 100644 --- a/pkgs/unified_analytics/lib/src/asserts.dart +++ b/pkgs/unified_analytics/lib/src/asserts.dart @@ -45,19 +45,19 @@ void checkBody(Map body) { if (eventName.length > 40) { throw AnalyticsException( 'Limit event names to 40 chars or less', - moreInfo: 'Event name: $eventName is too long', + moreInfo: 'Event name: "$eventName" is too long', ); } if (!alphaNumericPattern.hasMatch(eventName)) { throw AnalyticsException( 'Event name can only have alphanumeric chars and underscores', - moreInfo: 'Event name: $eventName contains invalid characters', + moreInfo: 'Event name: "$eventName" contains invalid characters', ); } if (!alphabeticPattern.hasMatch(eventName[0])) { throw AnalyticsException( 'Event name first char must be alphabetic char', - moreInfo: 'Event name: $eventName must begin with a valid character', + moreInfo: 'Event name: "$eventName" must begin with a valid character', ); } @@ -68,7 +68,7 @@ void checkBody(Map body) { if (eventParams.length > 25) { throw AnalyticsException( 'Limit params for each event to less than 25', - moreInfo: 'Event: $eventName has too many parameters', + moreInfo: 'Event: "$eventName" has too many parameters', ); } @@ -87,7 +87,8 @@ void checkBody(Map body) { value is bool)) { throw AnalyticsException( 'Values for event params have to be String, int, double, or bool', - moreInfo: 'Value for $key is not a valid type for event: $eventName', + moreInfo: + 'Value for "$key" is not a valid type for event: "$eventName"', ); } @@ -98,20 +99,20 @@ void checkBody(Map body) { if (key.length > 40) { throw AnalyticsException( 'Limit event param names to 40 chars or less', - moreInfo: 'The key: $key under the event: $eventName is too long', + moreInfo: 'The key: "$key" under the event: "$eventName" is too long', ); } if (!alphaNumericPattern.hasMatch(key)) { throw AnalyticsException( 'Event param name can only have alphanumeric chars and underscores', - moreInfo: 'The key: $key under the event: $eventName contains ' + moreInfo: 'The key: "$key" under the event: "$eventName" contains ' 'invalid characters', ); } if (!alphabeticPattern.hasMatch(key[0])) { throw AnalyticsException( 'Event param name first char must be alphabetic char', - moreInfo: 'The key: $key under the event: $eventName must begin ' + moreInfo: 'The key: "$key" under the event: "$eventName" must begin ' 'in a valid character', ); } @@ -124,7 +125,7 @@ void checkBody(Map body) { if (value.length > 100) { throw AnalyticsException( 'Limit characters in event param value to 100 chars or less', - moreInfo: 'Value for $key is too long, value=$value', + moreInfo: 'Value for "$key" is too long, value="$value"', ); } } @@ -146,7 +147,7 @@ void checkBody(Map body) { // User property names must be 24 characters or fewer if (key.length > 24) { throw AnalyticsException('Limit user property names to 24 chars or less', - moreInfo: 'The user property: $key has a value that is too long'); + moreInfo: 'The user property key: "$key" is too long'); } // GA4 Limitation: @@ -155,8 +156,8 @@ void checkBody(Map body) { if (userPropValue is String && userPropValue.length > 36) { throw AnalyticsException( 'Limit user property values to 36 chars or less', - moreInfo: 'The value for the user property: $key which has ' - 'a value: $value is too long', + moreInfo: 'The value for the user property: "$key" which has ' + 'a value: "${value['value']}" is too long', ); } } From 225f5e729995a50f6f96b19db905cc20cc2d8ba3 Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Mon, 16 Oct 2023 12:22:35 -0400 Subject: [PATCH 3/5] Update to exception class to remove optional param --- pkgs/unified_analytics/lib/src/asserts.dart | 65 ++++++++----------- pkgs/unified_analytics/test/asserts_test.dart | 47 ++++++++++---- 2 files changed, 62 insertions(+), 50 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/asserts.dart b/pkgs/unified_analytics/lib/src/asserts.dart index 6334feecf..b07ef6a8d 100644 --- a/pkgs/unified_analytics/lib/src/asserts.dart +++ b/pkgs/unified_analytics/lib/src/asserts.dart @@ -44,20 +44,20 @@ void checkBody(Map body) { // with an alphabetic character if (eventName.length > 40) { throw AnalyticsException( - 'Limit event names to 40 chars or less', - moreInfo: 'Event name: "$eventName" is too long', + 'Limit event names to 40 chars or less\n' + 'Event name: "$eventName" is too long', ); } if (!alphaNumericPattern.hasMatch(eventName)) { throw AnalyticsException( - 'Event name can only have alphanumeric chars and underscores', - moreInfo: 'Event name: "$eventName" contains invalid characters', + 'Event name can only have alphanumeric chars and underscores\n' + 'Event name: "$eventName" contains invalid characters', ); } if (!alphabeticPattern.hasMatch(eventName[0])) { throw AnalyticsException( - 'Event name first char must be alphabetic char', - moreInfo: 'Event name: "$eventName" must begin with a valid character', + 'Event name first char must be alphabetic char\n' + 'Event name: "$eventName" must begin with a valid character', ); } @@ -67,8 +67,8 @@ void checkBody(Map body) { // Events can have a maximum of 25 parameters if (eventParams.length > 25) { throw AnalyticsException( - 'Limit params for each event to less than 25', - moreInfo: 'Event: "$eventName" has too many parameters', + 'Limit params for each event to less than 25\n' + 'Event: "$eventName" has too many parameters', ); } @@ -86,9 +86,8 @@ void checkBody(Map body) { value is double || value is bool)) { throw AnalyticsException( - 'Values for event params have to be String, int, double, or bool', - moreInfo: - 'Value for "$key" is not a valid type for event: "$eventName"', + 'Values for event params have to be String, int, double, or bool\n' + 'Value for "$key" is not a valid type for event: "$eventName"', ); } @@ -98,22 +97,22 @@ void checkBody(Map body) { // and must start with an alphabetic character if (key.length > 40) { throw AnalyticsException( - 'Limit event param names to 40 chars or less', - moreInfo: 'The key: "$key" under the event: "$eventName" is too long', + 'Limit event param names to 40 chars or less\n' + 'The key: "$key" under the event: "$eventName" is too long', ); } if (!alphaNumericPattern.hasMatch(key)) { throw AnalyticsException( - 'Event param name can only have alphanumeric chars and underscores', - moreInfo: 'The key: "$key" under the event: "$eventName" contains ' - 'invalid characters', + 'Event param name can only have alphanumeric chars and underscores\n' + 'The key: "$key" under the event: "$eventName" contains ' + 'invalid characters', ); } if (!alphabeticPattern.hasMatch(key[0])) { throw AnalyticsException( - 'Event param name first char must be alphabetic char', - moreInfo: 'The key: "$key" under the event: "$eventName" must begin ' - 'in a valid character', + 'Event param name first char must be alphabetic char\n' + 'The key: "$key" under the event: "$eventName" must begin ' + 'in a valid character', ); } @@ -124,8 +123,8 @@ void checkBody(Map body) { value as String; if (value.length > 100) { throw AnalyticsException( - 'Limit characters in event param value to 100 chars or less', - moreInfo: 'Value for "$key" is too long, value="$value"', + 'Limit characters in event param value to 100 chars or less\n' + 'Value for "$key" is too long, value="$value"', ); } } @@ -146,8 +145,8 @@ void checkBody(Map body) { // GA4 Limitation: // User property names must be 24 characters or fewer if (key.length > 24) { - throw AnalyticsException('Limit user property names to 24 chars or less', - moreInfo: 'The user property key: "$key" is too long'); + throw AnalyticsException('Limit user property names to 24 chars or less\n' + 'The user property key: "$key" is too long'); } // GA4 Limitation: @@ -155,9 +154,9 @@ void checkBody(Map body) { final userPropValue = value['value']; if (userPropValue is String && userPropValue.length > 36) { throw AnalyticsException( - 'Limit user property values to 36 chars or less', - moreInfo: 'The value for the user property: "$key" which has ' - 'a value: "${value['value']}" is too long', + 'Limit user property values to 36 chars or less\n' + 'The value for the user property: "$key" which has ' + 'a value: "${value['value']}" is too long', ); } } @@ -166,18 +165,8 @@ void checkBody(Map body) { class AnalyticsException implements Exception { final String message; - /// Optional field to pass if there is specific details - /// that can be included with the error message. - final String? moreInfo; - - AnalyticsException(this.message, {this.moreInfo}); + AnalyticsException(this.message); @override - String toString() { - if (moreInfo == null) { - return 'AnalyticsException: $message'; - } - - return 'AnalyticsException: $message\nDetails:\n$moreInfo'; - } + String toString() => 'AnalyticsException: $message'; } diff --git a/pkgs/unified_analytics/test/asserts_test.dart b/pkgs/unified_analytics/test/asserts_test.dart index cd0103375..9394dc3a6 100644 --- a/pkgs/unified_analytics/test/asserts_test.dart +++ b/pkgs/unified_analytics/test/asserts_test.dart @@ -72,7 +72,10 @@ void main() { 'user_properties': {} }; - final expectedErrorMessage = 'Limit event names to 40 chars or less'; + final expectedErrorMessage = 'Limit event names to 40 chars or less\n' + 'Event name: ' + '"hot_reload_timehot_reload_timehot_reload_timehot_reload_time"' + ' is too long'; expect( () => checkBody(body), throwsA(predicate( @@ -93,7 +96,8 @@ void main() { }; final expectedErrorMessage = - 'Event name can only have alphanumeric chars and underscores'; + 'Event name can only have alphanumeric chars and underscores\n' + 'Event name: "hot_reload_time!!" contains invalid characters'; expect( () => checkBody(body), throwsA(predicate( @@ -114,7 +118,8 @@ void main() { }; final expectedErrorMessage = - 'Event name first char must be alphabetic char'; + 'Event name first char must be alphabetic char\n' + 'Event name: "2hot_reload_time" must begin with a valid character'; expect( () => checkBody(body), throwsA(predicate( @@ -142,7 +147,8 @@ void main() { // Add the params to the first event in the body ((body['events'] as List).first as Map)['params'] = params; - final expectedErrorMessage = 'Limit params for each event to less than 25'; + final expectedErrorMessage = 'Limit params for each event to less than 25\n' + 'Event: "hot_reload_time" has too many parameters'; expect( () => checkBody(body), throwsA(predicate( @@ -168,7 +174,8 @@ void main() { }; final expectedErrorMessage = - 'Values for event params have to be String, int, double, or bool'; + 'Values for event params have to be String, int, double, or bool\n' + 'Value for "count" is not a valid type for event: "hot_reload_time"'; expect( () => checkBody(body), throwsA(predicate( @@ -194,7 +201,8 @@ void main() { }; final expectedErrorMessage = - 'Values for event params have to be String, int, double, or bool'; + 'Values for event params have to be String, int, double, or bool\n' + 'Value for "count" is not a valid type for event: "hot_reload_time"'; expect( () => checkBody(body), throwsA(predicate( @@ -214,7 +222,9 @@ void main() { 'user_properties': {} }; - final expectedErrorMessage = 'Limit event param names to 40 chars or less'; + final expectedErrorMessage = 'Limit event param names to 40 chars or less\n' + 'The key: "time_mstime_mstime_mstime_mstime_mstime_ms" ' + 'under the event: "hot_reload_time" is too long'; expect( () => checkBody(body), throwsA(predicate( @@ -235,7 +245,10 @@ void main() { }; final expectedErrorMessage = - 'Event param name can only have alphanumeric chars and underscores'; + 'Event param name can only have alphanumeric chars and underscores\n' + 'The key: "time_ns!" under the event: "hot_reload_time" contains ' + 'invalid characters'; + expect( () => checkBody(body), throwsA(predicate( @@ -258,7 +271,9 @@ void main() { }; final expectedErrorMessage = - 'Event param name first char must be alphabetic char'; + 'Event param name first char must be alphabetic char\n' + 'The key: "22time_ns" under the event: "hot_reload_time" must begin ' + 'in a valid character'; expect( () => checkBody(body), throwsA(predicate( @@ -285,7 +300,12 @@ void main() { }; final expectedErrorMessage = - 'Limit characters in event param value to 100 chars or less'; + 'Limit characters in event param value to 100 chars or less\n' + 'Value for "time_ns" is too long, value="' + 'dsfjlksdjfajlfdsfjlks' + 'djfajlfdsfjlksdjfajlfdsfjlksdjfaj' + 'lfdsfjlksdjfajlfdsfjlksdjfajlfdsf' + 'jlksdjfajlfdsfjlksdjfajlf"'; expect( () => checkBody(body), throwsA(predicate( @@ -332,7 +352,8 @@ void main() { }; final expectedErrorMessage = - 'Limit user property names to 24 chars or less'; + 'Limit user property names to 24 chars or less\n' + 'The user property key: "testtesttesttesttesttesttest" is too long'; expect( () => checkBody(body), throwsA(predicate( @@ -357,7 +378,9 @@ void main() { }; final expectedErrorMessage = - 'Limit user property values to 36 chars or less'; + 'Limit user property values to 36 chars or less\n' + 'The value for the user property: "test" which has ' + 'a value: "testtesttesttesttesttesttesttesttesttest" is too long'; expect( () => checkBody(body), throwsA(predicate( From bcca738ced45ee7c3a5f205ba3cbee577b0a0482 Mon Sep 17 00:00:00 2001 From: Elias Yishak <42216813+eliasyishak@users.noreply.github.com> Date: Tue, 17 Oct 2023 10:55:56 -0400 Subject: [PATCH 4/5] Nit fix Co-authored-by: Christopher Fujino --- pkgs/unified_analytics/lib/src/asserts.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/asserts.dart b/pkgs/unified_analytics/lib/src/asserts.dart index b07ef6a8d..dcd3492aa 100644 --- a/pkgs/unified_analytics/lib/src/asserts.dart +++ b/pkgs/unified_analytics/lib/src/asserts.dart @@ -155,8 +155,7 @@ void checkBody(Map body) { if (userPropValue is String && userPropValue.length > 36) { throw AnalyticsException( 'Limit user property values to 36 chars or less\n' - 'The value for the user property: "$key" which has ' - 'a value: "${value['value']}" is too long', + 'For the user property key "$key", the value "$value" is too long', ); } } From 5d4c947c6626a2ced476425426cdc957b091c2ff Mon Sep 17 00:00:00 2001 From: eliasyishak <42216813+eliasyishak@users.noreply.github.com> Date: Tue, 17 Oct 2023 11:00:15 -0400 Subject: [PATCH 5/5] Fix tests --- pkgs/unified_analytics/lib/src/asserts.dart | 3 ++- pkgs/unified_analytics/test/asserts_test.dart | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pkgs/unified_analytics/lib/src/asserts.dart b/pkgs/unified_analytics/lib/src/asserts.dart index dcd3492aa..1ba620129 100644 --- a/pkgs/unified_analytics/lib/src/asserts.dart +++ b/pkgs/unified_analytics/lib/src/asserts.dart @@ -155,7 +155,8 @@ void checkBody(Map body) { if (userPropValue is String && userPropValue.length > 36) { throw AnalyticsException( 'Limit user property values to 36 chars or less\n' - 'For the user property key "$key", the value "$value" is too long', + 'For the user property key "$key", the value "${value['value']}" ' + 'is too long', ); } } diff --git a/pkgs/unified_analytics/test/asserts_test.dart b/pkgs/unified_analytics/test/asserts_test.dart index 9394dc3a6..9230fc6ae 100644 --- a/pkgs/unified_analytics/test/asserts_test.dart +++ b/pkgs/unified_analytics/test/asserts_test.dart @@ -379,8 +379,8 @@ void main() { final expectedErrorMessage = 'Limit user property values to 36 chars or less\n' - 'The value for the user property: "test" which has ' - 'a value: "testtesttesttesttesttesttesttesttesttest" is too long'; + 'For the user property key "test", the value ' + '"testtesttesttesttesttesttesttesttesttest" is too long'; expect( () => checkBody(body), throwsA(predicate(