1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-24 09:08:07 +02:00

[api-major] Replace MissingPDFException and UnexpectedResponseException with one exception

These old exceptions have a fair amount of overlap given how/where they are being used, which is likely because they were introduced at different points in time, hence we can shorten and simplify the code by replacing them with a more general `ResponseException` instead.

Besides an error message, the new `ResponseException` instances also include:
 - A numeric `status` field containing the server response status, similar to the old `UnexpectedResponseException`.

 - A boolean `missing` field, to allow easily detecting the situations where `MissingPDFException` was previously thrown.
This commit is contained in:
Jonas Jenwald 2024-12-28 14:20:48 +01:00
parent 7a57af12e1
commit 75cba72ca6
15 changed files with 65 additions and 92 deletions

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,