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 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # make-error
  2. [![Package Version](https://badgen.net/npm/v/make-error)](https://npmjs.org/package/make-error) [![Build Status](https://travis-ci.org/JsCommunity/make-error.png?branch=master)](https://travis-ci.org/JsCommunity/make-error) [![PackagePhobia](https://badgen.net/packagephobia/install/make-error)](https://packagephobia.now.sh/result?p=make-error) [![Latest Commit](https://badgen.net/github/last-commit/JsCommunity/make-error)](https://github.com/JsCommunity/make-error/commits/master)
  3. > Make your own error types!
  4. ## Features
  5. - Compatible Node & browsers
  6. - `instanceof` support
  7. - `error.name` & `error.stack` support
  8. - compatible with [CSP](https://en.wikipedia.org/wiki/Content_Security_Policy) (i.e. no `eval()`)
  9. ## Installation
  10. ### Node & [Browserify](http://browserify.org/)/[Webpack](https://webpack.js.org/)
  11. Installation of the [npm package](https://npmjs.org/package/make-error):
  12. ```
  13. > npm install --save make-error
  14. ```
  15. Then require the package:
  16. ```javascript
  17. var makeError = require("make-error");
  18. ```
  19. ### Browser
  20. You can directly use the build provided at [unpkg.com](https://unpkg.com):
  21. ```html
  22. <script src="https://unpkg.com/make-error@1/dist/make-error.js"></script>
  23. ```
  24. ## Usage
  25. ### Basic named error
  26. ```javascript
  27. var CustomError = makeError("CustomError");
  28. // Parameters are forwarded to the super class (here Error).
  29. throw new CustomError("a message");
  30. ```
  31. ### Advanced error class
  32. ```javascript
  33. function CustomError(customValue) {
  34. CustomError.super.call(this, "custom error message");
  35. this.customValue = customValue;
  36. }
  37. makeError(CustomError);
  38. // Feel free to extend the prototype.
  39. CustomError.prototype.myMethod = function CustomError$myMethod() {
  40. console.log("CustomError.myMethod (%s, %s)", this.code, this.message);
  41. };
  42. //-----
  43. try {
  44. throw new CustomError(42);
  45. } catch (error) {
  46. error.myMethod();
  47. }
  48. ```
  49. ### Specialized error
  50. ```javascript
  51. var SpecializedError = makeError("SpecializedError", CustomError);
  52. throw new SpecializedError(42);
  53. ```
  54. ### Inheritance
  55. > Best for ES2015+.
  56. ```javascript
  57. import { BaseError } from "make-error";
  58. class CustomError extends BaseError {
  59. constructor() {
  60. super("custom error message");
  61. }
  62. }
  63. ```
  64. ## Related
  65. - [make-error-cause](https://www.npmjs.com/package/make-error-cause): Make your own error types, with a cause!
  66. ## Contributions
  67. Contributions are _very_ welcomed, either on the documentation or on
  68. the code.
  69. You may:
  70. - report any [issue](https://github.com/JsCommunity/make-error/issues)
  71. you've encountered;
  72. - fork and create a pull request.
  73. ## License
  74. ISC © [Julien Fontanet](http://julien.isonoe.net)