Merge pull request #19264 from Snuffleupagus/ResponseException

[api-major] Replace `MissingPDFException` and `UnexpectedResponseException` with one exception
This commit is contained in:
Jonas Jenwald 2025-01-16 23:23:16 +01:00 committed by GitHub
commit 12d114bccb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 65 additions and 92 deletions

View File

@ -91,10 +91,10 @@ const PDFViewerApplication = {
let key = "pdfjs-loading-error"; let key = "pdfjs-loading-error";
if (reason instanceof pdfjsLib.InvalidPDFException) { if (reason instanceof pdfjsLib.InvalidPDFException) {
key = "pdfjs-invalid-file-error"; key = "pdfjs-invalid-file-error";
} else if (reason instanceof pdfjsLib.MissingPDFException) { } else if (reason instanceof pdfjsLib.ResponseException) {
key = "pdfjs-missing-file-error"; key = reason.missing
} else if (reason instanceof pdfjsLib.UnexpectedResponseException) { ? "pdfjs-missing-file-error"
key = "pdfjs-unexpected-response-error"; : "pdfjs-unexpected-response-error";
} }
self.l10n.get(key).then(msg => { self.l10n.get(key).then(msg => {
self.error(msg, { message: reason?.message }); self.error(msg, { message: reason?.message });

View File

@ -16,7 +16,7 @@
import { AbortException, assert, warn } from "../shared/util.js"; import { AbortException, assert, warn } from "../shared/util.js";
import { import {
createHeaders, createHeaders,
createResponseStatusError, createResponseError,
extractFilenameFromHeader, extractFilenameFromHeader,
getResponseOrigin, getResponseOrigin,
validateRangeRequestCapabilities, validateRangeRequestCapabilities,
@ -127,7 +127,7 @@ class PDFFetchStreamReader {
stream._responseOrigin = getResponseOrigin(response.url); stream._responseOrigin = getResponseOrigin(response.url);
if (!validateResponseStatus(response.status)) { if (!validateResponseStatus(response.status)) {
throw createResponseStatusError(response.status, url); throw createResponseError(response.status, url);
} }
this._reader = response.body.getReader(); this._reader = response.body.getReader();
this._headersCapability.resolve(); this._headersCapability.resolve();
@ -230,7 +230,7 @@ class PDFFetchStreamRangeReader {
); );
} }
if (!validateResponseStatus(response.status)) { if (!validateResponseStatus(response.status)) {
throw createResponseStatusError(response.status, url); throw createResponseError(response.status, url);
} }
this._readCapability.resolve(); this._readCapability.resolve();
this._reader = response.body.getReader(); this._reader = response.body.getReader();

View File

@ -16,7 +16,7 @@
import { assert, stringToBytes, warn } from "../shared/util.js"; import { assert, stringToBytes, warn } from "../shared/util.js";
import { import {
createHeaders, createHeaders,
createResponseStatusError, createResponseError,
extractFilenameFromHeader, extractFilenameFromHeader,
getResponseOrigin, getResponseOrigin,
validateRangeRequestCapabilities, validateRangeRequestCapabilities,
@ -329,7 +329,7 @@ class PDFNetworkStreamFullRequestReader {
} }
_onError(status) { _onError(status) {
this._storedError = createResponseStatusError(status, this._url); this._storedError = createResponseError(status, this._url);
this._headersCapability.reject(this._storedError); this._headersCapability.reject(this._storedError);
for (const requestCapability of this._requests) { for (const requestCapability of this._requests) {
requestCapability.reject(this._storedError); requestCapability.reject(this._storedError);
@ -454,7 +454,7 @@ class PDFNetworkStreamRangeRequestReader {
} }
_onError(status) { _onError(status) {
this._storedError ??= createResponseStatusError(status, this._url); this._storedError ??= createResponseError(status, this._url);
for (const requestCapability of this._requests) { for (const requestCapability of this._requests) {
requestCapability.reject(this._storedError); requestCapability.reject(this._storedError);
} }

View File

@ -13,11 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { import { assert, ResponseException } from "../shared/util.js";
assert,
MissingPDFException,
UnexpectedResponseException,
} from "../shared/util.js";
import { getFilenameFromContentDispositionHeader } from "./content_disposition.js"; import { getFilenameFromContentDispositionHeader } from "./content_disposition.js";
import { isPdfFile } from "./display_utils.js"; import { isPdfFile } from "./display_utils.js";
@ -108,13 +104,11 @@ function extractFilenameFromHeader(responseHeaders) {
return null; return null;
} }
function createResponseStatusError(status, url) { function createResponseError(status, url) {
if (status === 404 || (status === 0 && url.startsWith("file:"))) { return new ResponseException(
return new MissingPDFException('Missing PDF "' + url + '".');
}
return new UnexpectedResponseException(
`Unexpected server response (${status}) while retrieving PDF "${url}".`, `Unexpected server response (${status}) while retrieving PDF "${url}".`,
status status,
/* missing = */ status === 404 || (status === 0 && url.startsWith("file:"))
); );
} }
@ -124,7 +118,7 @@ function validateResponseStatus(status) {
export { export {
createHeaders, createHeaders,
createResponseStatusError, createResponseError,
extractFilenameFromHeader, extractFilenameFromHeader,
getResponseOrigin, getResponseOrigin,
validateRangeRequestCapabilities, validateRangeRequestCapabilities,

View File

@ -14,7 +14,8 @@
*/ */
/* globals process */ /* globals process */
import { AbortException, assert, MissingPDFException } from "../shared/util.js"; import { AbortException, assert } from "../shared/util.js";
import { createResponseError } from "./network_utils.js";
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error( throw new Error(
@ -111,7 +112,7 @@ class PDFNodeStreamFsFullReader {
}, },
error => { error => {
if (error.code === "ENOENT") { if (error.code === "ENOENT") {
error = new MissingPDFException(`Missing PDF "${this._url}".`); error = createResponseError(/* status = */ 0, this._url.href);
} }
this._storedError = error; this._storedError = error;
this._headersCapability.reject(error); this._headersCapability.reject(error);

View File

@ -31,13 +31,12 @@ import {
FeatureTest, FeatureTest,
ImageKind, ImageKind,
InvalidPDFException, InvalidPDFException,
MissingPDFException,
normalizeUnicode, normalizeUnicode,
OPS, OPS,
PasswordResponses, PasswordResponses,
PermissionFlag, PermissionFlag,
ResponseException,
shadow, shadow,
UnexpectedResponseException,
Util, Util,
VerbosityLevel, VerbosityLevel,
} from "./shared/util.js"; } from "./shared/util.js";
@ -112,7 +111,6 @@ export {
InvalidPDFException, InvalidPDFException,
isDataScheme, isDataScheme,
isPdfFile, isPdfFile,
MissingPDFException,
noContextMenu, noContextMenu,
normalizeUnicode, normalizeUnicode,
OPS, OPS,
@ -124,12 +122,12 @@ export {
PermissionFlag, PermissionFlag,
PixelsPerInch, PixelsPerInch,
RenderingCancelledException, RenderingCancelledException,
ResponseException,
setLayerDimensions, setLayerDimensions,
shadow, shadow,
stopEvent, stopEvent,
TextLayer, TextLayer,
TouchManager, TouchManager,
UnexpectedResponseException,
Util, Util,
VerbosityLevel, VerbosityLevel,
version, version,

View File

@ -17,9 +17,8 @@ import {
AbortException, AbortException,
assert, assert,
InvalidPDFException, InvalidPDFException,
MissingPDFException,
PasswordException, PasswordException,
UnexpectedResponseException, ResponseException,
UnknownErrorException, UnknownErrorException,
unreachable, unreachable,
} from "./util.js"; } from "./util.js";
@ -46,9 +45,8 @@ function wrapReason(ex) {
if ( if (
ex instanceof AbortException || ex instanceof AbortException ||
ex instanceof InvalidPDFException || ex instanceof InvalidPDFException ||
ex instanceof MissingPDFException ||
ex instanceof PasswordException || ex instanceof PasswordException ||
ex instanceof UnexpectedResponseException || ex instanceof ResponseException ||
ex instanceof UnknownErrorException ex instanceof UnknownErrorException
) { ) {
// Avoid re-creating the exception when its type is already correct. // Avoid re-creating the exception when its type is already correct.
@ -65,12 +63,10 @@ function wrapReason(ex) {
return new AbortException(ex.message); return new AbortException(ex.message);
case "InvalidPDFException": case "InvalidPDFException":
return new InvalidPDFException(ex.message); return new InvalidPDFException(ex.message);
case "MissingPDFException":
return new MissingPDFException(ex.message);
case "PasswordException": case "PasswordException":
return new PasswordException(ex.message, ex.code); return new PasswordException(ex.message, ex.code);
case "UnexpectedResponseException": case "ResponseException":
return new UnexpectedResponseException(ex.message, ex.status); return new ResponseException(ex.message, ex.status, ex.missing);
case "UnknownErrorException": case "UnknownErrorException":
return new UnknownErrorException(ex.message, ex.details); return new UnknownErrorException(ex.message, ex.details);
} }

View File

@ -501,16 +501,11 @@ class InvalidPDFException extends BaseException {
} }
} }
class MissingPDFException extends BaseException { class ResponseException extends BaseException {
constructor(msg) { constructor(msg, status, missing) {
super(msg, "MissingPDFException"); super(msg, "ResponseException");
}
}
class UnexpectedResponseException extends BaseException {
constructor(msg, status) {
super(msg, "UnexpectedResponseException");
this.status = status; this.status = status;
this.missing = missing;
} }
} }
@ -1161,7 +1156,6 @@ export {
LINE_DESCENT_FACTOR, LINE_DESCENT_FACTOR,
LINE_FACTOR, LINE_FACTOR,
MAX_IMAGE_SIZE_TO_CACHE, MAX_IMAGE_SIZE_TO_CACHE,
MissingPDFException,
normalizeUnicode, normalizeUnicode,
objectFromMap, objectFromMap,
objectSize, objectSize,
@ -1171,6 +1165,7 @@ export {
PasswordResponses, PasswordResponses,
PermissionFlag, PermissionFlag,
RenderingIntentFlag, RenderingIntentFlag,
ResponseException,
setVerbosityLevel, setVerbosityLevel,
shadow, shadow,
string32, string32,
@ -1180,7 +1175,6 @@ export {
TextRenderingMode, TextRenderingMode,
toBase64Util, toBase64Util,
toHexUtil, toHexUtil,
UnexpectedResponseException,
UnknownErrorException, UnknownErrorException,
unreachable, unreachable,
utf8StringToString, utf8StringToString,

View File

@ -20,12 +20,12 @@ import {
ImageKind, ImageKind,
InvalidPDFException, InvalidPDFException,
isNodeJS, isNodeJS,
MissingPDFException,
objectSize, objectSize,
OPS, OPS,
PasswordException, PasswordException,
PasswordResponses, PasswordResponses,
PermissionFlag, PermissionFlag,
ResponseException,
UnknownErrorException, UnknownErrorException,
} from "../../src/shared/util.js"; } from "../../src/shared/util.js";
import { import {
@ -297,7 +297,9 @@ describe("api", function () {
// Shouldn't get here. // Shouldn't get here.
expect(false).toEqual(true); expect(false).toEqual(true);
} catch (reason) { } catch (reason) {
expect(reason instanceof MissingPDFException).toEqual(true); expect(reason instanceof ResponseException).toEqual(true);
expect(reason.status).toEqual(isNodeJS ? 0 : 404);
expect(reason.missing).toEqual(true);
} }
await loadingTask.destroy(); await loadingTask.destroy();

View File

@ -13,10 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { import { AbortException, ResponseException } from "../../src/shared/util.js";
AbortException,
UnexpectedResponseException,
} from "../../src/shared/util.js";
import { PDFNetworkStream } from "../../src/display/network.js"; import { PDFNetworkStream } from "../../src/display/network.js";
import { testCrossOriginRedirects } from "./common_pdfstream_tests.js"; import { testCrossOriginRedirects } from "./common_pdfstream_tests.js";
import { TestPdfsServer } from "./test_utils.js"; import { TestPdfsServer } from "./test_utils.js";
@ -155,8 +152,9 @@ describe("network", function () {
// Shouldn't get here. // Shouldn't get here.
expect(false).toEqual(true); expect(false).toEqual(true);
} catch (ex) { } catch (ex) {
expect(ex instanceof UnexpectedResponseException).toEqual(true); expect(ex instanceof ResponseException).toEqual(true);
expect(ex.status).toEqual(0); expect(ex.status).toEqual(0);
expect(ex.missing).toEqual(false);
} }
} }

View File

@ -15,15 +15,12 @@
import { import {
createHeaders, createHeaders,
createResponseStatusError, createResponseError,
extractFilenameFromHeader, extractFilenameFromHeader,
validateRangeRequestCapabilities, validateRangeRequestCapabilities,
validateResponseStatus, validateResponseStatus,
} from "../../src/display/network_utils.js"; } from "../../src/display/network_utils.js";
import { import { ResponseException } from "../../src/shared/util.js";
MissingPDFException,
UnexpectedResponseException,
} from "../../src/shared/util.js";
describe("network_utils", function () { describe("network_utils", function () {
describe("createHeaders", function () { describe("createHeaders", function () {
@ -370,31 +367,28 @@ describe("network_utils", function () {
}); });
}); });
describe("createResponseStatusError", function () { describe("createResponseError", function () {
it("handles missing PDF file responses", function () { function testCreateResponseError(url, status, missing) {
expect(createResponseStatusError(404, "https://foo.com/bar.pdf")).toEqual( const error = createResponseError(status, url);
new MissingPDFException('Missing PDF "https://foo.com/bar.pdf".')
);
expect(createResponseStatusError(0, "file://foo.pdf")).toEqual( expect(error instanceof ResponseException).toEqual(true);
new MissingPDFException('Missing PDF "file://foo.pdf".') expect(error.message).toEqual(
`Unexpected server response (${status}) while retrieving PDF "${url}".`
); );
expect(error.status).toEqual(status);
expect(error.missing).toEqual(missing);
}
it("handles missing PDF file responses", function () {
testCreateResponseError("https://foo.com/bar.pdf", 404, true);
testCreateResponseError("file://foo.pdf", 0, true);
}); });
it("handles unexpected responses", function () { it("handles unexpected responses", function () {
expect(createResponseStatusError(302, "https://foo.com/bar.pdf")).toEqual( testCreateResponseError("https://foo.com/bar.pdf", 302, false);
new UnexpectedResponseException(
"Unexpected server response (302) while retrieving PDF " +
'"https://foo.com/bar.pdf".'
)
);
expect(createResponseStatusError(0, "https://foo.com/bar.pdf")).toEqual( testCreateResponseError("https://foo.com/bar.pdf", 0, false);
new UnexpectedResponseException(
"Unexpected server response (0) while retrieving PDF " +
'"https://foo.com/bar.pdf".'
)
);
}); });
}); });

View File

@ -23,13 +23,12 @@ import {
ImageKind, ImageKind,
InvalidPDFException, InvalidPDFException,
isNodeJS, isNodeJS,
MissingPDFException,
normalizeUnicode, normalizeUnicode,
OPS, OPS,
PasswordResponses, PasswordResponses,
PermissionFlag, PermissionFlag,
ResponseException,
shadow, shadow,
UnexpectedResponseException,
Util, Util,
VerbosityLevel, VerbosityLevel,
} from "../../src/shared/util.js"; } from "../../src/shared/util.js";
@ -90,7 +89,6 @@ const expectedAPI = Object.freeze({
InvalidPDFException, InvalidPDFException,
isDataScheme, isDataScheme,
isPdfFile, isPdfFile,
MissingPDFException,
noContextMenu, noContextMenu,
normalizeUnicode, normalizeUnicode,
OPS, OPS,
@ -102,12 +100,12 @@ const expectedAPI = Object.freeze({
PermissionFlag, PermissionFlag,
PixelsPerInch, PixelsPerInch,
RenderingCancelledException, RenderingCancelledException,
ResponseException,
setLayerDimensions, setLayerDimensions,
shadow, shadow,
stopEvent, stopEvent,
TextLayer, TextLayer,
TouchManager, TouchManager,
UnexpectedResponseException,
Util, Util,
VerbosityLevel, VerbosityLevel,
version, version,

View File

@ -50,12 +50,11 @@ import {
InvalidPDFException, InvalidPDFException,
isDataScheme, isDataScheme,
isPdfFile, isPdfFile,
MissingPDFException,
PDFWorker, PDFWorker,
ResponseException,
shadow, shadow,
stopEvent, stopEvent,
TouchManager, TouchManager,
UnexpectedResponseException,
version, version,
} from "pdfjs-lib"; } from "pdfjs-lib";
import { AppOptions, OptionKind } from "./app_options.js"; import { AppOptions, OptionKind } from "./app_options.js";
@ -1108,10 +1107,10 @@ const PDFViewerApplication = {
let key = "pdfjs-loading-error"; let key = "pdfjs-loading-error";
if (reason instanceof InvalidPDFException) { if (reason instanceof InvalidPDFException) {
key = "pdfjs-invalid-file-error"; key = "pdfjs-invalid-file-error";
} else if (reason instanceof MissingPDFException) { } else if (reason instanceof ResponseException) {
key = "pdfjs-missing-file-error"; key = reason.missing
} else if (reason instanceof UnexpectedResponseException) { ? "pdfjs-missing-file-error"
key = "pdfjs-unexpected-response-error"; : "pdfjs-unexpected-response-error";
} }
return this._documentError(key, { message: reason.message }).then( return this._documentError(key, { message: reason.message }).then(
() => { () => {

View File

@ -442,6 +442,7 @@ const defaultOptions = {
: "../web/wasm/", : "../web/wasm/",
kind: OptionKind.API, kind: OptionKind.API,
}, },
workerPort: { workerPort: {
/** @type {Object} */ /** @type {Object} */
value: null, value: null,

View File

@ -37,7 +37,6 @@ const {
InvalidPDFException, InvalidPDFException,
isDataScheme, isDataScheme,
isPdfFile, isPdfFile,
MissingPDFException,
noContextMenu, noContextMenu,
normalizeUnicode, normalizeUnicode,
OPS, OPS,
@ -49,12 +48,12 @@ const {
PermissionFlag, PermissionFlag,
PixelsPerInch, PixelsPerInch,
RenderingCancelledException, RenderingCancelledException,
ResponseException,
setLayerDimensions, setLayerDimensions,
shadow, shadow,
stopEvent, stopEvent,
TextLayer, TextLayer,
TouchManager, TouchManager,
UnexpectedResponseException,
Util, Util,
VerbosityLevel, VerbosityLevel,
version, version,
@ -85,7 +84,6 @@ export {
InvalidPDFException, InvalidPDFException,
isDataScheme, isDataScheme,
isPdfFile, isPdfFile,
MissingPDFException,
noContextMenu, noContextMenu,
normalizeUnicode, normalizeUnicode,
OPS, OPS,
@ -97,12 +95,12 @@ export {
PermissionFlag, PermissionFlag,
PixelsPerInch, PixelsPerInch,
RenderingCancelledException, RenderingCancelledException,
ResponseException,
setLayerDimensions, setLayerDimensions,
shadow, shadow,
stopEvent, stopEvent,
TextLayer, TextLayer,
TouchManager, TouchManager,
UnexpectedResponseException,
Util, Util,
VerbosityLevel, VerbosityLevel,
version, version,