Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling received errors when sending a message to the plugin from the WebView #164

Open
Baraksi opened this issue Nov 27, 2020 · 1 comment

Comments

@Baraksi
Copy link

Baraksi commented Nov 27, 2020

Hey,
When sending a message to the plugin from the WebView using window.postMessage a Promise is returned (as documented). It works great when the event handler on the plugin side finishes as expected, but when an error is thrown on the plugin side the postMessage received error seems to be an empty object.

I tried passing browserWindow.webContents.on with event handlers that are both synchronous and asynchronous functions and both behave that way.

Is that the actual behavior or am I missing something? If it is, it would be great to get the full error information on the WebView side.

Thanks,
Barak

@Baraksi Baraksi changed the title Handling errors when sending a message to the plugin from the WebView Handling received errors when sending a message to the plugin from the WebView Nov 27, 2020
@lse-dvh
Copy link

lse-dvh commented Jan 28, 2022

Hello @Baraksi,
there is kinda a workaround for this. If an error occurs, you can send the error back to the webview:

// Sent from the webview to the plugin.
// But it will get rejected, because some error occured
window.postMessage('faultyCall', {
  helloMessage: "Hello World!",
})
// Event received by plugin
browserWindow.webContents.on('faultyCall', function (event) {
  const { helloMessage } = event;
  // If an error occurs during execution, the Promise from the webview simply gets rejected.
  // This is bad, because we would never know WHY it got rejected. So we catch, log and throw again.
  try {
    doSomethingThatRaisesAnError(helloMessage);
  } catch (error) {
    // Log error to Sketch, so you know what went wrong
    console.error(error);

    // Send error message back to webview and handle error separately
    // or straight up call a function with error as parameter
    sendToWebview("unique.id", `window.error = ${JSON.stringify(error.toString())};`);

    // Continue throwing error, so Promise in webview will get rejected    
    throw error; 
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants