Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#9391 resolve discussions, AbstractMethodError
- Loading branch information
Showing
5 changed files
with
85 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"use strict"; | ||
|
||
const WebpackError = require("./WebpackError"); | ||
const CURRENT_METHOD_REGEXP = /at ([a-zA-Z0-9_.]*)/; | ||
|
||
/** | ||
* @param {string=} method method name | ||
* @returns {string} message | ||
*/ | ||
function createMessage(method) { | ||
return `Abstract method${method ? " " + method : ""}. Must be overriden.`; | ||
} | ||
|
||
/** | ||
* Error for abstract method | ||
* @example | ||
* class FooClass { | ||
* abstractMethod() { | ||
* throw new AbstractMethodError(); // error message: Abstract method FooClass.abstractMethod. Must be overriden. | ||
* } | ||
* } | ||
* | ||
*/ | ||
class AbstractMethodError extends WebpackError { | ||
constructor() { | ||
super(createMessage()); | ||
this.name = "AbstractMethodError"; | ||
/** @type {RegExpMatchArray} */ | ||
const match = this.stack.split("\n")[1].match(CURRENT_METHOD_REGEXP); | ||
if (match && match[1]) { | ||
this.message = createMessage(match[1]); | ||
} | ||
} | ||
} | ||
|
||
module.exports = AbstractMethodError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"use strict"; | ||
|
||
const AbstractMethodError = require("../lib/AbstractMethodError"); | ||
|
||
describe("WebpackError", () => { | ||
class Foo { | ||
abstractMethod() { | ||
return new AbstractMethodError(); | ||
} | ||
} | ||
|
||
class Child extends Foo {} | ||
|
||
const expectedMessage = "Abstract method $1. Must be overriden."; | ||
|
||
it("Should construct message with caller info", () => { | ||
const fooClassError = new Foo().abstractMethod(); | ||
const childClassError = new Child().abstractMethod(); | ||
|
||
expect(fooClassError.message).toBe( | ||
expectedMessage.replace("$1", "Foo.abstractMethod") | ||
); | ||
expect(childClassError.message).toBe( | ||
expectedMessage.replace("$1", "Child.abstractMethod") | ||
); | ||
}); | ||
}); |