-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

+1 for exceptions

people who do not read docs could just forget error handling and confuse
themselves or their clients.


Am 25.03.2011 18:28, schrieb Mark C:
> Hi all,
> 
> Just want some opinions on whether the validator should throw an
> exception instead of returning an array, before I go and update the core.
> 
> At the moment, the validator returns an array of errors. Writing code to
> handle these errors would look like the example below (Takes a posted
> user and creates a user or errors e.g. if the name is blank):
> 
> function createUser($name) {
>      // ...
>     $user = new User();
>     $user->setName($name);
>     $errors = $this->validator->validate($user)
>     if ($errors) {
>         return $errors;
>     } else {
>         return $user;
>     }
> }
> 
> function postAction() {
>     // ...
>     $object = $this->createUser($request->getParameter('name'));
>     if ($object instanceof User)
>         return $this->render('HelloBundle:Users:delete.html.twig', array(
>             'user' => $object
>         ));
>     } else {
>          return $this->render('HelloBundle:Users:error.html.twig', array(
>             'errors' => $object
>         ));
>     }
>    
> }
> 
> As you can see you have to handle the errors in each function called,
> resulting in 2 if/elses. By throwing an exception you wouild only need
> to handle it once, at the point you do something different. The code
> would look like this:
> 
> function createUser($name) {
>      // ...
>     $user = new User();
>     $user->setName($name);
>     $this->validator->validate($user);
>     return $user;
> }
> 
> function postAction() {
>     //
>     try {
>         $user = $this->createUser($request->getParameter('myparam'));
>         return $this->render('HelloBundle:Users:delete.html.twig', array(
>             'user' => $user
>         ));
>     } catch (ValidationException $e) {
>         $errors = $e->getMessages();
>         return $this->render('HelloBundle:Users:error.html.twig', array(
>             'errors' => $errors
>         ));
>     }
> }
> 
> Which seems much cleaner and make more sense to me.
> 
> Any thoughts?
> 
> Cheers
> 
> -- 
> If you want to report a vulnerability issue on symfony, please send it
> to security at symfony-project.com
>  
> You received this message because you are subscribed to the Google
> Groups "symfony developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/symfony-devs?hl=en

- -- 
Liip AG // Agile Web Development // T +41 26 422 25 11
CH-1700 Fribourg // PGP 0xA581808B // www.liip.ch
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk2M1LYACgkQqBnXnqWBgIt2wwCfZD3CuPdjvQd0qLDeGTkEZRDj
hHwAn320UpGS9nWmBYCLumzMlkPk+pc5
=0WoN
-----END PGP SIGNATURE-----

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en

Reply via email to