| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Ivan Kopeykin @vankop
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const WebpackError = require("./WebpackError");
|
|---|
| 9 |
|
|---|
| 10 | const CURRENT_METHOD_REGEXP = /at ([a-zA-Z0-9_.]*)/;
|
|---|
| 11 |
|
|---|
| 12 | /**
|
|---|
| 13 | * Creates the error message shown when an abstract API is called without
|
|---|
| 14 | * being implemented by a subclass.
|
|---|
| 15 | * @param {string=} method method name
|
|---|
| 16 | * @returns {string} message
|
|---|
| 17 | */
|
|---|
| 18 | function createMessage(method) {
|
|---|
| 19 | return `Abstract method${method ? ` ${method}` : ""}. Must be overridden.`;
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * Captures a stack trace so the calling method name can be folded into the
|
|---|
| 24 | * final abstract-method error message.
|
|---|
| 25 | * @constructor
|
|---|
| 26 | */
|
|---|
| 27 | function Message() {
|
|---|
| 28 | /** @type {string | undefined} */
|
|---|
| 29 | this.stack = undefined;
|
|---|
| 30 | Error.captureStackTrace(this);
|
|---|
| 31 | /** @type {RegExpMatchArray | null} */
|
|---|
| 32 | const match =
|
|---|
| 33 | /** @type {string} */
|
|---|
| 34 | (/** @type {unknown} */ (this.stack))
|
|---|
| 35 | .split("\n")[3]
|
|---|
| 36 | .match(CURRENT_METHOD_REGEXP);
|
|---|
| 37 |
|
|---|
| 38 | this.message = match && match[1] ? createMessage(match[1]) : createMessage();
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Error thrown when code reaches a method that is intended to be overridden by
|
|---|
| 43 | * a subclass.
|
|---|
| 44 | * @example
|
|---|
| 45 | * ```js
|
|---|
| 46 | * class FooClass {
|
|---|
| 47 | * abstractMethod() {
|
|---|
| 48 | * throw new AbstractMethodError(); // error message: Abstract method FooClass.abstractMethod. Must be overridden.
|
|---|
| 49 | * }
|
|---|
| 50 | * }
|
|---|
| 51 | * ```
|
|---|
| 52 | */
|
|---|
| 53 | class AbstractMethodError extends WebpackError {
|
|---|
| 54 | /**
|
|---|
| 55 | * Creates an error whose message points at the abstract method that was
|
|---|
| 56 | * invoked.
|
|---|
| 57 | */
|
|---|
| 58 | constructor() {
|
|---|
| 59 | super(new Message().message);
|
|---|
| 60 | /** @type {string} */
|
|---|
| 61 | this.name = "AbstractMethodError";
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | module.exports = AbstractMethodError;
|
|---|