3

I'm using sentry as the auth package for my app, this is my controller method for signing up users.

class Controller_Auth extends \Controller_Base
{

    function action_signup($type='user')
    {
        $user = \Fieldset::forge('new_user');
        $user->add('email', 'Email', array(), array(array('required'), array('valid_email')));
        $user->add('password', 'Password', array(), array(array('required')));
        $user->add('confirm_password', 'Confirm Password', array(), array(array('match_field', 'password') ));
        $user->add('submit', '', array('value'=>'Sign Up', 'class' => 'btn', 'type'=>'submit'));

        $user->repopulate();

        if($user->validation()->run())
        {

            try
            {
                $fields = $user->validated();

                $user_id = \Sentry::user()->create(array('email'=>$fields['email'], 'password'=>$fields['password']));

                if($user_id)
                {
                    // the user was created - send email notifying user account was created
                }
                else
                {
                    // something went wrong - shouldn't really happen
                }
            }
            catch(\SentryUserException $e)
            {
                \Library_Message::set('error', $e->getMessage());
            }
            catch(\Database_Exception $e)
            {
                \Library_Message::set('error', 'Database error occured. Please try again');
            }
        }
        else
        {
            \Library_Message::set('error', $user->validation()->errors());
        }

        $this->template->set('content', $user->form(), false);
    }

}

As you can see I'm mixing up both validation errors and exceptions, I was wondering if there's a way to handle all the errors using exceptions?

Will Vousden
  • 32,488
  • 9
  • 84
  • 95
Sahan H.
  • 472
  • 6
  • 14
  • 9
    This isn't particularly helpful, but arguably you shouldn't use exceptions to indicate validation failures. Exceptions should really be used for "exceptional" circumstances where you're expecting everything to go smoothly. Validation code by its very nature expects failure. – Will Vousden May 02 '12 at 07:28

1 Answers1

0

I suggest to create a private method that throws an Exception on errors to make that homogeneous.

/**
 *
 * @throws Exception
 */
private function validateUserRun($user) {
   if(!$user->validation()->run()) {
       throw new YourException(); //Use a different exception than 'Exception' please
   }
}

And then call it on your try block

function action_signup($type='user') { 
//...
   try {
     $this->validateUserRun($user);
     $fields = $user->validated();
     //...
   } 
   catch(\SentryUserException $e) {
            \Library_Message::set('error', $e->getMessage());
   } catch(\Database_Exception $e) {
            \Library_Message::set('error', 'Database error occured. Please try again');
   } catch (YourException $e) {
      //Here you can catch the exception
   }
//...
}
Maxi Capodacqua
  • 303
  • 1
  • 15