Every failure is a DocumentEngineError with a stable code. Document engine error codes never change within 1.x, so switch on the code and never on the message text.
import { isDocumentEngineError } from "fabricjs-document-engine";
try {
await engine.load(id);
} catch (error) {
if (isDocumentEngineError(error) && error.code === "UNSAVED_CHANGES") {
await engine.save();
await engine.load(id);
}
}Exports and image checks also report the problem code TOO_LARGE when a size is over the canvas size limits. Besides code, an error can carry issues, unknownTypes, missingAssets, missingFonts, problems, migrationFrom, retryable and cause.
All document engine error codes
| Code | When |
|---|---|
INVALID_DOCUMENT | The document shape is wrong, or the imported text is not JSON |
UNSAFE_DOCUMENT | An unsafe image address, too many objects, or nesting too deep |
UNSUPPORTED_SCHEMA | Written by a newer major version of this package |
UNKNOWN_OBJECT_TYPE | A custom class is not registered |
INVALID_CUSTOM_OBJECT | A registered class has no static type, or is not a Fabric.js class |
MIGRATION_FAILED | An older document could not be upgraded |
LOAD_ABORTED | The load's signal was cancelled, or a newer load or destroy stopped it |
LOAD_CONFLICT | The canvas was edited while a document was loading. The edits are kept |
LOAD_FAILED | Fabric.js or storage failed to load |
MISSING_ASSETS | Images could not be loaded. Each entry in missingAssets has a failure.reason |
MISSING_FONTS | Fonts are unavailable and requireFonts is on |
ASSET_UPLOAD_FAILED | The upload handler failed |
UNSAVED_CHANGES | Opening another document would discard unsaved changes |
STORAGE_MISSING | load, save or autosave was used without storage |
SAVE_FAILED | Storage refused after all retries |
SAVE_CONFLICT | Another tab or device saved first |
SAVE_CANCELLED | A save was dropped because another document was opened, or the engine was destroyed |
DOCUMENT_NOT_FOUND | A built-in adapter has no such document |
HISTORY_FAILED | Undo or redo could not rebuild an object |
RECOVERY_MISSING | Recovery is not configured |
RECOVERY_NOT_FOUND | There is no recovery copy |
RECOVERY_FAILED | A recovery copy could not be written |
VERSIONS_UNSUPPORTED | Storage has no version methods |
VERSION_NOT_FOUND | There is no such version |
VERSION_FAILED | An automatic version failed |
INVALID_EXPORT_OPTIONS | The export options are not valid |
EXPORT_BLOCKED | An image or font would break the export. See error.problems |
EXPORT_ABORTED | The export was cancelled |
EXPORT_FAILED | Rendering failed |
SVG_IMPORT_FAILED | importSvg got text that is not a valid SVG |
LOAD_CONFLICT | The canvas was edited while a document opened. Pass discardUnsavedChanges to replace the edits |
DOCUMENT_CHANGED | An import, paste or image swap finished after another document opened, so it was dropped |
PDF_UNAVAILABLE | jspdf or svg2pdf.js is not installed, or there is no browser |
PDF_FAILED | A font is not TrueType, or the PDF could not be made |
DOCUMENT_CHANGED | Async work finished after another document opened, and was dropped |
ENGINE_DESTROYED | The engine was used after destroy() |
An oversized export fails with EXPORT_BLOCKED and a problem with the code TOO_LARGE, and an oversized image with the reason TOO_LARGE, see canvas size limits. An object drawn as a picture in an SVG export comes with the warning CLIP_PATH_RASTERIZED.
The ones that need UI
Most document engine error codes are logged and forgotten. These need the user to decide, so give them a dialog: SAVE_CONFLICT (save conflicts), MISSING_ASSETS (images and fonts) and UNSAVED_CHANGES. A full handler is in the production checklist.