Error Handling and Pop-Up Notifications in OnlyOffice Web Version

How can I handle errors in the OnlyOffice web version for a Docx document plugin developed using Document Builder? I tried using try-catch, but it seems that the errors are not being caught inside the catch block; instead, they are being directly printed to the console. I want to display a popup window when an error occurs. How can I achieve that?

Hello @ajay

Do I understand correctly that you are looking for a method similar to the one that was discussed in this thread?

Hi @Nikolas ,
Yes, something like that. Firstly, I’m having trouble catching exceptions/errors. For instance, when I invoke GetBookmarkRange(“bookmarkName”) and the bookmark doesn’t exist in the document, it throws an error. Unfortunately, I can’t seem to catch this error using a try-catch block; it bypasses and goes straight to the console. Secondly, I intend to display the thrown error in a popup window, but I’m encountering difficulties with that as well. I’ve experimented with using alerts and creating custom methods, but so far, I haven’t been successful.

It is mentioned in the thread that the enhancement suggestion on message boxes, i.e. pop-ups, was registered but, unfortunately, it hasn’t been implemented yet thus you cannot call message boxed for your scenario.

Okay, I understand that pop-ups are not available at the moment. However, is there any way I can handle errors in a try-catch block? Currently, errors are not being caught inside the catch block.

I don’t quite follow. For reference I took method AddElement and put it into try...catch logic with certain modifications:

try {
    var oDocument = Api.GetDocument();
    var oParagraph = oDocument.GetElement(0);
    var oRun = Api.CreateRun();
    oRun.AddText("This is just a text run. Nothing special.");
    oParagraph.AddElement(Run);
}
catch (error) {
    var oDocument = Api.GetDocument();
    var oParagraph = oDocument.GetElement(0);
    var oRun = Api.CreateRun();
    oRun.AddText("This is just a text run from catch block.");
    oParagraph.AddElement(oRun);
    console.error(error);
}

And since there is an error in the try block with oRun variable being declared incorrectly for the method (Run only), it throws an error and proceeds executing the rest of the script inside catch block.
According to the documentation on try...catch:

The try...catch statement is comprised of a try block and either a catch block, a finally block, or both. The code in the try block is executed first, and if it throws an exception, the code in the catch block will be executed.

Let me know if I misunderstand anything.

Hello @Constantine ,
I’m currently exploring options for accessing DOM elements from the left side panel of my plugin, given that pop-up functionality isn’t available. The idea is to create a separate element dedicated to displaying error messages. I’ve managed to come up with a method that works well when placed within the init method. However, I’ve encountered an issue: when I attempt to execute the same method within the callCommand method, I receive an error message stating ‘An error occurred: TypeError: document.getElementById is not a function.’

function sendNotification(message, type) {

        var container = document.getElementById("notificationContainer");

        var notification = document.createElement("div");

     

        notification.style.position = "fixed";

        notification.style.top = "2%";

        notification.style.width = "100%";

        notification.style.textAlign = "left";

     

        notification.textContent = message;

     

        if (type === "success") {

          notification.className = "success";

        } else if (type === "error") {

          notification.className = "error";

        }

     

        container.appendChild(notification);

     

        setTimeout(function () {

          container.removeChild(notification);

        }, 5000); // Remove the notification after 5 seconds

      }

Furthermore, I would appreciate some clarity on whether pop-up functionality can be utilized to accept user input

@Constantine

Kindly respond. I’ve utilized the callCommand method and implemented the use of DocumentBuilder to send structured data to the editor. I’m aiming to present any errors that may arise within the callCommand , such as ‘bookmark not found’ or issues related to external API calls. Could you suggest a method to display these error messages to the end user?

@Constantine
any updates?

Sorry for the late response.
I’ve got some information that might be useful.

Please note that we haven’t tested such scenarios so we cannot provide any examples.

In general, callCommand allows you using Document Builder API methods and it cannot fetch errors from plugin directly because it operates in different context. Possible workaround here is to write further errors to the callCommand function callback and then pass the information from the callback to the element on the left panel of your plugin.

@Constantine
I tried this, but I am unable to access the elements of my left panel in the plugin from callCommand because the document and window objects are restricted in OnlyOffice.

I will try to find more info, possibly I would come up with an alternative approach.

Did you try to access main context directly from callCommand? If your final scenario is:

  1. Execute Document Builder API script via callCommand method;
  2. If the script results in error - fetch it;
  3. The error displays in the <p> element in iframe of the plugin.

Here, the third step should be like that:

  • fetched error is written as a text to the callCommand callback parameter;

callback - function - The result that the method returns. Only the js standart types are available (any objects will be replaced with undefined).

  • From init context making call to the <p> element;
  • Writing the text of the error.

Yes, I tried to access elements from the callCommand first parameter callback function but was unable to do so. Additionally, I attempted to return an error message from the first callback function to the last parameter callback function (here I am able to access elements). However, in some cases, I encountered difficulty in returning an error message, such as when I made a call to the fetch method. In these situations, I was unable to retrieve the error message, preventing me from accessing it in the last parameter callback function. I have shared my plugin here. Please take a look

As I can see you are still trying to use try...catch block for this purpose. Unfortunately, I’m failing to understand the logic of actions there since the description of try...catch follows quite different path as I’ve mentioned before:

With that said, all asked information about the topic that can be useful for this case I’ve provided in my earlier replies.

Considering the fact that my colleague did review of your plugin once with different problem, if you want to get further support on this, please refer to this post: