Skip to content

Error Reference

The @scaryterry/pdfium library uses a structured error system with specific error codes and specialised error classes. This enables precise error handling and debugging.

All errors extend the base PDFiumError class:

import {
PDFiumError,
InitialisationError,
NetworkError, // extends InitialisationError
DocumentError,
PermissionsError, // extends DocumentError
PageError,
RenderError,
MemoryError,
TextError,
ObjectError,
WorkerError,
} from '@scaryterry/pdfium';

All library errors extend this class.

PropertyTypeDescription
codePDFiumErrorCodeNumeric error code
messagestringHuman-readable message
contextRecord<string, unknown>Additional context
try {
using document = await pdfium.openDocument(data);
} catch (error) {
if (error instanceof PDFiumError) {
console.error(`Error ${error.code}: ${error.message}`);
console.error('Context:', error.context);
}
}

Thrown when WASM or library initialisation fails.

Error Codes: 100-102

try {
using pdfium = await PDFium.init({ wasmUrl: '/invalid.wasm' });
} catch (error) {
if (error instanceof InitialisationError) {
console.error('Failed to initialise PDFium:', error.message);
}
}

Thrown when a network fetch fails during initialisation (e.g. WASM URL unreachable). Extends InitialisationError.

Error Code: 103

try {
using pdfium = await PDFium.init({ wasmUrl: 'https://cdn.example.com/pdfium.wasm' });
} catch (error) {
if (error instanceof NetworkError) {
console.error('Failed to fetch WASM binary:', error.message);
}
}

Thrown for document loading, access, and save operations.

Error Codes: 200-208

try {
using document = await pdfium.openDocument(data, { password: 'wrong' });
} catch (error) {
if (error instanceof DocumentError) {
switch (error.code) {
case PDFiumErrorCode.DOC_PASSWORD_REQUIRED:
console.error('Password required');
break;
case PDFiumErrorCode.DOC_PASSWORD_INCORRECT:
console.error('Wrong password');
break;
case PDFiumErrorCode.DOC_FORMAT_INVALID:
console.error('Invalid PDF');
break;
}
}
}

Thrown when a document operation is denied by the PDF’s permission flags (e.g. printing or editing restricted). Extends DocumentError.

Error Code: 209

try {
// Attempt an operation on a permissions-restricted document
const bytes = document.save();
} catch (error) {
if (error instanceof PermissionsError) {
console.error('Permission denied:', error.message);
}
}

Thrown for page access and loading issues.

Error Codes: 300-303

try {
using page = document.getPage(999);
} catch (error) {
if (error instanceof PageError) {
if (error.code === PDFiumErrorCode.PAGE_INDEX_OUT_OF_RANGE) {
console.error('Page does not exist');
}
}
}

Thrown when page rendering fails.

Error Codes: 400-402

try {
const result = page.render({ width: 100000, height: 100000 });
} catch (error) {
if (error instanceof RenderError) {
if (error.code === PDFiumErrorCode.RENDER_INVALID_DIMENSIONS) {
console.error('Dimensions exceed limit');
}
}
}

Thrown for WASM memory allocation failures.

Error Codes: 500-502

try {
using document = await pdfium.openDocument(hugeBuffer);
} catch (error) {
if (error instanceof MemoryError) {
console.error('Memory allocation failed:', error.message);
}
}

Thrown when text extraction operations fail.

Error Codes: 600-602

try {
const text = page.getText();
} catch (error) {
if (error instanceof TextError) {
console.error('Text extraction failed:', error.message);
}
}

Thrown for page object and annotation access errors.

Error Codes: 700-751

try {
const annot = page.getAnnotation(999);
} catch (error) {
if (error instanceof ObjectError) {
if (error.code === PDFiumErrorCode.ANNOT_INDEX_OUT_OF_RANGE) {
console.error('Annotation does not exist');
}
}
}

Thrown for worker communication and timeout issues.

Error Codes: 800-803

try {
using proxy = await WorkerProxy.create(workerUrl, wasmBinary, { timeout: 5000 });
} catch (error) {
if (error instanceof WorkerError) {
if (error.code === PDFiumErrorCode.WORKER_TIMEOUT) {
console.error('Worker operation timed out');
}
}
}
CodeNameDescriptionResolution
100INIT_WASM_LOAD_FAILEDFailed to load WASM binaryCheck WASM URL/path; verify file exists
101INIT_LIBRARY_FAILEDPDFium library init failedCheck WASM binary integrity
102INIT_INVALID_OPTIONSInvalid initialisation optionsValidate options against PDFiumInitOptions
103INIT_NETWORK_ERRORNetwork error while fetching resourcesCheck URL accessibility and CORS headers

CodeNameDescriptionResolution
200DOC_FILE_NOT_FOUNDFile not foundVerify file path
201DOC_FORMAT_INVALIDInvalid PDF formatVerify file is valid PDF
202DOC_PASSWORD_REQUIREDDocument is encryptedProvide password in options
203DOC_PASSWORD_INCORRECTWrong passwordVerify password
204DOC_SECURITY_UNSUPPORTEDUnsupported security handlerDocument uses proprietary DRM
205DOC_ALREADY_CLOSEDDocument already closedDon’t use disposed documents
206DOC_LOAD_UNKNOWNUnknown load errorCheck PDF validity
207DOC_SAVE_FAILEDFailed to save documentCheck save options; verify disk space
208DOC_CREATE_FAILEDFailed to create documentCheck memory availability
209DOC_PERMISSION_DENIEDOperation not permitted by document permissionsCheck document permission flags

CodeNameDescriptionResolution
300PAGE_NOT_FOUNDPage not foundVerify page exists
301PAGE_LOAD_FAILEDFailed to load pagePDF may be corrupted
302PAGE_ALREADY_CLOSEDPage already closedDon’t use disposed pages
303PAGE_INDEX_OUT_OF_RANGEPage index out of rangeUse index 0 to pageCount-1

CodeNameDescriptionResolution
400RENDER_BITMAP_FAILEDFailed to create bitmapCheck memory; reduce dimensions
401RENDER_INVALID_DIMENSIONSDimensions exceed limitReduce scale/dimensions or increase maxRenderDimension
402RENDER_FAILEDRendering failedCheck page validity

CodeNameDescriptionResolution
500MEMORY_ALLOCATION_FAILEDWASM allocation failedReduce document size; close unused resources
501MEMORY_BUFFER_OVERFLOWBuffer overflowDocument too large; increase maxDocumentSize
502MEMORY_INVALID_POINTERInvalid memory pointerInternal error; report bug

CodeNameDescriptionResolution
600TEXT_EXTRACTION_FAILEDText extraction failedPage may lack text content
601TEXT_PAGE_FAILEDFailed to load text pageCheck page validity
602TEXT_LOAD_FAILEDFailed to load textInternal error

CodeNameDescriptionResolution
700OBJECT_TYPE_UNKNOWNUnknown object typeObject type not supported
701OBJECT_ACCESS_FAILEDFailed to access objectCheck object index
750ANNOT_INDEX_OUT_OF_RANGEAnnotation index out of rangeUse index 0 to annotationCount-1
751ANNOT_LOAD_FAILEDFailed to load annotationAnnotation may be malformed

CodeNameDescriptionResolution
800WORKER_CREATE_FAILEDFailed to create workerCheck worker URL
801WORKER_COMMUNICATION_FAILEDWorker communication failedWorker may have crashed
802WORKER_TIMEOUTWorker operation timed outIncrease timeout or reduce work
803WORKER_RESOURCE_LIMITWorker resource limit reachedClose unused resources

CodeNameDescriptionResolution
900RESOURCE_DISPOSEDResource already disposedDon’t use after dispose()
try {
using document = await pdfium.openDocument(data);
using page = document.getPage(0);
const text = page.getText();
} catch (error) {
if (error instanceof PDFiumError) {
console.error(`PDFium error ${error.code}: ${error.message}`);
} else {
throw error; // Re-throw non-PDFium errors
}
}
import {
PDFiumError,
PDFiumErrorCode,
DocumentError,
RenderError,
} from '@scaryterry/pdfium';
async function loadAndRender(data: Uint8Array, password?: string) {
try {
using document = await pdfium.openDocument(data, { password });
using page = document.getPage(0);
return page.render({ scale: 2 });
} catch (error) {
if (error instanceof DocumentError) {
switch (error.code) {
case PDFiumErrorCode.DOC_PASSWORD_REQUIRED:
throw new Error('This PDF requires a password');
case PDFiumErrorCode.DOC_PASSWORD_INCORRECT:
throw new Error('Incorrect password');
case PDFiumErrorCode.DOC_FORMAT_INVALID:
throw new Error('This file is not a valid PDF');
default:
throw new Error(`Document error: ${error.message}`);
}
}
if (error instanceof RenderError) {
if (error.code === PDFiumErrorCode.RENDER_INVALID_DIMENSIONS) {
throw new Error('Image dimensions too large');
}
}
throw error;
}
}
async function openWithPasswordPrompt(data: Uint8Array): Promise<PDFiumDocument> {
let password: string | undefined;
while (true) {
try {
return await pdfium.openDocument(data, { password });
} catch (error) {
if (error instanceof DocumentError) {
if (error.code === PDFiumErrorCode.DOC_PASSWORD_REQUIRED ||
error.code === PDFiumErrorCode.DOC_PASSWORD_INCORRECT) {
password = await promptForPassword(); // Your UI function
if (!password) {
throw new Error('Password entry cancelled');
}
continue;
}
}
throw error;
}
}
}
async function extractTextSafely(page: PDFiumPage): Promise<string | null> {
try {
return page.getText();
} catch (error) {
if (error instanceof TextError) {
console.warn(`Text extraction failed for page ${page.index}: ${error.message}`);
return null;
}
throw error;
}
}

Use getErrorMessage() to get human-readable messages:

import { getErrorMessage, PDFiumErrorCode } from '@scaryterry/pdfium';
const message = getErrorMessage(PDFiumErrorCode.DOC_PASSWORD_REQUIRED);
// "Document requires a password"