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

Convert the various ...Exceptions to proper classes, to reduce code duplication #11185

Merged
merged 1 commit into from
Sep 29, 2019
Merged
Show file tree
Hide file tree
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
40 changes: 7 additions & 33 deletions src/core/core_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
/* eslint no-var: error */

import { assert, warn } from '../shared/util';
import { assert, BaseException, warn } from '../shared/util';

function getLookupTableFactory(initializer) {
let lookup;
Expand All @@ -28,43 +28,17 @@ function getLookupTableFactory(initializer) {
};
}

const MissingDataException = (function MissingDataExceptionClosure() {
function MissingDataException(begin, end) {
class MissingDataException extends BaseException {
constructor(begin, end) {
super(`Missing data [${begin}, ${end})`);
this.begin = begin;
this.end = end;
this.message = `Missing data [${begin}, ${end})`;
}

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

return MissingDataException;
})();

const XRefEntryException = (function XRefEntryExceptionClosure() {
function XRefEntryException(msg) {
this.message = msg;
}

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

return XRefEntryException;
})();

const XRefParseException = (function XRefParseExceptionClosure() {
function XRefParseException(msg) {
this.message = msg;
}
}

XRefParseException.prototype = new Error();
XRefParseException.prototype.name = 'XRefParseException';
XRefParseException.constructor = XRefParseException;
class XRefEntryException extends BaseException { }

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

/**
* Get the value of an inheritable property.
Expand Down
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() {
Copy link
Collaborator Author

@Snuffleupagus Snuffleupagus Sep 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once extending Error isn't a problem any more, when SystemJS has been updated/replaced, it should be possible to simplify this even further to e.g.

class BaseException extends Error {
  constructor(message) {
    if (this.constructor === BaseException) {
      unreachable('Cannot initialize BaseException.');
    }
    super(message);
    this.name = this.constructor.name;
  }
}

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