Skip to content

Commit

Permalink
Convert the various ...Exceptions to proper classes, to reduced cod…
Browse files Browse the repository at this point in the history
…e duplication

By utilizing a base "class", things become significantly simpler. Unfortunately the new `BaseException` cannot be a proper ES6 class and just extend `Error`, since the SystemJS dependency doesn't seem to play well with that.
Note also that we (generally) need to keep the `name` property on the actual `...Exception` object, rather than on its prototype, since the property will otherwise be dropped during the structured cloning used with `postMessage`.
  • Loading branch information
Snuffleupagus committed Sep 28, 2019
1 parent cd909c5 commit 0777f63
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 85 deletions.
18 changes: 6 additions & 12 deletions src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
/* eslint no-var: error */

import {
assert, CMapCompressionType, isString, removeNullCharacters, stringToBytes,
unreachable, Util, warn
assert, BaseException, CMapCompressionType, isString, removeNullCharacters,
stringToBytes, unreachable, Util, warn
} from '../shared/util';

const DEFAULT_LINK_REL = 'noopener noreferrer nofollow';
Expand Down Expand Up @@ -307,18 +307,12 @@ class PageViewport {
}
}

const RenderingCancelledException = (function RenderingCancelledException() {
function RenderingCancelledException(msg, type) {
this.message = msg;
class RenderingCancelledException extends BaseException {
constructor(msg, type) {
super(msg);
this.type = type;
}

RenderingCancelledException.prototype = new Error();
RenderingCancelledException.prototype.name = 'RenderingCancelledException';
RenderingCancelledException.constructor = RenderingCancelledException;

return RenderingCancelledException;
})();
}

const LinkTarget = {
NONE: 0, // Default value.
Expand Down
102 changes: 29 additions & 73 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,99 +401,54 @@ function shadow(obj, prop, value) {
return value;
}

const PasswordException = (function PasswordExceptionClosure() {
function PasswordException(msg, code) {
this.name = 'PasswordException';
this.message = msg;
this.code = code;
const BaseException = (function BaseExceptionClosure() {
function BaseException(message) {
if (this.constructor === BaseException) {
unreachable('Cannot initialize BaseException.');
}
this.message = message;
this.name = this.constructor.name;
}
BaseException.prototype = new Error();
BaseException.constructor = BaseException;

PasswordException.prototype = new Error();
PasswordException.constructor = PasswordException;

return PasswordException;
return BaseException;
})();

const UnknownErrorException = (function UnknownErrorExceptionClosure() {
function UnknownErrorException(msg, details) {
this.name = 'UnknownErrorException';
this.message = msg;
this.details = details;
}

UnknownErrorException.prototype = new Error();
UnknownErrorException.constructor = UnknownErrorException;

return UnknownErrorException;
})();

const InvalidPDFException = (function InvalidPDFExceptionClosure() {
function InvalidPDFException(msg) {
this.name = 'InvalidPDFException';
this.message = msg;
class PasswordException extends BaseException {
constructor(msg, code) {
super(msg);
this.code = code;
}
}

InvalidPDFException.prototype = new Error();
InvalidPDFException.constructor = InvalidPDFException;

return InvalidPDFException;
})();

const MissingPDFException = (function MissingPDFExceptionClosure() {
function MissingPDFException(msg) {
this.name = 'MissingPDFException';
this.message = msg;
class UnknownErrorException extends BaseException {
constructor(msg, details) {
super(msg);
this.details = details;
}
}

MissingPDFException.prototype = new Error();
MissingPDFException.constructor = MissingPDFException;
class InvalidPDFException extends BaseException { }

return MissingPDFException;
})();
class MissingPDFException extends BaseException { }

const UnexpectedResponseException =
(function UnexpectedResponseExceptionClosure() {
function UnexpectedResponseException(msg, status) {
this.name = 'UnexpectedResponseException';
this.message = msg;
class UnexpectedResponseException extends BaseException {
constructor(msg, status) {
super(msg);
this.status = status;
}

UnexpectedResponseException.prototype = new Error();
UnexpectedResponseException.constructor = UnexpectedResponseException;

return UnexpectedResponseException;
})();
}

/**
* Error caused during parsing PDF data.
*/
const FormatError = (function FormatErrorClosure() {
function FormatError(msg) {
this.message = msg;
}

FormatError.prototype = new Error();
FormatError.prototype.name = 'FormatError';
FormatError.constructor = FormatError;

return FormatError;
})();
class FormatError extends BaseException { }

/**
* Error used to indicate task cancellation.
*/
const AbortException = (function AbortExceptionClosure() {
function AbortException(msg) {
this.name = 'AbortException';
this.message = msg;
}

AbortException.prototype = new Error();
AbortException.constructor = AbortException;

return AbortException;
})();
class AbortException extends BaseException { }

const NullCharactersRegExp = /\x00/g;

Expand Down Expand Up @@ -915,6 +870,7 @@ const createObjectURL = (function createObjectURLClosure() {
})();

export {
BaseException,
FONT_IDENTITY_MATRIX,
IDENTITY_MATRIX,
OPS,
Expand Down

0 comments on commit 0777f63

Please sign in to comment.