EllisLab text mark
Advanced Search
     
Method chaining and errors
Posted: 20 July 2010 05:09 AM
Avatar
Joined: 2008-10-20
398 posts

I’m working on a small image library and I’m playing with method chaining. This is my first time of writing method chaining and I understand the principal, but not sure on how to handle errors.

$lib->load_image("x")->resize(200,200); 

Pretty simple, load_image should return $this, but what if the file was not found? Do I just set my error internally and you check after?

$lib->load_image("x")->resize(200,200);
if (
$lib->error) print $lib->display_errors(); 

Is that the correct way to handle it? I don’t want to just kill the script!

 Signature 

Mat-Moo
Image moo - the easy to use image library!
MatMoo.com!
E-Mail Remind - Free reminders by email
Printfetti - Your photos on confetti!

 
Posted: 20 July 2010 11:38 AM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-10-18
334 posts

I usually make these kind of methods return something (or TRUE), the image maybe in this case, or FALSE on failure.
Then I can do this:

if( ! $new_img $lib->resize()) {
  
echo $lib->errors();

For more critical errors I just throw exceptions with an exception thrower (a method that helps me throw different kinds of exceptions) and try to catch them, or let a custom exception handler do it.

 Signature 

Wallpapers and Images Site: Desktop Wallpapers