Merge pull request #9964 from Snuffleupagus/node-MissingPDFException

Attempt to throw `MissingPDFException` when applicable in `node_stream.js` (issue 9791)
This commit is contained in:
Tim van der Meij 2018-08-06 23:42:24 +02:00 committed by GitHub
commit 13b98746d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 14 deletions

View File

@ -20,7 +20,7 @@ let https = __non_webpack_require__('https');
let url = __non_webpack_require__('url'); let url = __non_webpack_require__('url');
import { import {
AbortException, assert, createPromiseCapability AbortException, assert, createPromiseCapability, MissingPDFException
} from '../shared/util'; } from '../shared/util';
import { import {
extractFilenameFromHeader, validateRangeRequestCapabilities extractFilenameFromHeader, validateRangeRequestCapabilities
@ -300,6 +300,12 @@ class PDFNodeStreamFullReader extends BaseFullReader {
super(stream); super(stream);
let handleResponse = (response) => { let handleResponse = (response) => {
if (response.statusCode === 404) {
const error = new MissingPDFException(`Missing PDF "${this._url}".`);
this._storedError = error;
this._headersCapability.reject(error);
return;
}
this._headersCapability.resolve(); this._headersCapability.resolve();
this._setReadableStream(response); this._setReadableStream(response);
@ -359,17 +365,24 @@ class PDFNodeStreamRangeReader extends BaseRangeReader {
} }
this._httpHeaders['Range'] = `bytes=${start}-${end - 1}`; this._httpHeaders['Range'] = `bytes=${start}-${end - 1}`;
let handleResponse = (response) => {
if (response.statusCode === 404) {
const error = new MissingPDFException(`Missing PDF "${this._url}".`);
this._storedError = error;
return;
}
this._setReadableStream(response);
};
this._request = null; this._request = null;
if (this._url.protocol === 'http:') { if (this._url.protocol === 'http:') {
this._request = http.request(createRequestOptions( this._request = http.request(
this._url, this._httpHeaders), (response) => { createRequestOptions(this._url, this._httpHeaders),
this._setReadableStream(response); handleResponse);
});
} else { } else {
this._request = https.request(createRequestOptions( this._request = https.request(
this._url, this._httpHeaders), (response) => { createRequestOptions(this._url, this._httpHeaders),
this._setReadableStream(response); handleResponse);
});
} }
this._request.on('error', (reason) => { this._request.on('error', (reason) => {
@ -392,6 +405,9 @@ class PDFNodeStreamFsFullReader extends BaseFullReader {
fs.lstat(path, (error, stat) => { fs.lstat(path, (error, stat) => {
if (error) { if (error) {
if (error.code === 'ENOENT') {
error = new MissingPDFException(`Missing PDF "${path}".`);
}
this._storedError = error; this._storedError = error;
this._headersCapability.reject(error); this._headersCapability.reject(error);
return; return;

View File

@ -164,11 +164,6 @@ describe('api', function() {
}); });
}); });
it('creates pdf doc from non-existent URL', function(done) { it('creates pdf doc from non-existent URL', function(done) {
if (isNodeJS()) {
pending('Fix `src/display/node_stream.js` to actually throw ' +
'a `MissingPDFException` in all cases where a PDF file ' +
'cannot be found, such that this test-case can be enabled.');
}
var loadingTask = getDocument( var loadingTask = getDocument(
buildGetDocumentParams('non-existent.pdf')); buildGetDocumentParams('non-existent.pdf'));
loadingTask.promise.then(function(error) { loadingTask.promise.then(function(error) {