Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to throw MissingPDFException when applicable in node_stream.js (issue 9791) #9964

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions src/display/node_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let https = __non_webpack_require__('https');
let url = __non_webpack_require__('url');

import {
AbortException, assert, createPromiseCapability
AbortException, assert, createPromiseCapability, MissingPDFException
} from '../shared/util';
import {
extractFilenameFromHeader, validateRangeRequestCapabilities
Expand Down Expand Up @@ -300,6 +300,12 @@ class PDFNodeStreamFullReader extends BaseFullReader {
super(stream);

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._setReadableStream(response);

Expand Down Expand Up @@ -359,17 +365,24 @@ class PDFNodeStreamRangeReader extends BaseRangeReader {
}
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;
Copy link
Contributor

@timvandermeij timvandermeij Aug 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might need return after this too to avoid setting the readable stream to a failed response, just like above.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that seems reasonable; fixed now.

return;
}
this._setReadableStream(response);
};

this._request = null;
if (this._url.protocol === 'http:') {
this._request = http.request(createRequestOptions(
this._url, this._httpHeaders), (response) => {
this._setReadableStream(response);
});
this._request = http.request(
createRequestOptions(this._url, this._httpHeaders),
handleResponse);
} else {
this._request = https.request(createRequestOptions(
this._url, this._httpHeaders), (response) => {
this._setReadableStream(response);
});
this._request = https.request(
createRequestOptions(this._url, this._httpHeaders),
handleResponse);
}

this._request.on('error', (reason) => {
Expand All @@ -392,6 +405,9 @@ class PDFNodeStreamFsFullReader extends BaseFullReader {

fs.lstat(path, (error, stat) => {
if (error) {
if (error.code === 'ENOENT') {
error = new MissingPDFException(`Missing PDF "${path}".`);
}
this._storedError = error;
this._headersCapability.reject(error);
return;
Expand Down
5 changes: 0 additions & 5 deletions test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,6 @@ describe('api', function() {
});
});
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(
buildGetDocumentParams('non-existent.pdf'));
loadingTask.promise.then(function(error) {
Expand Down