refactor(core): fix for no-uncaught-json-parse warnings

This commit is contained in:
Michael Kret
2022-10-21 21:52:43 +03:00
committed by GitHub
parent ca9eca9ae9
commit 1d57b10942
36 changed files with 150 additions and 93 deletions

View File

@@ -16,4 +16,4 @@ export * from './WorkflowDataProxy';
export * from './WorkflowErrors';
export * from './WorkflowHooks';
export { LoggerProxy, NodeHelpers, ObservableObject, TelemetryHelpers };
export { deepCopy } from './utils';
export { deepCopy, jsonParse } from './utils';

View File

@@ -30,3 +30,22 @@ export const deepCopy = <T>(source: T): T => {
return clone;
};
// eslint-enable
type ErrorMessage = { errorMessage: string };
type FallbackValue<T> = { fallbackValue: T };
export const jsonParse = <T>(
jsonString: string,
options: ErrorMessage | FallbackValue<T> | {} = {},
): T => {
try {
return JSON.parse(jsonString) as T;
} catch (error) {
if ('fallbackValue' in options) {
return options.fallbackValue;
}
if ('errorMessage' in options) {
throw new Error(options.errorMessage);
}
throw error;
}
};