From eff8ede33ec8d0e338ca50d6fa8dad45ad54f481 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 1 Dec 2024 11:36:18 +0100 Subject: [PATCH] Make the `onError` callback required in `NetworkManager` This helps ensure that loading errors are always handled correctly, and note that both `PDFNetworkStreamFullRequestReader` and `PDFNetworkStreamRangeRequestReader` already provided such a callback. --- src/display/network.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/display/network.js b/src/display/network.js index 8f08e27c5f51c..930c2d39639a5 100644 --- a/src/display/network.js +++ b/src/display/network.js @@ -85,11 +85,10 @@ class NetworkManager { } xhr.responseType = "arraybuffer"; - if (args.onError) { - xhr.onerror = function (evt) { - args.onError(xhr.status); - }; - } + assert(args.onError, "Expected `onError` callback to be provided."); + xhr.onerror = () => { + args.onError(xhr.status); + }; xhr.onreadystatechange = this.onStateChange.bind(this, xhrId); xhr.onprogress = this.onProgress.bind(this, xhrId); @@ -137,7 +136,7 @@ class NetworkManager { // Success status == 0 can be on ftp, file and other protocols. if (xhr.status === 0 && this.isHttp) { - pendingRequest.onError?.(xhr.status); + pendingRequest.onError(xhr.status); return; } const xhrStatus = xhr.status || OK_RESPONSE; @@ -153,7 +152,7 @@ class NetworkManager { !ok_response_on_range_request && xhrStatus !== pendingRequest.expectedStatus ) { - pendingRequest.onError?.(xhr.status); + pendingRequest.onError(xhr.status); return; } @@ -168,7 +167,7 @@ class NetworkManager { }); } else { warn(`Missing or invalid "Content-Range" header.`); - pendingRequest.onError?.(0); + pendingRequest.onError(0); } } else if (chunk) { pendingRequest.onDone({ @@ -176,7 +175,7 @@ class NetworkManager { chunk, }); } else { - pendingRequest.onError?.(xhr.status); + pendingRequest.onError(xhr.status); } }