diff --git a/plugins/CHANGELOG.md b/plugins/CHANGELOG.md index cb2a2400e4..614a77407b 100644 --- a/plugins/CHANGELOG.md +++ b/plugins/CHANGELOG.md @@ -1,6 +1,7 @@ ## 1.5.0 (Unreleased) - **plugins-runtime**: Added `version` field that returns the current version +- **plugins-runtime**: Added optional parameter `throwOnError` to `penpot.ui.sendMessage` (default false, backwards-compatible) - **plugin-types**: Added a flags subcontexts with the flag `naturalChildrenOrdering` - **plugin-types**: Fix penpot.openPage() to navigate in same tab by default - **plugin-types:** Change `LibraryComponent.isVariant()` return type to type guard `this is LibraryVariantComponent` diff --git a/plugins/libs/plugin-types/index.d.ts b/plugins/libs/plugin-types/index.d.ts index b2d810a17d..bad2ce5e9f 100644 --- a/plugins/libs/plugin-types/index.d.ts +++ b/plugins/libs/plugin-types/index.d.ts @@ -54,13 +54,14 @@ export interface Penpot extends Omit< * Sends a message to the plugin UI. * * @param message content usually is an object + * @param throwOnError if true, throws an error when the message cannot be cloned instead of logging to console (default: false) * * @example * ```js * this.sendMessage({ type: 'example-type', content: 'data we want to share' }); * ``` */ - sendMessage: (message: unknown) => void; + sendMessage: (message: unknown, throwOnError?: boolean) => void; /** * This is usually used in the `plugin.ts` file in order to handle the data sent by our plugin * diff --git a/plugins/libs/plugins-runtime/src/lib/api/index.ts b/plugins/libs/plugins-runtime/src/lib/api/index.ts index 00d64e58ed..52bd078ca5 100644 --- a/plugins/libs/plugins-runtime/src/lib/api/index.ts +++ b/plugins/libs/plugins-runtime/src/lib/api/index.ts @@ -67,17 +67,24 @@ export function createApi( return plugin.resizeModal(width, height); }, - sendMessage(message: unknown) { + sendMessage(message: unknown, throwOnError = false) { let cloneableMessage: unknown; try { cloneableMessage = structuredClone(message); } catch (err) { - console.error( + const msg = 'plugin sendMessage: the message could not be cloned. ' + - 'Ensure the message does not contain functions, DOM nodes, or other non-serializable values.', - err, - ); + 'Ensure the message does not contain functions, DOM nodes, or other non-serializable values.'; + if (throwOnError) { + throw new Error( + msg + + ' Original error: ' + + (err instanceof Error ? err.message : String(err)), + ); + } else { + console.error(msg, err); + } return; }