Re-factor the BaseException.name handling, and clean-up some code

Once we're finally able to get rid of SystemJS, which is unfortunately still blocked on [bug 1247687](https://bugzilla.mozilla.org/show_bug.cgi?id=1247687), we might also want to clean-up (or even completely remove) the `BaseException` abstraction and simply extend `Error` directly instead.

At that point we'd need to (explicitly) set the `name` on each class anyway, so this patch is essentially preparing for future clean-up. Furthermore, after the `BaseException` abstraction was added there's been *multiple* issues filed about third-party minification breaking our code since `this.constructor.name` is not guaranteed to always do what you intended.

While hard-coding the strings indeed feels quite unfortunate, it's likely the "best" solution to avoid the problem described above.
This commit is contained in:
Jonas Jenwald 2021-08-09 12:02:49 +02:00
parent 745d5cc819
commit 6167566f1b
8 changed files with 67 additions and 40 deletions

View File

@ -52,17 +52,29 @@ function getArrayLookupTableFactory(initializer) {
class MissingDataException extends BaseException { class MissingDataException extends BaseException {
constructor(begin, end) { constructor(begin, end) {
super(`Missing data [${begin}, ${end})`); super(`Missing data [${begin}, ${end})`, "MissingDataException");
this.begin = begin; this.begin = begin;
this.end = end; this.end = end;
} }
} }
class ParserEOFException extends BaseException {} class ParserEOFException extends BaseException {
constructor(msg) {
super(msg, "ParserEOFException");
}
}
class XRefEntryException extends BaseException {} class XRefEntryException extends BaseException {
constructor(msg) {
super(msg, "XRefEntryException");
}
}
class XRefParseException extends BaseException {} class XRefParseException extends BaseException {
constructor(msg) {
super(msg, "XRefParseException");
}
}
/** /**
* Get the value of an inheritable property. * Get the value of an inheritable property.

View File

@ -20,7 +20,7 @@ import { CCITTFaxDecoder } from "./ccitt.js";
class Jbig2Error extends BaseException { class Jbig2Error extends BaseException {
constructor(msg) { constructor(msg) {
super(`JBIG2 error: ${msg}`); super(`JBIG2 error: ${msg}`, "Jbig2Error");
} }
} }

View File

@ -18,18 +18,22 @@ import { readUint16 } from "./core_utils.js";
class JpegError extends BaseException { class JpegError extends BaseException {
constructor(msg) { constructor(msg) {
super(`JPEG error: ${msg}`); super(`JPEG error: ${msg}`, "JpegError");
} }
} }
class DNLMarkerError extends BaseException { class DNLMarkerError extends BaseException {
constructor(message, scanLines) { constructor(message, scanLines) {
super(message); super(message, "DNLMarkerError");
this.scanLines = scanLines; this.scanLines = scanLines;
} }
} }
class EOIMarkerError extends BaseException {} class EOIMarkerError extends BaseException {
constructor(msg) {
super(msg, "EOIMarkerError");
}
}
/** /**
* This code was forked from https://github.com/notmasteryet/jpgjs. * This code was forked from https://github.com/notmasteryet/jpgjs.

View File

@ -19,7 +19,7 @@ import { ArithmeticDecoder } from "./arithmetic_decoder.js";
class JpxError extends BaseException { class JpxError extends BaseException {
constructor(msg) { constructor(msg) {
super(`JPX error: ${msg}`); super(`JPX error: ${msg}`, "JpxError");
} }
} }

View File

@ -2565,17 +2565,8 @@ class WorkerTransport {
case "UnknownErrorException": case "UnknownErrorException":
reason = new UnknownErrorException(ex.message, ex.details); reason = new UnknownErrorException(ex.message, ex.details);
break; break;
} default:
if (!(reason instanceof Error)) { unreachable("DocException - expected a valid Error.");
const msg = "DocException - expected a valid Error.";
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING")
) {
unreachable(msg);
} else {
warn(msg);
}
} }
loadingTask._capability.reject(reason); loadingTask._capability.reject(reason);
}); });

View File

@ -300,7 +300,7 @@ class PageViewport {
class RenderingCancelledException extends BaseException { class RenderingCancelledException extends BaseException {
constructor(msg, type) { constructor(msg, type) {
super(msg); super(msg, "RenderingCancelledException");
this.type = type; this.type = type;
} }
} }

View File

@ -20,6 +20,7 @@ import {
MissingPDFException, MissingPDFException,
UnexpectedResponseException, UnexpectedResponseException,
UnknownErrorException, UnknownErrorException,
warn,
} from "./util.js"; } from "./util.js";
const CallbackKind = { const CallbackKind = {
@ -41,19 +42,22 @@ const StreamKind = {
}; };
function wrapReason(reason) { function wrapReason(reason) {
if (
!(
reason instanceof Error ||
(typeof reason === "object" && reason !== null)
)
) {
if ( if (
typeof PDFJSDev === "undefined" || typeof PDFJSDev === "undefined" ||
PDFJSDev.test("!PRODUCTION || TESTING") PDFJSDev.test("!PRODUCTION || TESTING")
) { ) {
assert( throw new Error(
reason instanceof Error ||
(typeof reason === "object" && reason !== null),
'wrapReason: Expected "reason" to be a (possibly cloned) Error.' 'wrapReason: Expected "reason" to be a (possibly cloned) Error.'
); );
} else {
if (typeof reason !== "object" || reason === null) {
return reason;
} }
warn('wrapReason: Expected "reason" to be a (possibly cloned) Error.');
return reason;
} }
switch (reason.name) { switch (reason.name) {
case "AbortException": case "AbortException":

View File

@ -459,12 +459,12 @@ function shadow(obj, prop, value) {
*/ */
const BaseException = (function BaseExceptionClosure() { const BaseException = (function BaseExceptionClosure() {
// eslint-disable-next-line no-shadow // eslint-disable-next-line no-shadow
function BaseException(message) { function BaseException(message, name) {
if (this.constructor === BaseException) { if (this.constructor === BaseException) {
unreachable("Cannot initialize BaseException."); unreachable("Cannot initialize BaseException.");
} }
this.message = message; this.message = message;
this.name = this.constructor.name; this.name = name;
} }
BaseException.prototype = new Error(); BaseException.prototype = new Error();
BaseException.constructor = BaseException; BaseException.constructor = BaseException;
@ -474,25 +474,33 @@ const BaseException = (function BaseExceptionClosure() {
class PasswordException extends BaseException { class PasswordException extends BaseException {
constructor(msg, code) { constructor(msg, code) {
super(msg); super(msg, "PasswordException");
this.code = code; this.code = code;
} }
} }
class UnknownErrorException extends BaseException { class UnknownErrorException extends BaseException {
constructor(msg, details) { constructor(msg, details) {
super(msg); super(msg, "UnknownErrorException");
this.details = details; this.details = details;
} }
} }
class InvalidPDFException extends BaseException {} class InvalidPDFException extends BaseException {
constructor(msg) {
super(msg, "InvalidPDFException");
}
}
class MissingPDFException extends BaseException {} class MissingPDFException extends BaseException {
constructor(msg) {
super(msg, "MissingPDFException");
}
}
class UnexpectedResponseException extends BaseException { class UnexpectedResponseException extends BaseException {
constructor(msg, status) { constructor(msg, status) {
super(msg); super(msg, "UnexpectedResponseException");
this.status = status; this.status = status;
} }
} }
@ -500,12 +508,20 @@ class UnexpectedResponseException extends BaseException {
/** /**
* Error caused during parsing PDF data. * Error caused during parsing PDF data.
*/ */
class FormatError extends BaseException {} class FormatError extends BaseException {
constructor(msg) {
super(msg, "FormatError");
}
}
/** /**
* Error used to indicate task cancellation. * Error used to indicate task cancellation.
*/ */
class AbortException extends BaseException {} class AbortException extends BaseException {
constructor(msg) {
super(msg, "AbortException");
}
}
const NullCharactersRegExp = /\x00/g; const NullCharactersRegExp = /\x00/g;