You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.6 KiB

4 years ago
  1. # node-errno
  2. > Better [libuv](https://github.com/libuv/libuv)/[Node.js](https://nodejs.org)/[io.js](https://iojs.org) error handling & reporting. Available in npm as *errno*.
  3. [![npm](https://img.shields.io/npm/v/errno.svg)](https://www.npmjs.com/package/errno)
  4. [![Build Status](https://secure.travis-ci.org/rvagg/node-errno.png)](http://travis-ci.org/rvagg/node-errno)
  5. [![npm](https://img.shields.io/npm/dm/errno.svg)](https://www.npmjs.com/package/errno)
  6. * [errno exposed](#errnoexposed)
  7. * [Custom errors](#customerrors)
  8. <a name="errnoexposed"></a>
  9. ## errno exposed
  10. Ever find yourself needing more details about Node.js errors? Me too, so *node-errno* contains the errno mappings direct from libuv so you can use them in your code.
  11. **By errno:**
  12. ```js
  13. require('errno').errno[3]
  14. // → {
  15. // "errno": 3,
  16. // "code": "EACCES",
  17. // "description": "permission denied"
  18. // }
  19. ```
  20. **By code:**
  21. ```js
  22. require('errno').code.ENOTEMPTY
  23. // → {
  24. // "errno": 53,
  25. // "code": "ENOTEMPTY",
  26. // "description": "directory not empty"
  27. // }
  28. ```
  29. **Make your errors more descriptive:**
  30. ```js
  31. var errno = require('errno')
  32. function errmsg(err) {
  33. var str = 'Error: '
  34. // if it's a libuv error then get the description from errno
  35. if (errno.errno[err.errno])
  36. str += errno.errno[err.errno].description
  37. else
  38. str += err.message
  39. // if it's a `fs` error then it'll have a 'path' property
  40. if (err.path)
  41. str += ' [' + err.path + ']'
  42. return str
  43. }
  44. var fs = require('fs')
  45. fs.readFile('thisisnotarealfile.txt', function (err, data) {
  46. if (err)
  47. console.log(errmsg(err))
  48. })
  49. ```
  50. **Use as a command line tool:**
  51. ```
  52. ~ $ errno 53
  53. {
  54. "errno": 53,
  55. "code": "ENOTEMPTY",
  56. "description": "directory not empty"
  57. }
  58. ~ $ errno EROFS
  59. {
  60. "errno": 56,
  61. "code": "EROFS",
  62. "description": "read-only file system"
  63. }
  64. ~ $ errno foo
  65. No such errno/code: "foo"
  66. ```
  67. Supply no arguments for the full list. Error codes are processed case-insensitive.
  68. You will need to install with `npm install errno -g` if you want the `errno` command to be available without supplying a full path to the node_modules installation.
  69. <a name="customerrors"></a>
  70. ## Custom errors
  71. Use `errno.custom.createError()` to create custom `Error` objects to throw around in your Node.js library. Create error hierarchies so `instanceof` becomes a useful tool in tracking errors. Call-stack is correctly captured at the time you create an instance of the error object, plus a `cause` property will make available the original error object if you pass one in to the constructor.
  72. ```js
  73. var create = require('errno').custom.createError
  74. var MyError = create('MyError') // inherits from Error
  75. var SpecificError = create('SpecificError', MyError) // inherits from MyError
  76. var OtherError = create('OtherError', MyError)
  77. // use them!
  78. if (condition) throw new SpecificError('Eeek! Something bad happened')
  79. if (err) return callback(new OtherError(err))
  80. ```
  81. Also available is a `errno.custom.FilesystemError` with in-built access to errno properties:
  82. ```js
  83. fs.readFile('foo', function (err, data) {
  84. if (err) return callback(new errno.custom.FilesystemError(err))
  85. // do something else
  86. })
  87. ```
  88. The resulting error object passed through the callback will have the following properties: `code`, `errno`, `path` and `message` will contain a descriptive human-readable message.
  89. ## Contributors
  90. * [bahamas10](https://github.com/bahamas10) (Dave Eddy) - Added CLI
  91. * [ralphtheninja](https://github.com/ralphtheninja) (Lars-Magnus Skog)
  92. ## Copyright & Licence
  93. *Copyright (c) 2012-2015 [Rod Vagg](https://github.com/rvagg) ([@rvagg](https://twitter.com/rvagg))*
  94. Made available under the MIT licence:
  95. Permission is hereby granted, free of charge, to any person obtaining a copy
  96. of this software and associated documentation files (the "Software"), to deal
  97. in the Software without restriction, including without limitation the rights
  98. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  99. copies of the Software, and to permit persons to whom the Software is furnished
  100. to do so, subject to the following conditions:
  101. The above copyright notice and this permission notice shall be included in all
  102. copies or substantial portions of the Software.
  103. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  104. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  105. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  106. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  107. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  108. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  109. SOFTWARE.