Handle files that do not open

Show a dialog, a toast, or your own message when the editor refuses a file, and add your own support actions.

The editor refuses a file that it cannot open safely, such as a damaged archive or a file that is not .docx. The pages stay blank, and an open-failure dialog shows a generic message and a stable error code.

Add an open-failure action

Pass renderOpenFailureActions in React, or fill the #openFailureActions slot in Vue, to add buttons before Close:

import { DocxEditor } from '@docx-editor.dev/react';

export function Editor({ bytes }: { bytes: Uint8Array }) {
  return (
    <DocxEditor
      document={bytes}
      renderOpenFailureActions={({ failure }) => (
        <>
          <DocxEditor.OpenFailure.Action asChild>
            <a href={`/support?code=${failure.code}`}>Get help</a>
          </DocxEditor.OpenFailure.Action>
          <DocxEditor.OpenFailure.Action
            variant="primary"
            dismiss
            onClick={() => reportOpenFailure(failure.code)}
          >
            Report the problem
          </DocxEditor.OpenFailure.Action>
        </>
      )}
    />
  );
}

reportOpenFailure is your own function. Send the code, not the file.

Action propEffect
variant="primary"Styles the main action.
asChildStyles your own element, such as a link.
dismissCloses the dialog after your click handler runs. Call event.preventDefault() in the handler to keep it open.

In a composed editor, pass the same function as actions to DocxEditor.OpenFailure.

Choose a failure source

SourceHoldsUse it for
snapshot().openFailureA DocumentOpenFailure with a stable code and a reason, or null.Showing the failure to a person.
snapshot().parseErrorThe engine's own text. It can quote document content and is not sanitized.Logs that stay on your server.
The document.open diagnosticThe same code, with size buckets and an engine cause.Telemetry. See Diagnostics.

openFailure stays set until another document starts to open. Each refusal is a new object, so a repeated refusal of the same file shows the dialog again.

The reason groups codes by what a person can do:

reasonCodesSuggested action
damagedopen.zip-corrupt, open.zip-invalid-entry-name, open.xml-malformed, open.content-types-invalid, open.relationships-invalidSave the file again as .docx in a desktop editor.
not-docxopen.unsupported-source, open.missing-main-documentChoose a .docx file.
too-largeopen.zip-entry-limit, open.zip-size-limit, open.zip-ratio-limit, open.xml-part-size-limit, open.xml-depth-limit, open.xml-element-limit, open.relationship-limit, open.part-limitTry a smaller copy.
blockedopen.xml-dtd-forbiddenSave the file again as .docx. The editor refuses this content for security.
unknownopen.invalid-limits, open.failedTry again, or try another copy.

The packaged messages are translated under the openFailure keys in @docx-editor.dev/i18n. To change one in every editor, override its key. For more information, see @docx-editor.dev/i18n.

Configure the dialog

Pass options as the openFailure object on <DocxEditor>, or as props of DocxEditor.OpenFailure:

// MESSAGES is your Record<DocumentOpenFailure['reason'], string>.
<DocxEditor
  document={bytes}
  openFailure={{
    title: 'Upload failed',
    message: (failure) => MESSAGES[failure.reason],
    showCode: false,
  }}
/>
OptionDefaultEffect
titleopenFailure.titleThe dialog title.
messageThe message for the reasonContent, or a function of the DocumentOpenFailure. In Vue, pass text or a function.
showCodetrueShows the error code.
closeLabelcommon.closeThe Close label.
showClosetrueShows Close. Escape still closes the dialog.
onDismissNoneRuns after any close. In Vue, listen for dismiss.
className, styleNoneStyles the dialog element.
tThe editor's translatorResolves the packaged strings.

To replace the header, body, and footer, pass children in React or the default slot in Vue.

  • Your content receives failure, message, and dismiss.
  • The dialog keeps its frame, focus handling, and Escape.
  • Your content owns its accessible message and actions.

<DocxEditor> includes the dialog. A composed editor does not: add DocxEditor.OpenFailure (Vue: DocxEditorOpenFailure) inside DocxEditor.Root.

The dialog uses the shared dialog classes described in Customize popups:

SelectorElement
[data-docx-dialog='openFailure']The dialog.
.docx-dialog__messageThe message.
.docx-dialog__codeThe error code line.
.docx-dialog__button, .docx-dialog__button--primaryClose and each Action.

Show a banner or toast

Turn off the dialog with openFailure={false}, then read the failure with useOpenFailure inside DocxEditor.Root:

import { DocxEditor, useOpenFailure } from '@docx-editor.dev/react';

function OpenFailureBanner() {
  const { failure, message, dismiss } = useOpenFailure();
  if (!failure) return null;
  return (
    <div role="alert" className="banner">
      {message} ({failure.code})
      <button type="button" onClick={dismiss}>
        Dismiss
      </button>
    </div>
  );
}

<DocxEditor document={bytes} openFailure={false}>
  <OpenFailureBanner />
</DocxEditor>;

Children of <DocxEditor> render after the pages. To place a banner elsewhere, compose the editor and put it before DocxEditor.Viewport.

When a toast closes after a delay, call dismiss(failure) with the failure it showed. A late close then cannot hide a newer refusal.

Limits

  • Disable the default dialog when you replace it. Otherwise each surface has its own dismiss state, and the reader must close both.
  • dismiss hides only the current refusal. The next refusal shows again, even for the same file.
  • The dialog is an alert. A backdrop click does not close it.
  • Do not send parseError to telemetry; it can quote the document.

See also