Add optional parameter throwOnError to penpot.ui.sendMessage

This provides more flexibility to callers, who may need to react
to a failure appropriately.
This commit is contained in:
Dominik Jain 2026-05-14 14:36:25 +02:00 committed by Alonso Torres
parent 2a326ba23e
commit 94a5c6c4fd
3 changed files with 15 additions and 6 deletions

View File

@ -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`

View File

@ -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
*

View File

@ -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;
}