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.

README.md 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # node-error-ex [![Travis-CI.org Build Status](https://img.shields.io/travis/Qix-/node-error-ex.svg?style=flat-square)](https://travis-ci.org/Qix-/node-error-ex) [![Coveralls.io Coverage Rating](https://img.shields.io/coveralls/Qix-/node-error-ex.svg?style=flat-square)](https://coveralls.io/r/Qix-/node-error-ex)
  2. > Easily subclass and customize new Error types
  3. ## Examples
  4. To include in your project:
  5. ```javascript
  6. var errorEx = require('error-ex');
  7. ```
  8. To create an error message type with a specific name (note, that `ErrorFn.name`
  9. will not reflect this):
  10. ```javascript
  11. var JSONError = errorEx('JSONError');
  12. var err = new JSONError('error');
  13. err.name; //-> JSONError
  14. throw err; //-> JSONError: error
  15. ```
  16. To add a stack line:
  17. ```javascript
  18. var JSONError = errorEx('JSONError', {fileName: errorEx.line('in %s')});
  19. var err = new JSONError('error')
  20. err.fileName = '/a/b/c/foo.json';
  21. throw err; //-> (line 2)-> in /a/b/c/foo.json
  22. ```
  23. To append to the error message:
  24. ```javascript
  25. var JSONError = errorEx('JSONError', {fileName: errorEx.append('in %s')});
  26. var err = new JSONError('error');
  27. err.fileName = '/a/b/c/foo.json';
  28. throw err; //-> JSONError: error in /a/b/c/foo.json
  29. ```
  30. ## API
  31. #### `errorEx([name], [properties])`
  32. Creates a new ErrorEx error type
  33. - `name`: the name of the new type (appears in the error message upon throw;
  34. defaults to `Error.name`)
  35. - `properties`: if supplied, used as a key/value dictionary of properties to
  36. use when building up the stack message. Keys are property names that are
  37. looked up on the error message, and then passed to function values.
  38. - `line`: if specified and is a function, return value is added as a stack
  39. entry (error-ex will indent for you). Passed the property value given
  40. the key.
  41. - `stack`: if specified and is a function, passed the value of the property
  42. using the key, and the raw stack lines as a second argument. Takes no
  43. return value (but the stack can be modified directly).
  44. - `message`: if specified and is a function, return value is used as new
  45. `.message` value upon get. Passed the property value of the property named
  46. by key, and the existing message is passed as the second argument as an
  47. array of lines (suitable for multi-line messages).
  48. Returns a constructor (Function) that can be used just like the regular Error
  49. constructor.
  50. ```javascript
  51. var errorEx = require('error-ex');
  52. var BasicError = errorEx();
  53. var NamedError = errorEx('NamedError');
  54. // --
  55. var AdvancedError = errorEx('AdvancedError', {
  56. foo: {
  57. line: function (value, stack) {
  58. if (value) {
  59. return 'bar ' + value;
  60. }
  61. return null;
  62. }
  63. }
  64. }
  65. var err = new AdvancedError('hello, world');
  66. err.foo = 'baz';
  67. throw err;
  68. /*
  69. AdvancedError: hello, world
  70. bar baz
  71. at tryReadme() (readme.js:20:1)
  72. */
  73. ```
  74. #### `errorEx.line(str)`
  75. Creates a stack line using a delimiter
  76. > This is a helper function. It is to be used in lieu of writing a value object
  77. > for `properties` values.
  78. - `str`: The string to create
  79. - Use the delimiter `%s` to specify where in the string the value should go
  80. ```javascript
  81. var errorEx = require('error-ex');
  82. var FileError = errorEx('FileError', {fileName: errorEx.line('in %s')});
  83. var err = new FileError('problem reading file');
  84. err.fileName = '/a/b/c/d/foo.js';
  85. throw err;
  86. /*
  87. FileError: problem reading file
  88. in /a/b/c/d/foo.js
  89. at tryReadme() (readme.js:7:1)
  90. */
  91. ```
  92. #### `errorEx.append(str)`
  93. Appends to the `error.message` string
  94. > This is a helper function. It is to be used in lieu of writing a value object
  95. > for `properties` values.
  96. - `str`: The string to append
  97. - Use the delimiter `%s` to specify where in the string the value should go
  98. ```javascript
  99. var errorEx = require('error-ex');
  100. var SyntaxError = errorEx('SyntaxError', {fileName: errorEx.append('in %s')});
  101. var err = new SyntaxError('improper indentation');
  102. err.fileName = '/a/b/c/d/foo.js';
  103. throw err;
  104. /*
  105. SyntaxError: improper indentation in /a/b/c/d/foo.js
  106. at tryReadme() (readme.js:7:1)
  107. */
  108. ```
  109. ## License
  110. Licensed under the [MIT License](http://opensource.org/licenses/MIT).
  111. You can find a copy of it in [LICENSE](LICENSE).