Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down