Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more info into the AnalyticsException #181

Merged
merged 5 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions pkgs/unified_analytics/lib/src/asserts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,33 @@ void checkBody(Map<String, Object?> 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<String, Object?>;

// 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
Expand All @@ -75,24 +86,34 @@ void checkBody(Map<String, Object?> 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:
// Parameter names (including item parameters) must be 40 characters
// 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:
Expand All @@ -102,7 +123,9 @@ void checkBody(Map<String, Object?> 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"',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slight re-word for better clarity

Suggested change
'Value for "$key" is too long, value="$value"',
'For key "$key" the value "$value" is too long',

);
}
}
}
Expand All @@ -122,15 +145,19 @@ void checkBody(Map<String, Object?> 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:
// User property values must be 36 characters or fewer
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',
);
}
}
}
Expand All @@ -139,4 +166,7 @@ class AnalyticsException implements Exception {
final String message;

AnalyticsException(this.message);

@override
String toString() => 'AnalyticsException: $message';
}
47 changes: 35 additions & 12 deletions pkgs/unified_analytics/test/asserts_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ void main() {
'user_properties': <String, Object?>{}
};

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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -214,7 +222,9 @@ void main() {
'user_properties': <String, Object?>{}
};

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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down