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>
</>
)}
/>
);
}<script setup lang="ts">
import { DocxEditor, DocxEditorOpenFailureAction } from '@docx-editor.dev/vue';
defineProps<{ bytes: Uint8Array }>();
</script>
<template>
<DocxEditor :document="bytes">
<template #openFailureActions="{ failure }">
<DocxEditorOpenFailureAction as-child>
<a :href="`/support?code=${failure.code}`">Get help</a>
</DocxEditorOpenFailureAction>
<DocxEditorOpenFailureAction
variant="primary"
dismiss
@click="reportOpenFailure(failure.code)"
>
Report the problem
</DocxEditorOpenFailureAction>
</template>
</DocxEditor>
</template>reportOpenFailure is your own function. Send the code, not the file.
Action prop | Effect |
|---|---|
variant="primary" | Styles the main action. |
asChild | Styles your own element, such as a link. |
dismiss | Closes 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
| Source | Holds | Use it for |
|---|---|---|
snapshot().openFailure | A DocumentOpenFailure with a stable code and a reason, or null. | Showing the failure to a person. |
snapshot().parseError | The engine's own text. It can quote document content and is not sanitized. | Logs that stay on your server. |
The document.open diagnostic | The 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:
reason | Codes | Suggested action |
|---|---|---|
damaged | open.zip-corrupt, open.zip-invalid-entry-name, open.xml-malformed, open.content-types-invalid, open.relationships-invalid | Save the file again as .docx in a desktop editor. |
not-docx | open.unsupported-source, open.missing-main-document | Choose a .docx file. |
too-large | open.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-limit | Try a smaller copy. |
blocked | open.xml-dtd-forbidden | Save the file again as .docx. The editor refuses this content for security. |
unknown | open.invalid-limits, open.failed | Try 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,
}}
/>| Option | Default | Effect |
|---|---|---|
title | openFailure.title | The dialog title. |
message | The message for the reason | Content, or a function of the DocumentOpenFailure. In Vue, pass text or a function. |
showCode | true | Shows the error code. |
closeLabel | common.close | The Close label. |
showClose | true | Shows Close. Escape still closes the dialog. |
onDismiss | None | Runs after any close. In Vue, listen for dismiss. |
className, style | None | Styles the dialog element. |
t | The editor's translator | Resolves 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, anddismiss. - 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:
| Selector | Element |
|---|---|
[data-docx-dialog='openFailure'] | The dialog. |
.docx-dialog__message | The message. |
.docx-dialog__code | The error code line. |
.docx-dialog__button, .docx-dialog__button--primary | Close 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>;<!-- OpenFailureBanner.vue -->
<script setup lang="ts">
import { useOpenFailure } from '@docx-editor.dev/vue';
const { failure, message, dismiss } = useOpenFailure();
</script>
<template>
<div v-if="failure" role="alert" class="banner">
{{ message }} ({{ failure.code }})
<button type="button" @click="dismiss">Dismiss</button>
</div>
</template>Mount the banner in the default slot of <DocxEditor :document="bytes" :open-failure="false">.
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.
dismisshides 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
parseErrorto telemetry; it can quote the document.