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仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘