Skip to content

Commit

Permalink
Clarify error messaging if 'isPubliclyHosted' key is missing (#6660)
Browse files Browse the repository at this point in the history
  • Loading branch information
kenzieschmoll authored Nov 3, 2023
1 parent d09ba71 commit 4370128
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
32 changes: 23 additions & 9 deletions packages/devtools_shared/lib/src/extensions/extension_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,34 @@ class DevToolsExtensionConfig implements Comparable {
isPubliclyHosted: bool.parse(isPubliclyHosted),
);
} else {
const requiredKeys = {
if (!json.keys.contains(isPubliclyHostedKey)) {
throw StateError(
'Missing key "$isPubliclyHostedKey" when trying to parse '
'DevToolsExtensionConfig object.',
);
}

const requiredKeysFromConfigFile = {
nameKey,
pathKey,
issueTrackerKey,
versionKey,
isPubliclyHostedKey,
};
final diff = requiredKeys.difference(json.keys.toSet());
if (diff.isEmpty) {
// We do not expect the config.yaml file to contain
// [isPubliclyHostedKey], as this should be inferred.
final jsonKeysFromConfigFile = Set.of(json.keys.toSet())
..remove(isPubliclyHostedKey);

final diff = requiredKeysFromConfigFile.difference(
jsonKeysFromConfigFile,
);

if (diff.isNotEmpty) {
throw StateError(
'Missing required fields ${diff.toString()} in the extension '
'config.yaml.',
);
} else {
// All the required keys are present, but the value types did not match.
final sb = StringBuffer();
for (final entry in json.entries) {
Expand All @@ -66,11 +85,6 @@ class DevToolsExtensionConfig implements Comparable {
'values to be of type String, but one or more had a different type:\n'
'${sb.toString()}',
);
} else {
throw StateError(
'Missing required fields ${diff.toString()} in the extension '
'config.yaml.',
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ void main() {
);
}

Matcher throwsMissingIsPubliclyHostedError() {
return throwsA(
isA<StateError>().having(
(e) => e.message,
'missing isPubliclyHosted key StateError',
startsWith('Missing key "isPubliclyHosted"'),
),
);
}

// Missing 'name'.
expect(
() {
Expand Down Expand Up @@ -130,7 +140,7 @@ void main() {
'issueTracker': 'www.google.com',
});
},
throwsMissingRequiredFieldsError(),
throwsMissingIsPubliclyHostedError(),
);
});

Expand Down

0 comments on commit 4370128

Please sign in to comment.