Errors are not reported if something else goes wrong
See original GitHub issueIssue Description
It took me a long while to figure out why my program was not reporting JSON Schema errors! Here’s a much-simplified version of what was happening:
ringo:autogen$ cat t.js
var $RefParser = require('json-schema-ref-parser');
$RefParser.dereference('some nonexistent file', function(err, schema) {
if (err) {
console.error(err);
} else {
console.log(schema);
}
});
console.log('All done');
process.exit(0);
ringo:autogen$ node t.js
All done
ringo:autogen$
If I comment out that last line (process.exit(0)
), then the error from the missing JSON Schema is correctly reported: Error opening file "/Users/mike/git/folio/other/mod-graphql/src/autogen/some nonexistent file" ENOENT: no such file or directory
.
I think what’s going on here is that, since the dereference
callback function is run asynchronously, it doesn’t reach the point of reporting the ENOENT error before the program exits. But it does report the errors if the exit is explicit (i.e. fall off the end of the program) rather than explicit.
I think that errors should always be reported when they occur, irrespective of how the program terminates. Or, if that’s not possible, there should be a way to await the completion of the dereference
callback function before continuing with the rest of the program.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Got it. I need the remainder of my program not to follow the
dereference
invocation, but to be in the callback’s no-error branch.Thanks for helping me think more clearly about this. I agree that the issue should be closed.
That’s just normal Node.js async behavior. The
process.exit()
method is synchronous and exits the program immediately without allowing asynchronous operations any time to complete.