.throws with no message creates error with empty message #2525
Open
Description
When calling throwsException
with a String
as the error
argument and no message
argument, it will create errors with an empty message as seen in the code sample below.
function throwsException(fake, error, message) {
if (typeof error === "function") {
fake.exceptionCreator = error;
} else if (typeof error === "string") {
fake.exceptionCreator = function () {
const newException = new Error(message || "");
newException.name = error;
return newException;
};
} else if (!error) {
fake.exceptionCreator = function () {
return new Error("Error");
};
} else {
fake.exception = error;
}
}
While this is not a bug as such, I do think it is poor design to return an Error instance with an empty message
property.
Instead, we could calculate a reasonable error message from the error
argument.
To Reproduce
Steps to reproduce:
const sinon = require("sinon");
const callback = sinon.stub();
callback.withArgs(1).throws("apple pie");
try {
callback(1);
} catch (error) {
console.log(`error.message: "${error.message}"`;
}
Expected behavior
Non-empty error.message