Error Handling
Branch on error codes from any Polotno package: editor, exporters, importers, and polotno-node
Every Polotno package reports failures the same way: a plain Error with a code property and a structured, JSON-serializable details object. Consuming code branches on error.code: the same pattern Node.js itself uses (err.code === 'ENOENT'):
import { jsonToPDFBytes, type PolotnoError } from '@polotno/pdf-export';
try {
await jsonToPDFBytes(design);
} catch (e) {
const error = e as PolotnoError;
if (error.code === 'IMAGE_FAILED') {
// which element failed:
console.log(error.details?.elementId);
if (error.details?.reason === 'unsupported-format') {
// e.g. 'application/pdf' – an embedded PDF passed as an image
console.log(error.details.mimeType);
}
} else {
throw e;
}
}There are no error classes and no instanceof checks. A property check works across bundle copies, worker threads, and serialization boundaries: codes even survive the headless-browser boundary inside polotno-node, where thrown errors are rehydrated with code and details intact.
Codes vs. messages
Branch on error.code and details.reason, not on error.message. Codes are meant to stay stable across releases; messages are human-readable text and may be reworded at any time: log them, display them, but don't match on them.
Error codes
details.reason carries the fine grain within a code; the other details fields tell you what exactly failed.
| Code | When | Key details |
|---|---|---|
DESIGN_INVALID | Design JSON failed validation (schema check, store.loadJSON, unknown element type) | errors: array of { path, message } |
VARIABLES_INVALID | resolveVariables with strict: true found an error in the record: see Template variables | reason: missing-required (a required variable has no value), type-mismatch (a value does not suit its property): plus issues, every issue for the record as { severity, reason, variable?, key? } |
EXPORT_FAILED | An export could not run or an element failed to render | reason: page-not-found, workspace-not-mounted, unmounted-during-export, invalid-options, element, busy, pdfx, limit-exceeded (the render passed a resource limit, which limit names), empty-output (the browser could not encode the canvas, usually because it is too large; try a lower pixelRatio), upload-failed (email export: your uploadImage rejected or returned no URL; a rejection's error is the cause), spot-ink-rasterized (PDF export: a filtered SVG or blurred text uses a mapped spot ink, which the image it is drawn as cannot carry; see Spot colors): plus pageId, option, limit, label, elementId, mimeType, pixelRatio, name, and pageNumber and inks for spot-ink-rasterized |
IMAGE_FAILED | An image could not be loaded, decoded, or embedded | reason: unsupported-format, load, decode-failed, fetch-failed, blocked (the download check refused the URL, so a retry cannot help), invalid-data-url, timeout, raster-failed: plus mimeType, supported, src, elementId |
FONT_FAILED | A font could not be resolved or loaded | reason: not-found, timeout, fetch-failed, decode-failed, blocked: plus family, src; load-timeout (the editor's asset loader gave up waiting on a font): plus label, timeoutMs instead of family |
VIDEO_FAILED | A video could not be loaded, decoded, or encoded | reason: load, no-video-track, unsupported-codec, cannot-decode, no-encoder, zero-duration, invalid-dimensions, timeout: plus src |
FETCH_FAILED | A network request for an asset failed (after retries, where applicable) | url, status, attempts, and reason: 'blocked' when the download check refused the URL, so a retry cannot help |
TOOL_FAILED | A canvas image tool (crop, expand, inpaint, upscale, remove-background) could not complete | reason: handler-failed, busy, canceled, stale-result, unsupported-target, unavailable, invalid-geometry, invalid-result: plus tool, elementId |
IMPORT_FAILED | An imported file (PSD / SVG / PDF / PPTX / IDML) could not be parsed | format: psd, svg, pdf, pptx, idml: plus reason: password-required, password-incorrect (PDF: the file is intact, so prompt and retry; a password-protected PPTX gets password-required, so remove the password in PowerPoint first), truncated (PSD or PPTX: the file is cut short, so ask for a complete copy), corrupt (PDF, PSD, PPTX or IDML: the file's own data is malformed, so a retry cannot help; an InDesign .indd file reads as a corrupt IDML, so save it as IDML first), unsupported (PSD: a valid file in Lab, Duotone or Multichannel mode; PPTX: a legacy binary .ppt, an unsupported compression method, or a package with no ppt/presentation.xml; IDML: a zip that is not an IDML package and holds no .idml file, with found: 'indd' when it holds an .indd file instead, or XML nested deeper than 256 levels), too-large (PSD: the decoded layer pixels would exceed the parser's 2 GiB memory budget, so the file is too large to import; IDML: a part over 383 MiB, or parts over 1 GiB in total, the files read from a zipped InDesign package included); absent means the parser failure has no specific classification |
UNKNOWN | An unclassified failure reported through onLoadError | : |
The original low-level error, when there is one, is chained as error.cause.
Where errors come from
Each package throws only the codes relevant to it:
| Package | Codes you can catch |
|---|---|
polotno (editor) | DESIGN_INVALID, EXPORT_FAILED from store methods; TOOL_FAILED and IMAGE_FAILED from image tool sessions; IMAGE_FAILED, VIDEO_FAILED, FONT_FAILED, UNKNOWN via onLoadError |
polotno-node | Everything the editor reports, rehydrated across the browser boundary, plus FETCH_FAILED and EXPORT_FAILED (reason: 'busy') |
@polotno/pdf-export | DESIGN_INVALID, IMAGE_FAILED, EXPORT_FAILED, FONT_FAILED, FETCH_FAILED |
@polotno/pptx-export | DESIGN_INVALID, IMAGE_FAILED, EXPORT_FAILED, FETCH_FAILED |
@polotno/image-export, @polotno/email-export | DESIGN_INVALID, EXPORT_FAILED, IMAGE_FAILED, FONT_FAILED |
@polotno/video-export | VIDEO_FAILED, FETCH_FAILED, EXPORT_FAILED |
@polotno/psd-import, @polotno/svg-import, @polotno/pdf-import, @polotno/pptx-import, @polotno/idml-import | IMPORT_FAILED |
@polotno/schema | DESIGN_INVALID; VARIABLES_INVALID from resolveVariables in @polotno/schema/variables with strict: true |
onLoadError in the editor
Exports and imports throw. The editor canvas is different: a broken image or font inside the editor should not crash rendering, so those failures are reported instead: to the global onLoadError handler. The error object has exactly the same shape as thrown errors:
import { onLoadError } from 'polotno/config';
onLoadError((error) => {
if (error.code === 'IMAGE_FAILED') {
notifyUser(`An image could not be loaded (element ${error.details?.elementId})`);
} else if (error.code === 'FONT_FAILED') {
notifyUser(`Font "${error.details?.family}" could not be loaded`);
}
});See Asset Loading Configuration for timeouts and the rest of the loading configuration.
TypeScript
Packages export a PolotnoError type and a PolotnoErrorCode union for their error codes. Import these types from the package you call:
import type { PolotnoError, PolotnoErrorCode } from '@polotno/pdf-export';Image tool sessions can throw TOOL_FAILED, but the editor’s PolotnoError type omits that code. Its exported details map includes the code. Until the editor type is corrected, extend the type in your application when handling image tool sessions:
import type {
PolotnoError,
PolotnoErrorDetailsMap,
} from 'polotno/utils/errors';
type EditorOperationError = PolotnoError | (Error & {
code: 'TOOL_FAILED';
details?: PolotnoErrorDetailsMap['TOOL_FAILED'];
});
// Inside a catch block for an editor operation:
function reportEditorError(error: EditorOperationError) {
if (error.code === 'TOOL_FAILED') {
console.log(error.details?.tool, error.details?.reason);
}
}