Skip to content

Commit

Permalink
Make the onError callback required in NetworkManager
Browse files Browse the repository at this point in the history
This helps ensure that loading errors are always handled correctly, and note that both `PDFNetworkStreamFullRequestReader` and `PDFNetworkStreamRangeRequestReader` already provided such a callback.
  • Loading branch information
Snuffleupagus committed Dec 6, 2024
1 parent 2661d06 commit eff8ede
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/display/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand All @@ -153,7 +152,7 @@ class NetworkManager {
!ok_response_on_range_request &&
xhrStatus !== pendingRequest.expectedStatus
) {
pendingRequest.onError?.(xhr.status);
pendingRequest.onError(xhr.status);
return;
}

Expand All @@ -168,15 +167,15 @@ class NetworkManager {
});
} else {
warn(`Missing or invalid "Content-Range" header.`);
pendingRequest.onError?.(0);
pendingRequest.onError(0);
}
} else if (chunk) {
pendingRequest.onDone({
begin: 0,
chunk,
});
} else {
pendingRequest.onError?.(xhr.status);
pendingRequest.onError(xhr.status);
}
}

Expand Down

0 comments on commit eff8ede

Please sign in to comment.