Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Node] Node attaches errno information to some exceptions.
This is valuable information for Node applications that need to figure out why a particular system call failed. Note that errno can either be a string or a number, which is why I have typed it as 'any'. While this is undocumented, it is tested: Code property: https://github.com/joyent/node/blob/v0.10.23-release/test/simple/test-fs-open.js#L34 Errno property [string]: https://github.com/joyent/node/blob/v0.10.23-release/test/internet/test-dns.js#L148 Errno property [number]: https://github.com/joyent/node/blob/v0.10.23-release/test/simple/test-domain-implicit-fs.js#L43 Syscall property: https://github.com/joyent/node/blob/v0.10.23-release/test/simple/test-stdout-close-catch.js#L38 Path property (I can’t find a test, but many examples have this line): https://github.com/joyent/node/blob/v0.10.23-release/deps/npm/node_modules/fstream-npm/example/bundle.js#L6 ..and well supported in the code for all platforms (this is where I got the name 'ErrnoException' from): *nix: https://github.com/joyent/node/blob/v0.10.23-release/src/node.cc#L719 Windows: https://github.com/joyent/node/blob/v0.10.23-release/src/node.cc#L856 Unfortunately, while every errno exception is guaranteed to have the ‘errno’ and ‘code’ properties (see the logic in the source code in node.cc), there is no hard guarantee that particular library functions will always pass an ErrnoException to the callback. I cannot find a counterexample at this moment, though. Since we can’t have union types in TypeScript, I simply made all of the properties on ErrnoException optional as a compromise. I am only adding modifications to the fs module, which definitely throws ErrnoExceptions. But according to the examples above, other modules (e.g. dns) also pass errno information back to applications. People familiar with those other modules can update their typings to reference this interface when applicable; I lack the experience with those modules to update their typings. Further proof: ```javascript $ node > var fs = require('fs'); undefined > fs.open('doesntexistlol', 'r', function(e) { console.log(Object.keys(e)); console.log(e.code); console.log(e.errno); console.log(e.path); }); undefined > [ 'errno', 'code', 'path' ] ENOENT 34 doesntexistlol ```
- Loading branch information
John Vilk
committed
Dec 17, 2013
1 parent
718eff7
commit cddd0b7
Showing
1 changed file
with
52 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters