From eadd30eb6cd3c2b0ad2f098d0087e7c990403b5b Mon Sep 17 00:00:00 2001 From: Shi-Hao Hong Date: Thu, 7 May 2020 18:09:03 -0700 Subject: [PATCH] Improve arb FormatException error message (#56373) --- dev/tools/localization/gen_l10n_types.dart | 10 +++- .../test/localization/gen_l10n_test.dart | 58 ++++++++++--------- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/dev/tools/localization/gen_l10n_types.dart b/dev/tools/localization/gen_l10n_types.dart index a8853ddd34..2007e4b577 100644 --- a/dev/tools/localization/gen_l10n_types.dart +++ b/dev/tools/localization/gen_l10n_types.dart @@ -363,8 +363,16 @@ class AppResourceBundle { factory AppResourceBundle(File file) { assert(file != null); // Assuming that the caller has verified that the file exists and is readable. + Map resources; + try { + resources = json.decode(file.readAsStringSync()) as Map; + } on FormatException catch (e) { + throw L10nException( + 'The arb file ${file.path} has the following formatting issue: \n' + '${e.toString()}', + ); + } - final Map resources = json.decode(file.readAsStringSync()) as Map; String localeString = resources['@@locale'] as String; if (localeString == null) { final RegExp filenameRE = RegExp(r'^[^_]*_(\w+)\.arb$'); diff --git a/dev/tools/test/localization/gen_l10n_test.dart b/dev/tools/test/localization/gen_l10n_test.dart index 8748a221c1..ad6943f563 100644 --- a/dev/tools/test/localization/gen_l10n_test.dart +++ b/dev/tools/test/localization/gen_l10n_test.dart @@ -1284,40 +1284,46 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e }); }); - test('should throw when failing to parse the arb file', () { - const String arbFileWithTrailingComma = ''' + test( + 'should throw with descriptive error message when failing to parse the ' + 'arb file', + () { + const String arbFileWithTrailingComma = ''' { "title": "Stocks", "@title": { "description": "Title for the Stocks application" }, }'''; - final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n') - ..createSync(recursive: true); - l10nDirectory.childFile(defaultTemplateArbFileName) - .writeAsStringSync(arbFileWithTrailingComma); + final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n') + ..createSync(recursive: true); + l10nDirectory.childFile(defaultTemplateArbFileName) + .writeAsStringSync(arbFileWithTrailingComma); - final LocalizationsGenerator generator = LocalizationsGenerator(fs); - try { - generator.initialize( - inputPathString: defaultL10nPathString, - outputPathString: defaultL10nPathString, - templateArbFileName: defaultTemplateArbFileName, - outputFileString: defaultOutputFileString, - classNameString: defaultClassNameString, + final LocalizationsGenerator generator = LocalizationsGenerator(fs); + try { + generator.initialize( + inputPathString: defaultL10nPathString, + outputPathString: defaultL10nPathString, + templateArbFileName: defaultTemplateArbFileName, + outputFileString: defaultOutputFileString, + classNameString: defaultClassNameString, + ); + generator.loadResources(); + generator.writeOutputFiles(); + } on L10nException catch (e) { + expect(e.message, contains('app_en.arb')); + expect(e.message, contains('FormatException')); + expect(e.message, contains('Unexpected character')); + return; + } + + fail( + 'should fail with an L10nException due to a trailing comma in the ' + 'arb file.' ); - generator.loadResources(); - generator.writeOutputFiles(); - } on FormatException catch (e) { - expect(e.message, contains('Unexpected character')); - return; - } - - fail( - 'should fail with a FormatException due to a trailing comma in the ' - 'arb file.' - ); - }); + }, + ); test('should throw when resource is missing resource attribute', () { const String arbFileWithMissingResourceAttribute = '''