Unverified Commit eadd30eb authored by Shi-Hao Hong's avatar Shi-Hao Hong Committed by GitHub

Improve arb FormatException error message (#56373)

parent 26dc70dc
...@@ -363,8 +363,16 @@ class AppResourceBundle { ...@@ -363,8 +363,16 @@ class AppResourceBundle {
factory AppResourceBundle(File file) { factory AppResourceBundle(File file) {
assert(file != null); assert(file != null);
// Assuming that the caller has verified that the file exists and is readable. // Assuming that the caller has verified that the file exists and is readable.
Map<String, dynamic> resources;
try {
resources = json.decode(file.readAsStringSync()) as Map<String, dynamic>;
} on FormatException catch (e) {
throw L10nException(
'The arb file ${file.path} has the following formatting issue: \n'
'${e.toString()}',
);
}
final Map<String, dynamic> resources = json.decode(file.readAsStringSync()) as Map<String, dynamic>;
String localeString = resources['@@locale'] as String; String localeString = resources['@@locale'] as String;
if (localeString == null) { if (localeString == null) {
final RegExp filenameRE = RegExp(r'^[^_]*_(\w+)\.arb$'); final RegExp filenameRE = RegExp(r'^[^_]*_(\w+)\.arb$');
......
...@@ -1284,7 +1284,10 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e ...@@ -1284,7 +1284,10 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
}); });
}); });
test('should throw when failing to parse the arb file', () { test(
'should throw with descriptive error message when failing to parse the '
'arb file',
() {
const String arbFileWithTrailingComma = ''' const String arbFileWithTrailingComma = '''
{ {
"title": "Stocks", "title": "Stocks",
...@@ -1308,16 +1311,19 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e ...@@ -1308,16 +1311,19 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
); );
generator.loadResources(); generator.loadResources();
generator.writeOutputFiles(); generator.writeOutputFiles();
} on FormatException catch (e) { } on L10nException catch (e) {
expect(e.message, contains('app_en.arb'));
expect(e.message, contains('FormatException'));
expect(e.message, contains('Unexpected character')); expect(e.message, contains('Unexpected character'));
return; return;
} }
fail( fail(
'should fail with a FormatException due to a trailing comma in the ' 'should fail with an L10nException due to a trailing comma in the '
'arb file.' 'arb file.'
); );
}); },
);
test('should throw when resource is missing resource attribute', () { test('should throw when resource is missing resource attribute', () {
const String arbFileWithMissingResourceAttribute = ''' const String arbFileWithMissingResourceAttribute = '''
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment