Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add prototype to errors #1439

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/api/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class EncryptedPDFError extends Error {
const msg =
'Input document to `PDFDocument.load` is encrypted. You can use `PDFDocument.load(..., { ignoreEncryption: true })` if you wish to load the document anyways.';
super(msg);
(<any>Object).setPrototypeOf(this, EncryptedPDFError.prototype);
}
}

Expand All @@ -15,6 +16,7 @@ export class FontkitNotRegisteredError extends Error {
const msg =
'Input to `PDFDocument.embedFont` was a custom font, but no `fontkit` instance was found. You must register a `fontkit` instance with `PDFDocument.registerFontkit(...)` before embedding custom fonts.';
super(msg);
(<any>Object).setPrototypeOf(this, FontkitNotRegisteredError.prototype);
}
}

Expand All @@ -24,6 +26,7 @@ export class ForeignPageError extends Error {
const msg =
'A `page` passed to `PDFDocument.addPage` or `PDFDocument.insertPage` was from a different (foreign) PDF document. If you want to copy pages from one PDFDocument to another, you must use `PDFDocument.copyPages(...)` to copy the pages before adding or inserting them.';
super(msg);
(<any>Object).setPrototypeOf(this, ForeignPageError.prototype);
}
}

Expand All @@ -33,13 +36,15 @@ export class RemovePageFromEmptyDocumentError extends Error {
const msg =
'PDFDocument has no pages so `PDFDocument.removePage` cannot be called';
super(msg);
(<any>Object).setPrototypeOf(this, RemovePageFromEmptyDocumentError.prototype);
}
}

export class NoSuchFieldError extends Error {
constructor(name: string) {
const msg = `PDFDocument has no form field with the name "${name}"`;
super(msg);
(<any>Object).setPrototypeOf(this, NoSuchFieldError.prototype);
}
}

Expand All @@ -51,61 +56,70 @@ export class UnexpectedFieldTypeError extends Error {
`Expected field "${name}" to be of type ${expectedType}, ` +
`but it is actually of type ${actualType}`;
super(msg);
(<any>Object).setPrototypeOf(this, UnexpectedFieldTypeError.prototype);
}
}

export class MissingOnValueCheckError extends Error {
constructor(onValue: any) {
const msg = `Failed to select check box due to missing onValue: "${onValue}"`;
super(msg);
(<any>Object).setPrototypeOf(this, MissingOnValueCheckError.prototype);
}
}

export class FieldAlreadyExistsError extends Error {
constructor(name: string) {
const msg = `A field already exists with the specified name: "${name}"`;
super(msg);
(<any>Object).setPrototypeOf(this, FieldAlreadyExistsError.prototype);
}
}

export class InvalidFieldNamePartError extends Error {
constructor(namePart: string) {
const msg = `Field name contains invalid component: "${namePart}"`;
super(msg);
(<any>Object).setPrototypeOf(this, InvalidFieldNamePartError.prototype);
}
}

export class FieldExistsAsNonTerminalError extends Error {
constructor(name: string) {
const msg = `A non-terminal field already exists with the specified name: "${name}"`;
super(msg);
(<any>Object).setPrototypeOf(this, FieldExistsAsNonTerminalError.prototype);
}
}

export class RichTextFieldReadError extends Error {
constructor(fieldName: string) {
const msg = `Reading rich text fields is not supported: Attempted to read rich text field: ${fieldName}`;
super(msg);
(<any>Object).setPrototypeOf(this, RichTextFieldReadError.prototype);
}
}

export class CombedTextLayoutError extends Error {
constructor(lineLength: number, cellCount: number) {
const msg = `Failed to layout combed text as lineLength=${lineLength} is greater than cellCount=${cellCount}`;
super(msg);
(<any>Object).setPrototypeOf(this, CombedTextLayoutError.prototype);
}
}

export class ExceededMaxLengthError extends Error {
constructor(textLength: number, maxLength: number, name: string) {
const msg = `Attempted to set text with length=${textLength} for TextField with maxLength=${maxLength} and name=${name}`;
super(msg);
(<any>Object).setPrototypeOf(this, ExceededMaxLengthError.prototype);
}
}

export class InvalidMaxLengthError extends Error {
constructor(textLength: number, maxLength: number, name: string) {
const msg = `Attempted to set maxLength=${maxLength}, which is less than ${textLength}, the length of this field's current value (name=${name})`;
super(msg);
(<any>Object).setPrototypeOf(this, InvalidMaxLengthError.prototype);
}
}