diff --git a/src/platform/plugins/private/vis_types/vega/public/data_model/utils.ts b/src/platform/plugins/private/vis_types/vega/public/data_model/utils.ts index 0b5ba9f1b509b..099fb7a659d02 100644 --- a/src/platform/plugins/private/vis_types/vega/public/data_model/utils.ts +++ b/src/platform/plugins/private/vis_types/vega/public/data_model/utils.ts @@ -10,34 +10,33 @@ import compactStringify from 'json-stringify-pretty-compact'; import { CoreTheme } from '@kbn/core/public'; import { getEuiThemeVars } from '@kbn/ui-theme'; +import { normalizeObject } from '../vega_view/utils'; + +function normalizeAndStringify(value: unknown) { + if (typeof value === 'string') { + return value; + } + return compactStringify(normalizeObject(value), { maxLength: 70 }); +} export class Utils { /** * If the 2nd array parameter in args exists, append it to the warning/error string value */ - static formatWarningToStr(...args: any[]) { - let value = args[0]; + static formatWarningToStr(...args: any[]): string { + const value = normalizeAndStringify(args[0]); if (args.length >= 2) { try { - if (typeof args[1] === 'string') { - value += `\n${args[1]}`; - } else { - value += '\n' + compactStringify(args[1], { maxLength: 70 }); - } + return `${value}\n${normalizeAndStringify(args[1])}`; } catch (err) { - // ignore + return Utils.formatErrorToStr(err); } } return value; } - static formatErrorToStr(...args: any[]) { - let error: Error | string = args[0]; - if (!error) { - error = 'ERR'; - } else if (error instanceof Error) { - error = error.message; - } + static formatErrorToStr(...args: unknown[]) { + const error: string = args[0] instanceof Error ? args[0].message : 'Error'; return Utils.formatWarningToStr(error, ...Array.from(args).slice(1)); } } diff --git a/src/platform/plugins/private/vis_types/vega/public/data_model/vega_parser.ts b/src/platform/plugins/private/vis_types/vega/public/data_model/vega_parser.ts index 762a3830dc25b..b87403e357f30 100644 --- a/src/platform/plugins/private/vis_types/vega/public/data_model/vega_parser.ts +++ b/src/platform/plugins/private/vis_types/vega/public/data_model/vega_parser.ts @@ -271,8 +271,11 @@ The URL is an identifier only. Kibana and your browser will never access this UR } } this.vlspec = this.spec; - const vegaLogger = logger(Warn); // note: eslint has a false positive here - vegaLogger.warn = this._onWarning.bind(this); + const vegaLogger = logger(Warn); + vegaLogger.warn = (...args) => { + this._onWarning(...args); + return vegaLogger; + }; this.spec = compile(this.vlspec as TopLevelSpec, { logger: vegaLogger }).spec; // When using Vega-Lite (VL) with the type=map and user did not provid their own projection settings, @@ -775,8 +778,8 @@ The URL is an identifier only. Kibana and your browser will never access this UR */ _onWarning(...args: any[]) { if (!this.hideWarnings) { - this.warnings.push(Utils.formatWarningToStr(args)); - return Utils.formatWarningToStr(args); + this.warnings.push(Utils.formatWarningToStr(...args)); + return Utils.formatWarningToStr(...args); } } } diff --git a/src/platform/plugins/private/vis_types/vega/public/vega_view/utils.test.ts b/src/platform/plugins/private/vis_types/vega/public/vega_view/utils.test.ts index 74c166daa1a88..5d59efbb753e4 100644 --- a/src/platform/plugins/private/vis_types/vega/public/vega_view/utils.test.ts +++ b/src/platform/plugins/private/vis_types/vega/public/vega_view/utils.test.ts @@ -48,13 +48,13 @@ describe('normalizeObject', () => { test('should throw if a function is given as the object property', async () => { expect(() => { normalizeObject({ toJSON: () => alert('gotcha') }); - }).toThrow('a function cannot be used as a property name'); + }).toThrow('Object normalization error'); }); test('should throw if a function is given on a nested object', async () => { expect(() => { normalizeObject({ test: { toJSON: () => alert('gotcha') } }); - }).toThrow('a function cannot be used as a property name'); + }).toThrow('Object normalization error'); }); test('should return null for null', async () => { diff --git a/src/platform/plugins/private/vis_types/vega/public/vega_view/utils.ts b/src/platform/plugins/private/vis_types/vega/public/vega_view/utils.ts index 3b59709d5bfe8..0f54a826711b2 100644 --- a/src/platform/plugins/private/vis_types/vega/public/vega_view/utils.ts +++ b/src/platform/plugins/private/vis_types/vega/public/vega_view/utils.ts @@ -48,7 +48,9 @@ export function checkObjectForFunctionProperty(object: unknown): boolean { */ export function normalizeObject(object: unknown) { if (checkObjectForFunctionProperty(object)) { - throw new Error('a function cannot be used as a property name'); + throw new Error('Object normalization error', { + cause: 'A function cannot be used as a property name', + }); } const normalizedObject = object ? JSON.parse(JSON.stringify(object)) : null; ensureNoUnsafeProperties(normalizedObject); diff --git a/src/platform/plugins/private/vis_types/vega/public/vega_view/vega_base_view.js b/src/platform/plugins/private/vis_types/vega/public/vega_view/vega_base_view.js index c4f561642fc63..c6bd22ba4da9c 100644 --- a/src/platform/plugins/private/vis_types/vega/public/vega_view/vega_base_view.js +++ b/src/platform/plugins/private/vis_types/vega/public/vega_view/vega_base_view.js @@ -241,23 +241,29 @@ export class VegaBaseView { const vegaLogger = logger(Warn); - vegaLogger.warn = this.onWarn.bind(this); - vegaLogger.error = this.onError.bind(this); + vegaLogger.warn = (...args) => { + this.onWarn(...args); + return vegaLogger; + }; + vegaLogger.error = (...args) => { + this.onError(...args); + return vegaLogger; + }; config.logger = vegaLogger; return config; } - onError() { - const error = Utils.formatErrorToStr(...arguments); + onError(...args) { + const error = Utils.formatErrorToStr(...args); this._addMessage('err', error); this._parser.searchAPI.inspectorAdapters?.vega.setError(error); } - onWarn() { + onWarn(...args) { if (this._renderMode !== 'view' && (!this._parser || !this._parser.hideWarnings)) { - this._addMessage('warn', Utils.formatWarningToStr(...arguments)); + this._addMessage('warn', Utils.formatWarningToStr(...args)); } }