2014-07-16 73 views
0

我在我的控制器中有以下功能來處理準備和加載我的主頁。如您所見,我通過我的模型對PayPal API進行了2次不同的調用,並且在每次調用後我都檢查錯誤。出現錯誤時,我會刷新錯誤消息並相應地重定向到錯誤頁面。困惑如何改善這個功能..?

我想改善這一點,所以當我在加載視圖之前進行一系列調用時,我不必一再使用相同的代碼片段。

if(Session::has('errors')) 
     { 
      return Redirect::to('error'); 
     } 

我試着動這對自身的功能......

public function errorCheck() 
    { 
     if(Session::has('errors')) 
     { 
      return Redirect::to('error'); 
     } 
    } 

然後,我想我可能只是做我的指數函數內...

// GetBalance 
     $current_balance = $this->PayPal->getBalance(); 
     $this->errorCheck(); 

那並不我工作,但我猜,因爲errorCheck()只是返回一個值,並沒有實際觸發重定向,所以我最終在我的主頁上發生錯誤,因爲它沒有預期的數據存在(因爲API調用失敗)。

有關我需要做什麼的任何信息,以便我非常感謝我的errorCheck()函數只是簡單地觸發重定向。

謝謝!

回答

1

我能想到的避免這種情況的唯一方法是通過使用例外。

與您errorCheck()方法,你可以試試這個:

// GetBalance 
$current_balance = $this->PayPal->getBalance(); 
return $this->errorCheck(); 

但是這不是你想要的......它會在第一次調用後退出你的方法。你真正需要的是一種方法,以便捕獲無論它出現在哪裏並處理它 - 這就是例外情況。

我打算假設你的PayPal類是第三方包,你不能重寫它來拋出異常。鑑於這樣的假設,你可以做的是這樣的:

重寫你的errorCheck()方法,像這樣:

public function errorCheck() 
{ 
    if(Session::has('errors')) 
    { 
     throw new Exception("Problem with Paypal!"); 
    } 
} 

然後包裝所有您的Paypal訪問代碼在一個try/catch塊:

public function index() 
{ 
    try { 
     // GetBalance 
     $current_balance = $this->PayPal->getBalance(); 

     $this->errorCheck(); 

     // TransactionSearch 
     $params = array(
      'number_of_days' => 1 
     ); 
     $recent_history = $this->PayPal->transactionSearch($params); 

     $this->errorCheck(); 

     // Make View 
     $data = array('current_balance' => $current_balance, 'recent_history' => $recent_history); 
     return View::make('index')->with('data', $data); 
    } catch(Exception $e) { 
     return Redirect::to('error'); 
    } 
} 

每次您致電errorCheck()時,它都會檢查錯誤並拋出異常。在這種情況下,執行將立即跳轉到catch塊並重定向到錯誤頁面。

更好的解決方案是拋出異常更接近錯誤的來源,即。發生錯誤時在Paypal類的某處。這裏的想法是,這個例外包含了很多有用的信息,告訴你發生了什麼,比如堆棧跟蹤。在我給出的代碼中,堆棧跟蹤將顯示異常是在errorCheck()方法中拋出的,雖然這是真的,但並不真正有用。如果這個異常可能會在Paypal類的某個地方拋出,它會給你一個更好的指示,說明真正的錯誤。

+0

美麗。謝謝! –

0

爲什麼你甚至需要檢查錯誤兩次?它似乎並不涉及每個電話?即如果餘額調用失敗似乎不重要,因爲您不在交易搜索中使用結果。

我只是這樣做

public function index() 
{ 
     // GetBalance 
     $current_balance = $this->PayPal->getBalance(); 

     // TransactionSearch 
     $recent_history = $this->PayPal->transactionSearch(array('number_of_days' => 1)); 

     if(Session::has('errors')) 
     { 
      return Redirect::to('error'); 
     } 
     else 
     { 
      return View::make('index')->with('current_balance', $current_balance) 
             ->with('recent_history', $recent_history); 
     } 
} 
+0

我沒有在transactionSearch()中使用getBalance()的結果,但是,如果getBalance()中的數據沒有返回,那麼我的主頁無法正確加載,所以我可能不會浪費資源額外調用transactionSearch()。 –

+0

資源量可能不重要。代碼可維護性和調試的簡易性在這裏將變得更加重要。由您決定...... – Laurence

+0

PayPal/eBay /等不喜歡它,當你對他們的系統進行一些不必要的調用時,它們可能會限制任何給定應用程序可以進行的API調用次數一段時間。因此,這不是我自己的服務器資源問題,因爲它正在咀嚼任何可能會對API服務提供商調用的潛在風險。不過,我很欣賞你的反饋。 –

1

雖然拋出一個錯誤,肯定是要走的路,我會說你走了一步,並概括了重定向。每次調用PayPal API時,不要每次嘗試catch塊,都可以使用App::error來進行全局重定向。

創建一個異常類的地方適當的:

class PayPalApiException extends Exception {} 

然後在你的start/global.php添加這個(前其他App::error調用):

App::error(function(PayPalApiException $exception) 
{ 
    return Redirect::to('error'); 
}); 

然後你在控制器代碼可以變得更簡單:

public function index() 
{ 
    $current_balance = $this->PayPal->getBalance(); 

    $this->errorCheck(); 

    $recent_history = $this->PayPal->transactionSearch([ 
     'number_of_days' => 1 
    ]); 

    $this->errorCheck(); 

    $data = compact('current_balance', 'recent_history'); 

    return View::make('index')->with('data', $data); 
} 

protected function errorCheck() 
{ 
    if (Session::has('errors')) 
    { 
     throw new PayPalApiException("Problem with Paypal!"); 
    } 
}