[eluser]Unknown[/eluser]
Hello everyone,
I've been messing around with codeignitor for a while now and came across a design issue that I'd thought I get some opinions on. The issue centers around how to handle errors in a library. Using the example of an authentication library and based on looking at pretty much every auth library made for codeignitor I've seen 3 "styles" of handling errors within the library:
Option 1) Have the library return an error code which can then be interpreted by the controller:
Code:
Library Auth
function register($username, $password) {
if(username and password are null){
return 1
}elsif{username already exists){
return 2
}else{
insert into database
on failure return 3
on success return 4
}
Option 2) Have the library handle the errors internally and redirect to an error page with an appropriate error message set via flash data:
Code:
Library Auth
function register($password, $username){
if(username and password are null){
setflashdata("Error: Unable to create account, please try again")
redirect to error page
}elsif{username already exists){
setflashdata("Error: user already exists")
redirect to error page
}else{
insert into database
on failure, setflashdata("Error: Unable to create account, please try again")
redirect to error page
on success
return true
}
Option 3) Extend the validation class and do all your error checking in the validation class including things that have to be checked against your database. Note that any system errors that the user does not need to know the details about (e.g., insert fails, data wasn't passed properly) would display a generic Error message and would be still handled in the controller. In this case the library would return false for failure but would rely on the validation class to do all error handling that requires a "custom" message (e.g., username already exists)
Note: In all three examples you would use the validator class to validate form input. However, only in option 3 would you extend the validator class to do "advanced" error checking such as checking for duplicate usernames.
I sort have have my own thoughts on each of the above methods but will refrain from inadvertently shaping the conversation by posting them just yet. Also, if I left out some other way to handle errors in the library please feel free to throw something new out. I based the above mainly through looking at others code so would be happy to know if there is a completely separate method that can be used. Thank you for your thoughts!