AbstractMethodError.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 | undefined} */
  20. this.stack = undefined;
  21. Error.captureStackTrace(this);
  22. /** @type {RegExpMatchArray | null} */
  23. const match =
  24. /** @type {string} */
  25. (/** @type {unknown} */ (this.stack))
  26. .split("\n")[3]
  27. .match(CURRENT_METHOD_REGEXP);
  28. this.message = match && match[1] ? createMessage(match[1]) : createMessage();
  29. }
  30. /**
  31. * Error for abstract method
  32. * @example
  33. * ```js
  34. * class FooClass {
  35. * abstractMethod() {
  36. * throw new AbstractMethodError(); // error message: Abstract method FooClass.abstractMethod. Must be overridden.
  37. * }
  38. * }
  39. * ```
  40. */
  41. class AbstractMethodError extends WebpackError {
  42. constructor() {
  43. super(new Message().message);
  44. this.name = "AbstractMethodError";
  45. }
  46. }
  47. module.exports = AbstractMethodError;