My object context is not where I expect it to be.
It should be of the class type as thrown, but when caught is object. Why is this so? Probably something stupid. It nearly always is.
I have to use an old unknown JS runtime. It can't be upgraded so I can't use newer features, etc.
I ported the code to run in Node so it's slightly different in terms of imports and exports, but the prototypes and functions are the same. The behavior is also the same: unwanted.
This ClassA provides the test case.
import ClassB from "./ClassB.js"
import ExceptionC from "./ClassB.js"
export default function ClassA() {};
ClassA.prototype = {
objB: new ClassB(),
funcA: function() {
try {
this.objB.thrower();
}
catch (thrownFromB) {
console.log("Caught Exception. This exception instance should be an ExceptionC. It is type:" + typeof thrownFromB);
if (thrownFromB instanceof ExceptionC) {
console.log("ExceptionC message: " + thrownFromB.message + " origError: " + thrownFromB.origError);
}
}
}
};
This ClassB provides the throw and the custom exception class also.
export default function ClassB() {};
ClassB.prototype = {
AugmentedException: function(errorMsg, origError) {
Error.call(this);
this.message = errorMsg;
this.origError = origError;
},
ExceptionC: function(errorMsg, origError) {
AugmentedException.call(this, errorMsg, origError);
},
thrower: function() {
console.log("throwing a new ExceptionC");
throw new ExceptionC("foo", "bar");
}
};
This is the main function.
import ClassA from "./ClassA.js"
var foo = new ClassA();
foo.funcA();
This is the output.
>> OUTPUT:
>> throwing a new ExceptionC...
>> Caught Exception. This exception instance should be an ExceptionC. It is type: object