[d24f17c] | 1 | # fault
|
---|
| 2 |
|
---|
| 3 | [![Build][build-badge]][build]
|
---|
| 4 | [![Coverage][coverage-badge]][coverage]
|
---|
| 5 | [![Downloads][downloads-badge]][downloads]
|
---|
| 6 | [![Size][size-badge]][size]
|
---|
| 7 |
|
---|
| 8 | Functional errors with formatted output.
|
---|
| 9 |
|
---|
| 10 | ## Install
|
---|
| 11 |
|
---|
| 12 | [npm][]:
|
---|
| 13 |
|
---|
| 14 | ```sh
|
---|
| 15 | npm install fault
|
---|
| 16 | ```
|
---|
| 17 |
|
---|
| 18 | ## Use
|
---|
| 19 |
|
---|
| 20 | ```js
|
---|
| 21 | var fault = require('fault')
|
---|
| 22 |
|
---|
| 23 | throw fault('Hello %s!', 'Eric')
|
---|
| 24 | ```
|
---|
| 25 |
|
---|
| 26 | Yields:
|
---|
| 27 |
|
---|
| 28 | ```text
|
---|
| 29 | Error: Hello Eric!
|
---|
| 30 | at FormattedError (~/node_modules/fault/index.js:30:12)
|
---|
| 31 | at Object.<anonymous> (~/example.js:3:7)
|
---|
| 32 | …
|
---|
| 33 | ```
|
---|
| 34 |
|
---|
| 35 | Or, format a float in a type error:
|
---|
| 36 |
|
---|
| 37 | ```js
|
---|
| 38 | var fault = require('fault')
|
---|
| 39 |
|
---|
| 40 | throw fault.type('Who doesn’t like %f? \uD83C\uDF70', Math.PI)
|
---|
| 41 | ```
|
---|
| 42 |
|
---|
| 43 | Yields:
|
---|
| 44 |
|
---|
| 45 | ```text
|
---|
| 46 | TypeError: Who doesn’t like 3.141593? 🍰
|
---|
| 47 | at Function.FormattedError [as type] (~/node_modules/fault/index.js:30:12)
|
---|
| 48 | at Object.<anonymous> (~/example.js:3:7)
|
---|
| 49 | ```
|
---|
| 50 |
|
---|
| 51 | ## API
|
---|
| 52 |
|
---|
| 53 | ### `fault(format?[, values...])`
|
---|
| 54 |
|
---|
| 55 | Create an error with a printf-like formatted message.
|
---|
| 56 |
|
---|
| 57 | ###### Parameters
|
---|
| 58 |
|
---|
| 59 | * `format` (`string`, optional)
|
---|
| 60 | * `values` (`*`, optional)
|
---|
| 61 |
|
---|
| 62 | ###### Formatters
|
---|
| 63 |
|
---|
| 64 | * `%s` — String
|
---|
| 65 | * `%b` — Binary
|
---|
| 66 | * `%c` — Character
|
---|
| 67 | * `%d` — Decimal
|
---|
| 68 | * `%f` — Floating point
|
---|
| 69 | * `%o` — Octal
|
---|
| 70 | * `%x` — Lowercase hexadecimal
|
---|
| 71 | * `%X` — Uppercase hexadecimal
|
---|
| 72 | * `%` followed by any other character, prints that character
|
---|
| 73 |
|
---|
| 74 | See [`samsonjs/format`][fmt] for argument parsing.
|
---|
| 75 |
|
---|
| 76 | ###### Returns
|
---|
| 77 |
|
---|
| 78 | An instance of [`Error`][error].
|
---|
| 79 |
|
---|
| 80 | ###### Other errors
|
---|
| 81 |
|
---|
| 82 | * `fault.eval(format?[, values...])` — [EvalError][]
|
---|
| 83 | * `fault.range(format?[, values...])` — [RangeError][]
|
---|
| 84 | * `fault.reference(format?[, values...])` — [ReferenceError][]
|
---|
| 85 | * `fault.syntax(format?[, values...])` — [SyntaxError][]
|
---|
| 86 | * `fault.type(format?[, values...])` — [TypeError][]
|
---|
| 87 | * `fault.uri(format?[, values...])` — [URIError][]
|
---|
| 88 |
|
---|
| 89 | #### `fault.create(Constructor)`
|
---|
| 90 |
|
---|
| 91 | Factory to create instances of `ErrorConstructor` with support for formatting.
|
---|
| 92 | Used internally to wrap the global error constructors, exposed for custom
|
---|
| 93 | errors.
|
---|
| 94 | Returns a function just like `fault`.
|
---|
| 95 |
|
---|
| 96 | ## License
|
---|
| 97 |
|
---|
| 98 | [MIT][license] © [Titus Wormer][author]
|
---|
| 99 |
|
---|
| 100 | <!-- Definitions -->
|
---|
| 101 |
|
---|
| 102 | [build-badge]: https://img.shields.io/travis/wooorm/fault.svg
|
---|
| 103 |
|
---|
| 104 | [build]: https://travis-ci.org/wooorm/fault
|
---|
| 105 |
|
---|
| 106 | [coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/fault.svg
|
---|
| 107 |
|
---|
| 108 | [coverage]: https://codecov.io/github/wooorm/fault
|
---|
| 109 |
|
---|
| 110 | [downloads-badge]: https://img.shields.io/npm/dm/fault.svg
|
---|
| 111 |
|
---|
| 112 | [downloads]: https://www.npmjs.com/package/fault
|
---|
| 113 |
|
---|
| 114 | [size-badge]: https://img.shields.io/bundlephobia/minzip/fault.svg
|
---|
| 115 |
|
---|
| 116 | [size]: https://bundlephobia.com/result?p=fault
|
---|
| 117 |
|
---|
| 118 | [npm]: https://docs.npmjs.com/cli/install
|
---|
| 119 |
|
---|
| 120 | [license]: license
|
---|
| 121 |
|
---|
| 122 | [author]: https://wooorm.com
|
---|
| 123 |
|
---|
| 124 | [fmt]: https://github.com/samsonjs/format
|
---|
| 125 |
|
---|
| 126 | [error]: https://developer.mozilla.org/JavaScript/Reference/Global_Objects/Error
|
---|
| 127 |
|
---|
| 128 | [evalerror]: https://developer.mozilla.org/JavaScript/Reference/Global_Objects/EvalError
|
---|
| 129 |
|
---|
| 130 | [rangeerror]: https://developer.mozilla.org/JavaScript/Reference/Global_Objects/RangeError
|
---|
| 131 |
|
---|
| 132 | [referenceerror]: https://developer.mozilla.org/JavaScript/Reference/Global_Objects/ReferenceError
|
---|
| 133 |
|
---|
| 134 | [syntaxerror]: https://developer.mozilla.org/JavaScript/Reference/Global_Objects/SyntaxError
|
---|
| 135 |
|
---|
| 136 | [typeerror]: https://developer.mozilla.org/JavaScript/Reference/Global_Objects/TypeError
|
---|
| 137 |
|
---|
| 138 | [urierror]: https://developer.mozilla.org/JavaScript/Reference/Global_Objects/URIError.
|
---|