Improve structure extension errors

This commit is contained in:
Schuyler Cebulskie
2019-03-24 16:45:34 -04:00
parent b74a4356dd
commit 04fa56d0c0

View File

@@ -45,17 +45,22 @@ class Structures {
if (typeof extender !== 'function') {
const received = `(received ${typeof extender})`;
throw new TypeError(
`"extender" argument must be a function that returns the extended structure class/prototype ${received}`
`"extender" argument must be a function that returns the extended structure class/prototype ${received}.`
);
}
const extended = extender(structures[structure]);
if (typeof extended !== 'function') {
throw new TypeError('The extender function must return the extended structure class/prototype.');
const received = `(received ${typeof extended})`;
throw new TypeError(`The extender function must return the extended structure class/prototype ${received}.`);
}
if (Object.getPrototypeOf(extended) !== structures[structure]) {
const prototype = Object.getPrototypeOf(extended);
if (prototype !== structures[structure]) {
const received = `${extended.name || 'unnamed'}${prototype.name ? ` extends ${prototype.name}` : ''}`;
throw new Error(
'The class/prototype returned from the extender function must extend the existing structure class/prototype.'
'The class/prototype returned from the extender function must extend the existing structure class/prototype' +
` (received function ${received}; expected extension of ${structures[structure].name}).`
);
}