Skip to content

Commit a0f4500

Browse files
Micah Bulemarcbachmann
authored andcommitted
A better way for handling PhantomJS exits
1 parent 9e14ef5 commit a0f4500

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

lib/pdf.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,27 @@ PDF.prototype.exec = function PdfExec (callback) {
117117
// If we don't have an exit code, we kill the process, ignore stderr after this point
118118
if (code === null) kill(child, onData, onError)
119119

120-
if (!data) {
121-
if (!err && code) err = new Error("html-pdf: Received the exit code '" + code + "'")
122-
else if (!err) err = new Error('html-pdf: Unknown Error')
123-
120+
// Since code has a truthy/falsy value of either 0 or 1, check for existence first.
121+
// Ignore if code has a value of 0 since that means PhantomJS has executed and exited successfully.
122+
// Also, as per your script and standards, having a code value of 1 means one can always assume that
123+
// an error occured.
124+
if (typeof code !== 'undefined' && code !== 0) {
125+
126+
var error = null;
127+
128+
if (err) {
129+
// Rudimentary checking if err is an instance of the Error class
130+
error = err instanceof Error ? err : new Error(err)
131+
} else {
132+
// This is to catch the edge case of having a exit code value of 1 but having no error
133+
error = new Error('html-pdf: Unknown Error')
134+
}
135+
136+
// Append anything caught from the stderr
124137
var postfix = stderr.length ? '\n' + Buffer.concat(stderr).toString() : ''
125138
if (postfix) err.message += postfix
126-
return callback(err, null)
139+
140+
return callback(error);
127141
}
128142

129143
callback(null, data)

0 commit comments

Comments
 (0)