Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would call it spec "$schema", as it's not necessarily JSON.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the $schema value cannot be anything else than a URL ending with json (source: https://github.com/vega/schema/blob/55615e9ec4d1aeae002799b90dff60aad9f31b19/parser.ts#L5 )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn’t make this connection. Looks good to me then 👍

})
);
}

return { isVegaLite, libVersion };
}

/**
Expand Down