1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 22:58:07 +02:00

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";
if (reason instanceof pdfjsLib.InvalidPDFException) {
key = "pdfjs-invalid-file-error";
} else if (reason instanceof pdfjsLib.MissingPDFException) {
key = "pdfjs-missing-file-error";
} else if (reason instanceof pdfjsLib.UnexpectedResponseException) {
key = "pdfjs-unexpected-response-error";
} else if (reason instanceof pdfjsLib.ResponseException) {
key = reason.missing
? "pdfjs-missing-file-error"
: "pdfjs-unexpected-response-error";
}
self.l10n.get(key).then(msg => {
self.error(msg, { message: reason?.message });

View file

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

View file

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

View file

@ -13,11 +13,7 @@
* limitations under the License.
*/
import {
assert,
MissingPDFException,
UnexpectedResponseException,
} from "../shared/util.js";
import { assert, ResponseException } from "../shared/util.js";
import { getFilenameFromContentDispositionHeader } from "./content_disposition.js";
import { isPdfFile } from "./display_utils.js";
@ -108,13 +104,11 @@ function extractFilenameFromHeader(responseHeaders) {
return null;
}
function createResponseStatusError(status, url) {
if (status === 404 || (status === 0 && url.startsWith("file:"))) {
return new MissingPDFException('Missing PDF "' + url + '".');
}
return new UnexpectedResponseException(
function createResponseError(status, url) {
return new ResponseException(
`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 {
createHeaders,
createResponseStatusError,
createResponseError,
extractFilenameFromHeader,
getResponseOrigin,
validateRangeRequestCapabilities,

View file

@ -14,7 +14,8 @@
*/
/* 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")) {
throw new Error(
@ -111,7 +112,7 @@ class PDFNodeStreamFsFullReader {
},
error => {
if (error.code === "ENOENT") {
error = new MissingPDFException(`Missing PDF "${this._url}".`);
error = createResponseError(/* status = */ 0, this._url.href);
}
this._storedError = error;
this._headersCapability.reject(error);

View file

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

View file

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

View file

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

View file

@ -20,12 +20,12 @@ import {
ImageKind,
InvalidPDFException,
isNodeJS,
MissingPDFException,
objectSize,
OPS,
PasswordException,
PasswordResponses,
PermissionFlag,
ResponseException,
UnknownErrorException,
} from "../../src/shared/util.js";
import {
@ -297,7 +297,9 @@ describe("api", function () {
// Shouldn't get here.
expect(false).toEqual(true);
} 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();

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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