I have a function within a service structured like this:
The Policies
array contains functions that will throw an error if a policy is violated (e.g. password missing numbers or capital letters).
function myFunction(policies, password){
var policiesError = '';
policies.forEach(function(policy) {
try {
policy(password, payload);
} catch (err) {
policiesError += err.message + '\n'; // Also tried `concat()`
}
});
if (policiesError){
throw new Error(policiesError);
}
}
In the controller
, I've implemented:
try{
myService.myFunction(myFunctions, 'password');
} catch (err){
$scope.policiesError = err.message; // Displayed as {{policiesError}}
}
The current message output in the controller appears as:
"Password too shortPassword has no lettersInvalid characters in password"
However, the desired formatting should be:
"Password too short
Password has no letters
Invalid characters in password"
It's a seemingly straightforward issue, but despite multiple attempts to tackle it, I'm still struggling.
Any suggestions for achieving a well-formatted message using new Error
and proper error handling techniques?