diff --git a/pkgs/unified_analytics/lib/src/asserts.dart b/pkgs/unified_analytics/lib/src/asserts.dart index 715c3c48a..1ba620129 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\n' + '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\n' + '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\n' + '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\n' + '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\n' + '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\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', + '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'); + 'Event param name first char must be alphabetic char\n' + '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\n' + '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\n' + 'The user property key: "$key" 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\n' + 'For the user property key "$key", the value "${value['value']}" ' + 'is too long', + ); } } } @@ -139,4 +166,7 @@ class AnalyticsException implements Exception { final String message; AnalyticsException(this.message); + + @override + String toString() => 'AnalyticsException: $message'; } diff --git a/pkgs/unified_analytics/test/asserts_test.dart b/pkgs/unified_analytics/test/asserts_test.dart index cd0103375..9230fc6ae 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' + 'For the user property key "test", the value ' + '"testtesttesttesttesttesttesttesttesttest" is too long'; expect( () => checkBody(body), throwsA(predicate(