douzi9430 2012-04-18 12:34
浏览 92
已采纳

catch语句中函数的错误处理

This is a piece of code that I was wondering if it should be refactored to make it more complied with the Clean Code practices.

This is a class which is responsible for refunding some orders made by customers.

class RefundServiceInvoker {

    private $_orders;

    public function refundOrder() {   
        $this->getOrdersFromDB(); //This function gets all orders from DB and sets $_orders
        foreach ($this->_orders as $order) { 
            try {
                $order->refund(); //Some lines may throw an exception when refunded due to some business logic (ex. the order was already shipped)
                $this->updateOrderStatus('refunded')            
            } catch (Exception $e) {                   
                $this->logError($e);
                $this->sendMailToAdmin();
            }
        }
    }
}

of course this code is highly simplified than my original code.

My main problem is if $order->refund(); throws an exception it will be caught and logged to DB then a mail is sent. However what if $this->logError($e); itself throws an exception? or what if the mail server was down and an exception was thrown?

Better what if the DB was down itself and $this->getOrdersFromDB(); throws an exception?

My first solution was to wrap everything in one big try{}catch{}:

public function refundOrder() {   
            try {              
            $this->getOrdersFromDB(); //This function gets all orders from DB and sets $_orders
            foreach ($this->_orders as $order) { 

                    $order->refund(); //Some lines may throw an exception when refunded due to some business logic (ex. the order was already shipped)
                    $this->updateOrderStatus('refunded')            
                } catch (Exception $e) {                   
                    $this->logError($e);
                    $this->sendMailToAdmin();
                }
            }
        }

But that would mean if one order fails then all fails!! Should I put 2 try{}catch{} one for the whole function and the other for each order? But in this case too the functions in the catch might throw an exception that won't be caught.

Note:

The application is built using Zend framework 1.11.11.

Thanks in advance.

  • 写回答

3条回答 默认 最新

  • duansanzi5265 2012-04-18 13:12
    关注

    If you need to log and mail every exception, then you need something like this:

    class RefundServiceInvoker {
    
       private $_orders;
    
       public function refundOrder() {
          try {
             $this->getOrdersFromDB();
             foreach ($this->_orders as $order) {
                try {
                   $order->refund();
                } catch (MyBusinessException $e) {
                   # deals with the problematic $order without stopping the loop
                   $this->logError($e);
                   $this->sendMailToAdmin();
                }
                $this->updateOrderStatus('refunded');
             }
          } catch(Exception $e) {
             # deals with unexpected bugs
             $this->logError($e);
             $this->sendMailToAdmin();
          }
       }
    }
    

    But you have to put a try/catch inside log and mail methods to prevent them to throw any exception when servers are offline, for example. If you don’t, the $orders loop will be stopped when log/mail fails.

    If you need to log/mail only your business exception in refund() method, then you need something like this:

    class RefundServiceInvoker {
    
       private $_orders;
    
       public function refundOrder() {
          $this->getOrdersFromDB();
          foreach ($this->_orders as $order) {
             try {
                $order->refund();
             } catch (MyBusinessException $e) {
                # deals with the problematic $order without stopping the loop
                $this->logError($e);
                $this->sendMailToAdmin();
             }
             $this->updateOrderStatus('refunded');
          }
       }
    }
    

    Any other exception will lead to an http 500 – internal server error page, which is usually what you want, because it’s an unexpected bug.

    There’re other ways to handle this, but as you see, it depends on your needs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。