mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
refactor(core): fix for no-uncaught-json-parse warnings
This commit is contained in:
@@ -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';
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user