Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
In ghostscript4js, whenever the gs command returns a nonzero exit code it triggers a fatal unrecoverable error that can't be handled in JavaScript. In particular, the GhostscriptManager class tries to throw a string exception and the GhostscriptWorker (subclass of Napi::AsyncWorker) tries to catch it to set the JavaScript error with SetError to e.what(). While I'm not deeply familiar with Napi, I've found that if we throw a std::runtime_error exception instead and change SetError to set the string of the exception rather than use Napi::String::New, we can properly handle the error in JavaScript.
Changes
Incidental change
Testing
I added a test to cover this case. The test works by taking an invalid pdf (test/invalid.pdf) which was created by taking an ordinary text file with some Lorem ipsum text and renaming it to have a .pdf extension. When executing a gs command on that PDF file, note that the test suite crashes without finishing the tests, even though the gs.execute had a catch block.
After applying the change to ghostscript4js.cc and rebuilding with
node-gyp rebuild
, I reran npm test and saw that the tests now complete successfully. Note that when ghostscript crashes it still prints out the fatal error stack which I didn't address in this PR but I think it's still better than throwing an unrecoverable error.