Welcome Guest, Not a member yet? Register   Sign In
Error Handling Coding Styles: What's your Preference?
#1

[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!
#2

[eluser]xwero[/eluser]
I think option 1 is a bit over-abstracted, certainly if it is a stand alone library. You would have to check each time you change something if you don't have overlapping numbers and you have to keep track of them.

I go with option 2 with a few changes
- add an public/not public flag for the errors so it can be dealt with in the controller (redirects too)
- put the error strings in a language file
- on success return an empty string instead of a boolean
To add it all up in code you would have something like this in your controller
Code:
$check = register($password, $username);
if(empty($check))
{
   // success
}
else
{
  $temp = explode('|',$check);
  if($temp[0] == 0) // public error
  {
    // show error + optional redirection
  }
  else
  {
    // show generic error + developers choice + optional redirection
  }
}

If you go with option 3 i think the database failure/file error will be skipped often because it should work, even CI has this 'sin'. I guess it's up to the developer to decide which errors should be cached for display.




Theme © iAndrew 2016 - Forum software by © MyBB