diff --git a/src/display/display_utils.js b/src/display/display_utils.js index a160db0b627cf2..e2c47b3cfb4279 100644 --- a/src/display/display_utils.js +++ b/src/display/display_utils.js @@ -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'; @@ -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. diff --git a/src/shared/util.js b/src/shared/util.js index 4fe84d709916f9..6de6d297a6a2ef 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -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; @@ -915,6 +870,7 @@ const createObjectURL = (function createObjectURLClosure() { })(); export { + BaseException, FONT_IDENTITY_MATRIX, IDENTITY_MATRIX, OPS,