开发者

Handling exceptions and returning a response object from a service layer

I have a service layer which is responsible for handling exceptions.

The question is, should I be handling except开发者_JS百科ions in the service layer and how can I pass an appropriate exception message to a view?

class App_Service_Feed {
  function create() {
    //...
    try {
      $feed->getMapper->insert();
    } catch (Zend_Db_Statement_Exception $e) {
      //what do I return here?        
    } catch (Exception $e) {
      //what do I return here?
    }
  }
}

I'm thinking of returning a response object of some description, so that in my controller I manipulate this.

class App_Controller {
  //...
  $response = $service->create();
  if($response->status) {

  }
}

Alternatively, I'm wondering whether to handle exceptions in the controller...


Even better than the way of jason bourne (yeah):

class App_Service_Feed {
  function create() {
    //...
    try {
      $feed->getMapper->insert();
    } 
    catch (Zend_Db_Statement_Exception $e) 
    {
      throw new App_Service_Feed_Exception("Your own message", NULL, $e);      
    } 
    catch (Exception $e) 
    {
      throw new App_Service_Feed_Exception("Your other message", NULL, $e);
    }
  }
}

Why is this better?

  • You are using your own Exception class (extending Zend_Exception). So you can immediately see where the exception was thrown and you can build in your own additional checks, etc.
  • You are passing the last exception along to have more history information (trace) about Exception.

The best way to implement Exceptions is to have a hierarchy of extending Exception classes.

App_Exception extends Zend_Exception
App_Service_Exception extends App_Exception
App_Service_Feed_Exception extends App_Service_Exception

So every folder contains an Exception.php. This way you can catch and rethrow Exceptions on every level, if necessary.


All you need to do is throw the exception for Zend Front controller to catch it latter

class App_Service_Feed {
  function create() {
    //...
    try {
      $feed->getMapper->insert();
    } catch (Zend_Db_Statement_Exception $e) {
      throw new Zend_Exception("my own message");      
    } catch (Exception $e) {
      throw new Zend_Exception("my different message");
    }
  }
}


You can follow this approach I normally follow it when I use to do exception handling:

class App_Service_Feed {


function create() throws CustomException, OtherCustomException {
    //...
    try {
      $feed->getMapper->insert();
    } catch (Zend_Db_Statement_Exception $e) {
      //you can throw ur custom exception here. 
      //By doing so you can increase its functionality and understand what is the problem
       throw new CustomException();       
    } catch (Exception $e) {
      //here u can check some general exception like NullPointer, IOException etc(related 
      //to ur case) using instanceof.
      throw new OtherCustomException

    }
  }
}

Now in your controller you need to handle this exception and show some message:-

class App_Controller {
  //...
  App_Service_Feed obj = new App_Service_Feed();
  try{  
   obj.create()
  }catch(CustomException c)
   {
     //display message
   }catch(OtherCustomException o)
    {
      //display other message
    }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜