Skip to content

Commit

Permalink
[Vega] Improve error message in case of invalid $schema URL (elastic#…
Browse files Browse the repository at this point in the history
…114459)

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

* 🌐 Add i18n
  • Loading branch information
dej611 authored Oct 12, 2021
1 parent df971b7 commit ba8abc4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
14 changes: 14 additions & 0 deletions src/plugins/vis_types/vega/public/data_model/vega_parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`, () => {
Expand Down
40 changes: 26 additions & 14 deletions src/plugins/vis_types/vega/public/data_model/vega_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

/**
Expand Down

0 comments on commit ba8abc4

Please sign in to comment.