[Vega] Improve error message in case of invalid $schema URL (#114459)

* 🐛 Catch the schema parser and provide a better error message

* 🌐 Add i18n
This commit is contained in:
Marco Liberati 2021-10-12 16:58:37 +02:00 committed by GitHub
parent df971b7dc9
commit ba8abc4151
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 14 deletions

View file

@ -81,6 +81,20 @@ describe(`VegaParser.parseAsync`, () => {
})
)
);
test(`should return a specific error in case of $schema URL not valid`, async () => {
const vp = new VegaParser({
$schema: 'https://vega.github.io/schema/vega-lite/v4.jsonanythingtobreakthis',
mark: 'circle',
encoding: { row: { field: 'a' } },
});
await vp.parseAsync();
expect(vp.error).toBe(
'The URL for the JSON "$schema" is incorrect. Correct the URL, then click Update.'
);
});
});
describe(`VegaParser._setDefaultValue`, () => {

View file

@ -553,25 +553,37 @@ The URL is an identifier only. Kibana and your browser will never access this UR
* @private
*/
private parseSchema(spec: VegaSpec) {
const schema = schemaParser(spec.$schema);
const isVegaLite = schema.library === 'vega-lite';
const libVersion = isVegaLite ? vegaLiteVersion : vegaVersion;
try {
const schema = schemaParser(spec.$schema);
const isVegaLite = schema.library === 'vega-lite';
const libVersion = isVegaLite ? vegaLiteVersion : vegaVersion;
if (versionCompare(schema.version, libVersion) > 0) {
this._onWarning(
i18n.translate('visTypeVega.vegaParser.notValidLibraryVersionForInputSpecWarningMessage', {
if (versionCompare(schema.version, libVersion) > 0) {
this._onWarning(
i18n.translate(
'visTypeVega.vegaParser.notValidLibraryVersionForInputSpecWarningMessage',
{
defaultMessage:
'The input spec uses {schemaLibrary} {schemaVersion}, but current version of {schemaLibrary} is {libraryVersion}.',
values: {
schemaLibrary: schema.library,
schemaVersion: schema.version,
libraryVersion: libVersion,
},
}
)
);
}
return { isVegaLite, libVersion };
} catch (e) {
throw Error(
i18n.translate('visTypeVega.vegaParser.notValidSchemaForInputSpec', {
defaultMessage:
'The input spec uses {schemaLibrary} {schemaVersion}, but current version of {schemaLibrary} is {libraryVersion}.',
values: {
schemaLibrary: schema.library,
schemaVersion: schema.version,
libraryVersion: libVersion,
},
'The URL for the JSON "$schema" is incorrect. Correct the URL, then click Update.',
})
);
}
return { isVegaLite, libVersion };
}
/**