Merge pull request #13886 from Snuffleupagus/BaseException-name

Re-factor the `BaseException.name` handling, and clean-up some code
This commit is contained in:
Tim van der Meij 2021-08-11 22:35:05 +02:00 committed by GitHub
commit 14a0d612c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 82 additions and 67 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 = {
@ -42,18 +43,21 @@ const StreamKind = {
function wrapReason(reason) { function wrapReason(reason) {
if ( if (
typeof PDFJSDev === "undefined" || !(
PDFJSDev.test("!PRODUCTION || TESTING")
) {
assert(
reason instanceof Error || reason instanceof Error ||
(typeof reason === "object" && reason !== null), (typeof reason === "object" && reason !== null)
'wrapReason: Expected "reason" to be a (possibly cloned) Error.' )
); ) {
} else { if (
if (typeof reason !== "object" || reason === null) { typeof PDFJSDev === "undefined" ||
return reason; PDFJSDev.test("!PRODUCTION || TESTING")
) {
throw new Error(
'wrapReason: Expected "reason" to be a (possibly cloned) Error.'
);
} }
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;

View File

@ -323,11 +323,11 @@ const PDFViewerApplication = {
*/ */
async _parseHashParameters() { async _parseHashParameters() {
if (!AppOptions.get("pdfBugEnabled")) { if (!AppOptions.get("pdfBugEnabled")) {
return undefined; return;
} }
const hash = document.location.hash.substring(1); const hash = document.location.hash.substring(1);
if (!hash) { if (!hash) {
return undefined; return;
} }
const params = parseQueryString(hash), const params = parseQueryString(hash),
waitOn = []; waitOn = [];
@ -389,11 +389,13 @@ const PDFViewerApplication = {
} }
if (waitOn.length === 0) { if (waitOn.length === 0) {
return undefined; return;
} }
return Promise.all(waitOn).catch(reason => { try {
await Promise.all(waitOn);
} catch (reason) {
console.error(`_parseHashParameters: "${reason.message}".`); console.error(`_parseHashParameters: "${reason.message}".`);
}); }
}, },
/** /**

View File

@ -152,8 +152,6 @@ class BaseViewer {
`The API version "${version}" does not match the Viewer version "${viewerVersion}".` `The API version "${version}" does not match the Viewer version "${viewerVersion}".`
); );
} }
this._name = this.constructor.name;
this.container = options.container; this.container = options.container;
this.viewer = options.viewer || options.container.firstElementChild; this.viewer = options.viewer || options.container.firstElementChild;
@ -267,9 +265,7 @@ class BaseViewer {
} }
// The intent can be to just reset a scroll position and/or scale. // The intent can be to just reset a scroll position and/or scale.
if (!this._setCurrentPageNumber(val, /* resetCurrentPageView = */ true)) { if (!this._setCurrentPageNumber(val, /* resetCurrentPageView = */ true)) {
console.error( console.error(`currentPageNumber: "${val}" is not a valid page.`);
`${this._name}.currentPageNumber: "${val}" is not a valid page.`
);
} }
} }
@ -328,9 +324,7 @@ class BaseViewer {
} }
// The intent can be to just reset a scroll position and/or scale. // The intent can be to just reset a scroll position and/or scale.
if (!this._setCurrentPageNumber(page, /* resetCurrentPageView = */ true)) { if (!this._setCurrentPageNumber(page, /* resetCurrentPageView = */ true)) {
console.error( console.error(`currentPageLabel: "${val}" is not a valid page.`);
`${this._name}.currentPageLabel: "${val}" is not a valid page.`
);
} }
} }
@ -642,7 +636,7 @@ class BaseViewer {
!(Array.isArray(labels) && this.pdfDocument.numPages === labels.length) !(Array.isArray(labels) && this.pdfDocument.numPages === labels.length)
) { ) {
this._pageLabels = null; this._pageLabels = null;
console.error(`${this._name}.setPageLabels: Invalid page labels.`); console.error(`setPageLabels: Invalid page labels.`);
} else { } else {
this._pageLabels = labels; this._pageLabels = labels;
} }
@ -808,9 +802,7 @@ class BaseViewer {
scale = Math.min(MAX_AUTO_SCALE, horizontalScale); scale = Math.min(MAX_AUTO_SCALE, horizontalScale);
break; break;
default: default:
console.error( console.error(`_setScale: "${value}" is an unknown zoom value.`);
`${this._name}._setScale: "${value}" is an unknown zoom value.`
);
return; return;
} }
this._setScaleUpdatePages(scale, value, noScroll, /* preset = */ true); this._setScaleUpdatePages(scale, value, noScroll, /* preset = */ true);
@ -875,8 +867,7 @@ class BaseViewer {
Number.isInteger(pageNumber) && this._pages[pageNumber - 1]; Number.isInteger(pageNumber) && this._pages[pageNumber - 1];
if (!pageView) { if (!pageView) {
console.error( console.error(
`${this._name}.scrollPageIntoView: ` + `scrollPageIntoView: "${pageNumber}" is not a valid pageNumber parameter.`
`"${pageNumber}" is not a valid pageNumber parameter.`
); );
return; return;
} }
@ -955,8 +946,7 @@ class BaseViewer {
break; break;
default: default:
console.error( console.error(
`${this._name}.scrollPageIntoView: ` + `scrollPageIntoView: "${destArray[1].name}" is not a valid destination type.`
`"${destArray[1].name}" is not a valid destination type.`
); );
return; return;
} }
@ -1143,9 +1133,7 @@ class BaseViewer {
pageNumber <= this.pagesCount pageNumber <= this.pagesCount
) )
) { ) {
console.error( console.error(`isPageVisible: "${pageNumber}" is not a valid page.`);
`${this._name}.isPageVisible: "${pageNumber}" is not a valid page.`
);
return false; return false;
} }
return this._getVisiblePages().views.some(function (view) { return this._getVisiblePages().views.some(function (view) {
@ -1167,9 +1155,7 @@ class BaseViewer {
pageNumber <= this.pagesCount pageNumber <= this.pagesCount
) )
) { ) {
console.error( console.error(`isPageCached: "${pageNumber}" is not a valid page.`);
`${this._name}.isPageCached: "${pageNumber}" is not a valid page.`
);
return false; return false;
} }
const pageView = this._pages[pageNumber - 1]; const pageView = this._pages[pageNumber - 1];