Catching an exception in MVC type structure.

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Catching an exception in MVC type structure.

Post by social_experiment »

I have the following pages:

1. Class page
2. 'Controller' page
3. index page

On the controller page, I have the following code

Code: Select all

<?php
  // included class page, created object
try {		
   $data = $prodObj->getRandomProducts();
   if ($data !== FALSE) {
     $productArray = $data;
   }
   else {
      // no data in array, 0 records.
   }		
}
   catch (Exception $e) {
     var_dump($e->getTrace());
   }
?>
On the index page I want to use $productArray to display products.

Code: Select all

  // index page, display data
  <p><?php echo $productArray['product_id']; ?></p>
Where do I catch the exception, on the controller page or on the index page? Could I catch the exception inside the class, in the wrapper function?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Catching an exception in MVC type structure.

Post by requinix »

Depends how you want to react to the exception.

If you want to show an error page, that suggests changing the view and it should be done in the controller.
If you want to show the same page but with an error message, that suggests doing it in the view. However I think I'd use a second view for it instead of passing the information along to your index view.

Not sure what the "wrapper function" is.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Catching an exception in MVC type structure.

Post by social_experiment »

It's the public function inside the class that calls a protected function
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
jkon
Forum Newbie
Posts: 16
Joined: Mon Feb 15, 2010 6:01 am

Re: Catching an exception in MVC type structure.

Post by jkon »

From the 3 aspects that you described ( 1. Class page 2. 'Controller' page 3. index page) I can't understand what is the “top” . Generally , exceptions should be cached through the top level component of an application. By top I mean the one that is in the core of the app and generates all the others (e.g. the index.php isn't the top normally because it is just the instantiator of the front controller in MVC). That doesn't mean that each tier of the app should try – catch , the opposite. They should if they have to do something with that exception and then through it back to parent. If children's or other tiers only catch the exception to through it up (or again) there is no meaning to do so and is a sign of a week understanding of what Exceptions are (in my opinion).
User avatar
Christopher
Site Administrator
Posts: 13595
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Catching an exception in MVC type structure.

Post by Christopher »

It seems like $prodObj->getRandomProducts() should generate the exception and then whatever code above it can deal with the exception. Having that if() indicates that maybe you are calling things in the wrong place.
(#10850)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Catching an exception in MVC type structure.

Post by social_experiment »

jkon wrote:Generally , exceptions should be cached through the top level component of an application.
The top of these three would be the class; is your suggestion to catch the exception within the class?
Christopher wrote:that maybe you are calling things in the wrong place.
It would be better to catch the exception within getRandomProducts() ?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Christopher
Site Administrator
Posts: 13595
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Catching an exception in MVC type structure.

Post by Christopher »

The question I would ask is: what is the exception communicating? Is it that some actual error occurred? Or is it just that no product records were found? For me the difference is what the View code displays to the user. If it is an error then I want to display something that the user can tell tech support that communicates the problem. If it is just no records then just display that no records were found.
(#10850)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Catching an exception in MVC type structure.

Post by social_experiment »

Christopher wrote:The question I would ask is: what is the exception communicating? Is it that some actual error occurred?
The exception is to display an error, a database error in this case.

In the latest version of the code I've done the following:

- Moved the try / catch block into the class
- the 'controller' page creates the data
- index page uses the data
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Christopher
Site Administrator
Posts: 13595
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Catching an exception in MVC type structure.

Post by Christopher »

It seem like the index page that uses the data should get the $prodObj object. It can then catch the exception if there is a database error, or separately check if no results are returned and let the user know about that.
(#10850)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Catching an exception in MVC type structure.

Post by social_experiment »

Christopher wrote:It seem like the index page that uses the data should get the $prodObj object.
something to this effect:

Code: Select all

<?php
 // index page
 include_once 'controller.php';

 try {
  $data = $prodObj->getRandomProducts();
 }
 catch (Exception $e) {
   // display custom error
 } 
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Christopher
Site Administrator
Posts: 13595
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Catching an exception in MVC type structure.

Post by Christopher »

Yes, I guess. It is still not clear which file is which.
(#10850)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Catching an exception in MVC type structure.

Post by social_experiment »

this would be on the index page
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Christopher
Site Administrator
Posts: 13595
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Catching an exception in MVC type structure.

Post by Christopher »

I guess my question is: Why do you need an exception? It seems like if($data) is all you need. Either some random products are found, or the array is empty. If there is a syntax error it will show up in the logs. An SQL error should be fixed in development. So either display the random products or show that there are none.
(#10850)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Catching an exception in MVC type structure.

Post by social_experiment »

Christopher wrote:Why do you need an exception?
currently it helps with debugging but should something happen during the query process i'd like the application to degrade as gracefully as possible - replace the error message with something more user friendly and email the error message to an administrator
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply