Instead of throwing an error, return an object containing the description of the error, or use a tool like Either
that you can find in some functional libraries.
function throwAnError() {
throw new Error('some error message');
}
function returnAnError() {
return Either.Left(new Error('some error message'));
}