Typed error hierarchy for
@awacloud/pdf— basePdfError+ 4 subclasses.
Module pdfErrors | Source packages/front/office/pdf/src/errors.js | Deps none | Worker-safe yes
Every error carries a stable kebab-case code ('pdf/xref/truncated', 'pdf/page/bad-type', …), a human message, an optional structured context, and a chainable cause. No function in the package throw new Error(...) raw — everything goes through this hierarchy.
The classes are not exported at the top level from @awacloud/pdf: the only export is the pdfErrors factory descriptor. Consumers resolve the 5 classes (PdfError, ParseError, RenderError, ContractError, EncryptionError) + the isPdfError helper via runtime.resolve('pdfErrors') or pdfErrors.factory(). The classes are declared inside the factory body, so each factory() call creates a fresh set of classes: an error built from one call is not instanceof the classes of another, and that call's isPdfError returns false for it. A ModuleRuntime caches the resolved instance, so every module resolved through the same runtime shares one set of classes — catch with the classes resolved from that runtime, or compare e.code / e.name when the error may come from elsewhere.
Resolve
const errs = runtime.resolve('pdfErrors');
// Returns: { PdfError, ParseError, RenderError, ContractError, EncryptionError, isPdfError }
Stand-alone (without a ModuleRuntime):
import { pdfErrors } from '@awacloud/pdf';
const { ParseError, EncryptionError } = pdfErrors.factory();
API
| Class / method | Signature | Returns |
|---|---|---|
PdfError |
new (code: string, message: string, opts?: { context?, cause? }) |
Instance. |
ParseError |
extends PdfError |
Read-side / malformed bytes. |
RenderError |
extends PdfError |
Invalid model at write time (L1+). |
ContractError |
extends PdfError |
Consumer API contract violation. |
EncryptionError |
extends PdfError |
Crypto / signatures (L3+). |
isPdfError |
(e: any) => boolean |
true if e instanceof PdfError. |
new PdfError(code, message, opts?)
code: kebab-case (e.g. 'pdf/bad-header'). opts.context: arbitrary payload ({ offset, num, gen, raw }). opts.cause: source Error (re-throw).
Examples
Typed catch at the root
import { ModuleRuntime } from '@awacloud/fw/core/runtime.js';
import { fw_require, modules } from '@awacloud/pdf';
const rt = new ModuleRuntime();
for (const m of fw_require) rt.register(m);
for (const m of modules) rt.register(m);
const api = rt.resolve('pdf');
const { isPdfError } = rt.resolve('pdfErrors'); // the classes api throws
try {
api.read(bytes);
} catch (e) {
if (isPdfError(e)) {
console.log(e.name, e.code, e.context);
} else {
throw e;
}
}
Disambiguation by subclass
const errs = rt.resolve('pdfErrors');
try { rt.resolve('pdf').read(bytes); }
catch (e) {
if (e instanceof errs.ParseError) console.log('malformed input');
else if (e instanceof errs.ContractError) console.log('API misuse');
else throw e;
}
Errors
This page defines the codes; the modules emit them. Prefixes by domain:
pdf/tokenizer/...— emitted bypdfTokenizer.pdf/parser/...— emitted bypdfParser.pdf/xref/...— emitted bypdfXref.pdf/trailer/...— emitted bypdfTrailer.pdf/catalog/...— emitted bypdfCatalog.pdf/pages/...— emitted bypdfPages.pdf/page/...— emitted bypdfPage.pdf/document/...— emitted bypdfDocument.pdf/writer/...— emitted bypdfWriter; includespdf/writer/unresolvable-objects(RenderError,context.objects=Array<{ num, gen, code }>), thrown byassembleIndirects(model, { strict: true })when an in-use object cannot be resolved.pdf/use/...— emitted bypdf.pdf/sig/...— emitted bypdfSignature,pdfByteRange,pdfCertChain. The public-key verification path is wired and executed by construction — there is nopdf/sig/pk-verify-not-wiredcode; seepdfSignature's Errors table for the fullpdf/sig/*andpdf/sig/verify-pk/*set.pdf/ts/...— emitted bypdfTimestamp(RFC 3161) and bypdfSignature's/DocTimeStampverification branch.pdf/cert/...— emitted bypdfCertChain.pdf/crypto/...— emitted bypdfStandardV5,pdfStandardV6,pdfPermissions,pdfSecurity,pdfAesGcm.pdf/sandbox/...— emitted by the opt-inpdfSandboxmodule (bundlepdf-full).
See also
pdf— emitter ofContractErroron the.use()side.pdfDocument— propagatesParseErrorfor full reads.