mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +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:
parent
7a57af12e1
commit
75cba72ca6
15 changed files with 65 additions and 92 deletions
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue