AbstractMethodError.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Ivan Kopeykin @vankop
  4. */
  5. "use strict";
  6. const WebpackError = require("./WebpackError");
  7. const CURRENT_METHOD_REGEXP = /at ([a-zA-Z0-9_.]*)/;
  8. /**
  9. * @param {string=} method method name
  10. * @returns {string} message
  11. */
  12. function createMessage(method) {
  13. return `Abstract method${method ? " " + method : ""}. Must be overridden.`;
  14. }
  15. /**
  16. * @constructor
  17. */
  18. function Message() {
  19. /** @type {string} */
  20. this.stack = undefined;
  21. Error.captureStackTrace(this);
  22. /** @type {RegExpMatchArray} */
  23. const match = this.stack.split("\n")[3].match(CURRENT_METHOD_REGEXP);
  24. this.message = match && match[1] ? createMessage(match[1]) : createMessage();
  25. }
  26. /**
  27. * Error for abstract method
  28. * @example
  29. * class FooClass {
  30. * abstractMethod() {
  31. * throw new AbstractMethodError(); // error message: Abstract method FooClass.abstractMethod. Must be overridden.
  32. * }
  33. * }
  34. *
  35. */
  36. class AbstractMethodError extends WebpackError {
  37. constructor() {
  38. super(new Message().message);
  39. this.name = "AbstractMethodError";
  40. }
  41. }
  42. module.exports = AbstractMethodError;