Say, I have a function, RegisterUser()
which takes in an user's registration details such as their username, name and password and registers them if there is no invalid data. For such functions, it is important to report all errors at once, instead of stopping at the very first test at which validation fails.
An exception is probably out of the question here, since the general practice is to immediately throw them once an error is encountered. The only way I could think of doing this is using an integer field where various error values are ORed and returned; however, I haven't really seen this being used anywhere; not to mention the very complicated and confusing code that the handling would require:
rv = RegisterUser(...)
err_msg = []
if (rv != 0) {
err_msg_lookup_table = ["The username was invalid.", "That username is already taken up.", ...]
for (i = 1; i < MAX_INT_VAL; i *= 2) {
if (rv & i == i) {
err_msg.push(err_msg_lookup_table[log(i, 2)])
}
}
ShowMessage(err_msg)
}
Is there a better way to do it?
This is a dupe but doesn't have answers.