tableau/embedding-api-v3-guide

Throw "Iframe has not been created yet" issue to developers instead of "Unexpected error during initialization"

hackwithharsha opened this issue · 3 comments

In the following code block.

You are throwing an error IFrame has not been created Yet, which is okay. But, eventually what we get is an Unexpected error during initialization... In this case, I would like to see IFrame has not been created yet. In the catch block you can check the exception message and raise two different errors one for IFrame has not been created yet and another one is miscellaneous which is Unexpected error during initialization

This would be more helpful while integrating SPA frameworks like React in my case.

  public initializeViz(): void {
    try {
      const iframeWindow = this._iframe.contentWindow;
      if (!iframeWindow) {
        throw new TableauError(EmbeddingErrorCodes.InternalError, 'Iframe has not been created yet');
      }

      this._messenger = new CrossFrameMessenger(window, iframeWindow, this._frameUrl.origin);

      // We need the notification service for the bootstrap flow.
      this._dispatcher = new CrossFrameDispatcher(this._messenger);
      ApiServiceRegistry.get(this.embeddingId).registerService(new NotificationServiceImpl(this._dispatcher));

      const notificationService = ApiServiceRegistry.get(this.embeddingId).getService<NotificationService>(ServiceNames.Notification);
      const vizSizeKnownUnregister = notificationService.registerHandler(
        NotificationId.FirstVizSizeKnown,
        () => true,
        (model: FirstVizSizeKnownModel) => {
          this.handleVizSizeKnownEvent(model);
          vizSizeKnownUnregister();
        },
      );
      const vizInteractiveUnregister = notificationService.registerHandler(
        NotificationId.VizInteractive,
        () => true,
        (model: EmbeddingBootstrapInfo) => {
          this.handleVizInteractiveEvent(model);
          vizInteractiveUnregister();
        },
      );
      this._messenger.startListening();
      this._iframe.src = this._frameUrl.toString();
    } catch (e) {
      throw new TableauError(Contract.EmbeddingErrorCodes.InternalError, 'Unexpected error during initialization.');
    }
  }

Thanks for the report!

Logged this bug internally. For tracking #1475544

Looping back on this one. The issue was fixed back in January 2023 and we should show a more detailed exception now when the iframe is not created.