Merge pull request #19259 from Snuffleupagus/more-wrapReason
Reduce duplication when handling "DocException" and "PasswordRequest" messages
This commit is contained in:
commit
8a50d2d302
@ -18,14 +18,10 @@ import {
|
|||||||
assert,
|
assert,
|
||||||
getVerbosityLevel,
|
getVerbosityLevel,
|
||||||
info,
|
info,
|
||||||
InvalidPDFException,
|
|
||||||
isNodeJS,
|
isNodeJS,
|
||||||
MissingPDFException,
|
|
||||||
PasswordException,
|
PasswordException,
|
||||||
setVerbosityLevel,
|
setVerbosityLevel,
|
||||||
stringToPDFString,
|
stringToPDFString,
|
||||||
UnexpectedResponseException,
|
|
||||||
UnknownErrorException,
|
|
||||||
VerbosityLevel,
|
VerbosityLevel,
|
||||||
warn,
|
warn,
|
||||||
} from "../shared/util.js";
|
} from "../shared/util.js";
|
||||||
@ -36,10 +32,10 @@ import {
|
|||||||
} from "./core_utils.js";
|
} from "./core_utils.js";
|
||||||
import { Dict, isDict, Ref, RefSetCache } from "./primitives.js";
|
import { Dict, isDict, Ref, RefSetCache } from "./primitives.js";
|
||||||
import { LocalPdfManager, NetworkPdfManager } from "./pdf_manager.js";
|
import { LocalPdfManager, NetworkPdfManager } from "./pdf_manager.js";
|
||||||
|
import { MessageHandler, wrapReason } from "../shared/message_handler.js";
|
||||||
import { AnnotationFactory } from "./annotation.js";
|
import { AnnotationFactory } from "./annotation.js";
|
||||||
import { clearGlobalCaches } from "./cleanup_helper.js";
|
import { clearGlobalCaches } from "./cleanup_helper.js";
|
||||||
import { incrementalUpdate } from "./writer.js";
|
import { incrementalUpdate } from "./writer.js";
|
||||||
import { MessageHandler } from "../shared/message_handler.js";
|
|
||||||
import { PDFWorkerStream } from "./worker_stream.js";
|
import { PDFWorkerStream } from "./worker_stream.js";
|
||||||
import { StructTreeRoot } from "./struct_tree.js";
|
import { StructTreeRoot } from "./struct_tree.js";
|
||||||
|
|
||||||
@ -347,18 +343,9 @@ class WorkerMessageHandler {
|
|||||||
finishWorkerTask(task);
|
finishWorkerTask(task);
|
||||||
handler.send("DocException", ex);
|
handler.send("DocException", ex);
|
||||||
});
|
});
|
||||||
} else if (
|
|
||||||
ex instanceof InvalidPDFException ||
|
|
||||||
ex instanceof MissingPDFException ||
|
|
||||||
ex instanceof UnexpectedResponseException ||
|
|
||||||
ex instanceof UnknownErrorException
|
|
||||||
) {
|
|
||||||
handler.send("DocException", ex);
|
|
||||||
} else {
|
} else {
|
||||||
handler.send(
|
// Ensure that we always fallback to `UnknownErrorException`.
|
||||||
"DocException",
|
handler.send("DocException", wrapReason(ex));
|
||||||
new UnknownErrorException(ex.message, ex.toString())
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -24,17 +24,12 @@ import {
|
|||||||
FeatureTest,
|
FeatureTest,
|
||||||
getVerbosityLevel,
|
getVerbosityLevel,
|
||||||
info,
|
info,
|
||||||
InvalidPDFException,
|
|
||||||
isNodeJS,
|
isNodeJS,
|
||||||
MAX_IMAGE_SIZE_TO_CACHE,
|
MAX_IMAGE_SIZE_TO_CACHE,
|
||||||
MissingPDFException,
|
|
||||||
PasswordException,
|
|
||||||
RenderingIntentFlag,
|
RenderingIntentFlag,
|
||||||
setVerbosityLevel,
|
setVerbosityLevel,
|
||||||
shadow,
|
shadow,
|
||||||
stringToBytes,
|
stringToBytes,
|
||||||
UnexpectedResponseException,
|
|
||||||
UnknownErrorException,
|
|
||||||
unreachable,
|
unreachable,
|
||||||
warn,
|
warn,
|
||||||
} from "../shared/util.js";
|
} from "../shared/util.js";
|
||||||
@ -51,6 +46,7 @@ import {
|
|||||||
RenderingCancelledException,
|
RenderingCancelledException,
|
||||||
StatTimer,
|
StatTimer,
|
||||||
} from "./display_utils.js";
|
} from "./display_utils.js";
|
||||||
|
import { MessageHandler, wrapReason } from "../shared/message_handler.js";
|
||||||
import {
|
import {
|
||||||
NodeCanvasFactory,
|
NodeCanvasFactory,
|
||||||
NodeCMapReaderFactory,
|
NodeCMapReaderFactory,
|
||||||
@ -63,7 +59,6 @@ import { DOMCMapReaderFactory } from "display-cmap_reader_factory";
|
|||||||
import { DOMFilterFactory } from "./filter_factory.js";
|
import { DOMFilterFactory } from "./filter_factory.js";
|
||||||
import { DOMStandardFontDataFactory } from "display-standard_fontdata_factory";
|
import { DOMStandardFontDataFactory } from "display-standard_fontdata_factory";
|
||||||
import { GlobalWorkerOptions } from "./worker_options.js";
|
import { GlobalWorkerOptions } from "./worker_options.js";
|
||||||
import { MessageHandler } from "../shared/message_handler.js";
|
|
||||||
import { Metadata } from "./metadata.js";
|
import { Metadata } from "./metadata.js";
|
||||||
import { OptionalContentConfig } from "./optional_content_config.js";
|
import { OptionalContentConfig } from "./optional_content_config.js";
|
||||||
import { PDFDataTransportStream } from "./transport_stream.js";
|
import { PDFDataTransportStream } from "./transport_stream.js";
|
||||||
@ -2731,34 +2726,18 @@ class WorkerTransport {
|
|||||||
loadingTask._capability.resolve(new PDFDocumentProxy(pdfInfo, this));
|
loadingTask._capability.resolve(new PDFDocumentProxy(pdfInfo, this));
|
||||||
});
|
});
|
||||||
|
|
||||||
messageHandler.on("DocException", function (ex) {
|
messageHandler.on("DocException", ex => {
|
||||||
let reason;
|
loadingTask._capability.reject(wrapReason(ex));
|
||||||
switch (ex.name) {
|
|
||||||
case "PasswordException":
|
|
||||||
reason = new PasswordException(ex.message, ex.code);
|
|
||||||
break;
|
|
||||||
case "InvalidPDFException":
|
|
||||||
reason = new InvalidPDFException(ex.message);
|
|
||||||
break;
|
|
||||||
case "MissingPDFException":
|
|
||||||
reason = new MissingPDFException(ex.message);
|
|
||||||
break;
|
|
||||||
case "UnexpectedResponseException":
|
|
||||||
reason = new UnexpectedResponseException(ex.message, ex.status);
|
|
||||||
break;
|
|
||||||
case "UnknownErrorException":
|
|
||||||
reason = new UnknownErrorException(ex.message, ex.details);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
unreachable("DocException - expected a valid Error.");
|
|
||||||
}
|
|
||||||
loadingTask._capability.reject(reason);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
messageHandler.on("PasswordRequest", exception => {
|
messageHandler.on("PasswordRequest", ex => {
|
||||||
this.#passwordCapability = Promise.withResolvers();
|
this.#passwordCapability = Promise.withResolvers();
|
||||||
|
|
||||||
if (loadingTask.onPassword) {
|
try {
|
||||||
|
if (!loadingTask.onPassword) {
|
||||||
|
throw wrapReason(ex);
|
||||||
|
}
|
||||||
|
|
||||||
const updatePassword = password => {
|
const updatePassword = password => {
|
||||||
if (password instanceof Error) {
|
if (password instanceof Error) {
|
||||||
this.#passwordCapability.reject(password);
|
this.#passwordCapability.reject(password);
|
||||||
@ -2766,15 +2745,9 @@ class WorkerTransport {
|
|||||||
this.#passwordCapability.resolve({ password });
|
this.#passwordCapability.resolve({ password });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
try {
|
loadingTask.onPassword(updatePassword, ex.code);
|
||||||
loadingTask.onPassword(updatePassword, exception.code);
|
} catch (err) {
|
||||||
} catch (ex) {
|
this.#passwordCapability.reject(err);
|
||||||
this.#passwordCapability.reject(ex);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
this.#passwordCapability.reject(
|
|
||||||
new PasswordException(exception.message, exception.code)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return this.#passwordCapability.promise;
|
return this.#passwordCapability.promise;
|
||||||
});
|
});
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
import {
|
import {
|
||||||
AbortException,
|
AbortException,
|
||||||
assert,
|
assert,
|
||||||
|
InvalidPDFException,
|
||||||
MissingPDFException,
|
MissingPDFException,
|
||||||
PasswordException,
|
PasswordException,
|
||||||
UnexpectedResponseException,
|
UnexpectedResponseException,
|
||||||
@ -24,13 +25,11 @@ import {
|
|||||||
} from "./util.js";
|
} from "./util.js";
|
||||||
|
|
||||||
const CallbackKind = {
|
const CallbackKind = {
|
||||||
UNKNOWN: 0,
|
|
||||||
DATA: 1,
|
DATA: 1,
|
||||||
ERROR: 2,
|
ERROR: 2,
|
||||||
};
|
};
|
||||||
|
|
||||||
const StreamKind = {
|
const StreamKind = {
|
||||||
UNKNOWN: 0,
|
|
||||||
CANCEL: 1,
|
CANCEL: 1,
|
||||||
CANCEL_COMPLETE: 2,
|
CANCEL_COMPLETE: 2,
|
||||||
CLOSE: 3,
|
CLOSE: 3,
|
||||||
@ -43,31 +42,39 @@ const StreamKind = {
|
|||||||
|
|
||||||
function onFn() {}
|
function onFn() {}
|
||||||
|
|
||||||
function wrapReason(reason) {
|
function wrapReason(ex) {
|
||||||
if (
|
if (
|
||||||
!(
|
ex instanceof AbortException ||
|
||||||
reason instanceof Error ||
|
ex instanceof InvalidPDFException ||
|
||||||
(typeof reason === "object" && reason !== null)
|
ex instanceof MissingPDFException ||
|
||||||
)
|
ex instanceof PasswordException ||
|
||||||
|
ex instanceof UnexpectedResponseException ||
|
||||||
|
ex instanceof UnknownErrorException
|
||||||
) {
|
) {
|
||||||
|
// Avoid re-creating the exception when its type is already correct.
|
||||||
|
return ex;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(ex instanceof Error || (typeof ex === "object" && ex !== null))) {
|
||||||
unreachable(
|
unreachable(
|
||||||
'wrapReason: Expected "reason" to be a (possibly cloned) Error.'
|
'wrapReason: Expected "reason" to be a (possibly cloned) Error.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
switch (reason.name) {
|
switch (ex.name) {
|
||||||
case "AbortException":
|
case "AbortException":
|
||||||
return new AbortException(reason.message);
|
return new AbortException(ex.message);
|
||||||
|
case "InvalidPDFException":
|
||||||
|
return new InvalidPDFException(ex.message);
|
||||||
case "MissingPDFException":
|
case "MissingPDFException":
|
||||||
return new MissingPDFException(reason.message);
|
return new MissingPDFException(ex.message);
|
||||||
case "PasswordException":
|
case "PasswordException":
|
||||||
return new PasswordException(reason.message, reason.code);
|
return new PasswordException(ex.message, ex.code);
|
||||||
case "UnexpectedResponseException":
|
case "UnexpectedResponseException":
|
||||||
return new UnexpectedResponseException(reason.message, reason.status);
|
return new UnexpectedResponseException(ex.message, ex.status);
|
||||||
case "UnknownErrorException":
|
case "UnknownErrorException":
|
||||||
return new UnknownErrorException(reason.message, reason.details);
|
return new UnknownErrorException(ex.message, ex.details);
|
||||||
default:
|
|
||||||
return new UnknownErrorException(reason.message, reason.toString());
|
|
||||||
}
|
}
|
||||||
|
return new UnknownErrorException(ex.message, ex.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
class MessageHandler {
|
class MessageHandler {
|
||||||
@ -532,4 +539,4 @@ class MessageHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { MessageHandler };
|
export { MessageHandler, wrapReason };
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user