Skip to content

Commit 48bbea9

Browse files
[9.2] [Vega] add object normalization (#238503) (#240017)
# Backport This will backport the following commits from `main` to `9.2`: - [[Vega] add object normalization (#238503)](#238503) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Marco Vettorello","email":"marco.vettorello@elastic.co"},"sourceCommit":{"committedDate":"2025-10-22T07:44:33Z","message":"[Vega] add object normalization (#238503)\n\n## Summary\n\nAdd object normalization to Vega warning/error messages","sha":"4d51ac82208dde420739bcc9ffb0c762e6192d20","branchLabelMapping":{"^v9.3.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","Team:Visualizations","release_note:skip","ci:build-os-packages","backport:all-open","v9.3.0"],"title":"[Vega] add object normalization","number":238503,"url":"https://github.com/elastic/kibana/pull/238503","mergeCommit":{"message":"[Vega] add object normalization (#238503)\n\n## Summary\n\nAdd object normalization to Vega warning/error messages","sha":"4d51ac82208dde420739bcc9ffb0c762e6192d20"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.3.0","branchLabelMappingKey":"^v9.3.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/238503","number":238503,"mergeCommit":{"message":"[Vega] add object normalization (#238503)\n\n## Summary\n\nAdd object normalization to Vega warning/error messages","sha":"4d51ac82208dde420739bcc9ffb0c762e6192d20"}}]}] BACKPORT--> Co-authored-by: Marco Vettorello <marco.vettorello@elastic.co>
1 parent b34631e commit 48bbea9

5 files changed

Lines changed: 38 additions & 28 deletions

File tree

src/platform/plugins/private/vis_types/vega/public/data_model/utils.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,33 @@
1010
import compactStringify from 'json-stringify-pretty-compact';
1111
import type { CoreTheme } from '@kbn/core/public';
1212
import { getEuiThemeVars } from '@kbn/ui-theme';
13+
import { normalizeObject } from '../vega_view/utils';
14+
15+
function normalizeAndStringify(value: unknown) {
16+
if (typeof value === 'string') {
17+
return value;
18+
}
19+
return compactStringify(normalizeObject(value), { maxLength: 70 });
20+
}
1321

1422
export class Utils {
1523
/**
1624
* If the 2nd array parameter in args exists, append it to the warning/error string value
1725
*/
18-
static formatWarningToStr(...args: any[]) {
19-
let value = args[0];
26+
static formatWarningToStr(...args: any[]): string {
27+
const value = normalizeAndStringify(args[0]);
2028
if (args.length >= 2) {
2129
try {
22-
if (typeof args[1] === 'string') {
23-
value += `\n${args[1]}`;
24-
} else {
25-
value += '\n' + compactStringify(args[1], { maxLength: 70 });
26-
}
30+
return `${value}\n${normalizeAndStringify(args[1])}`;
2731
} catch (err) {
28-
// ignore
32+
return Utils.formatErrorToStr(err);
2933
}
3034
}
3135
return value;
3236
}
3337

34-
static formatErrorToStr(...args: any[]) {
35-
let error: Error | string = args[0];
36-
if (!error) {
37-
error = 'ERR';
38-
} else if (error instanceof Error) {
39-
error = error.message;
40-
}
38+
static formatErrorToStr(...args: unknown[]) {
39+
const error: string = args[0] instanceof Error ? args[0].message : 'Error';
4140
return Utils.formatWarningToStr(error, ...Array.from(args).slice(1));
4241
}
4342
}

src/platform/plugins/private/vis_types/vega/public/data_model/vega_parser.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,11 @@ The URL is an identifier only. Kibana and your browser will never access this UR
272272
}
273273
}
274274
this.vlspec = this.spec;
275-
const vegaLogger = logger(Warn); // note: eslint has a false positive here
276-
vegaLogger.warn = this._onWarning.bind(this);
275+
const vegaLogger = logger(Warn);
276+
vegaLogger.warn = (...args) => {
277+
this._onWarning(...args);
278+
return vegaLogger;
279+
};
277280
this.spec = compile(this.vlspec as TopLevelSpec, { logger: vegaLogger }).spec;
278281

279282
// When using Vega-Lite (VL) with the type=map and user did not provid their own projection settings,
@@ -776,8 +779,8 @@ The URL is an identifier only. Kibana and your browser will never access this UR
776779
*/
777780
_onWarning(...args: any[]) {
778781
if (!this.hideWarnings) {
779-
this.warnings.push(Utils.formatWarningToStr(args));
780-
return Utils.formatWarningToStr(args);
782+
this.warnings.push(Utils.formatWarningToStr(...args));
783+
return Utils.formatWarningToStr(...args);
781784
}
782785
}
783786
}

src/platform/plugins/private/vis_types/vega/public/vega_view/utils.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ describe('normalizeObject', () => {
4848
test('should throw if a function is given as the object property', async () => {
4949
expect(() => {
5050
normalizeObject({ toJSON: () => alert('gotcha') });
51-
}).toThrow('a function cannot be used as a property name');
51+
}).toThrow('Object normalization error');
5252
});
5353

5454
test('should throw if a function is given on a nested object', async () => {
5555
expect(() => {
5656
normalizeObject({ test: { toJSON: () => alert('gotcha') } });
57-
}).toThrow('a function cannot be used as a property name');
57+
}).toThrow('Object normalization error');
5858
});
5959

6060
test('should return null for null', async () => {

src/platform/plugins/private/vis_types/vega/public/vega_view/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ export function checkObjectForFunctionProperty(object: unknown): boolean {
4848
*/
4949
export function normalizeObject(object: unknown) {
5050
if (checkObjectForFunctionProperty(object)) {
51-
throw new Error('a function cannot be used as a property name');
51+
throw new Error('Object normalization error', {
52+
cause: 'A function cannot be used as a property name',
53+
});
5254
}
5355
const normalizedObject = object ? JSON.parse(JSON.stringify(object)) : null;
5456
ensureNoUnsafeProperties(normalizedObject);

src/platform/plugins/private/vis_types/vega/public/vega_view/vega_base_view.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,23 +241,29 @@ export class VegaBaseView {
241241

242242
const vegaLogger = logger(Warn);
243243

244-
vegaLogger.warn = this.onWarn.bind(this);
245-
vegaLogger.error = this.onError.bind(this);
244+
vegaLogger.warn = (...args) => {
245+
this.onWarn(...args);
246+
return vegaLogger;
247+
};
248+
vegaLogger.error = (...args) => {
249+
this.onError(...args);
250+
return vegaLogger;
251+
};
246252

247253
config.logger = vegaLogger;
248254

249255
return config;
250256
}
251257

252-
onError() {
253-
const error = Utils.formatErrorToStr(...arguments);
258+
onError(...args) {
259+
const error = Utils.formatErrorToStr(...args);
254260
this._addMessage('err', error);
255261
this._parser.searchAPI.inspectorAdapters?.vega.setError(error);
256262
}
257263

258-
onWarn() {
264+
onWarn(...args) {
259265
if (this._renderMode !== 'view' && (!this._parser || !this._parser.hideWarnings)) {
260-
this._addMessage('warn', Utils.formatWarningToStr(...arguments));
266+
this._addMessage('warn', Utils.formatWarningToStr(...args));
261267
}
262268
}
263269

0 commit comments

Comments
 (0)