이 코드는 Clean Code 사례를보다 잘 준수하도록 리팩토링해야하는지 궁금해하고있는 부분입니다.catch 문에서 함수에 대한 오류 처리
고객이 주문한 일부 주문에 대한 환불을 담당하는 클래스입니다.
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();
}
}
}
}
물론이 코드는 원래 코드보다 매우 단순합니다.
내 주된 문제는 $order->refund();
이 예외를 throw하면 DB에 걸려서 로그온되어 메일이 전송된다는 것입니다. 그러나 $this->logError($e);
자체가 예외를 throw하면 어떻게됩니까? 또는 메일 서버가 다운되어 예외가 발생하면 어떻게해야합니까?
DB가 다운되어 $this->getOrdersFromDB();
이 예외를 throw하면 어떻게 될까요?
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();
}
}
}
그러나 하나 개의 순서는 모든 실패 실패 할 경우 그 의미! :
나의 첫 번째 솔루션은 하나의 큰 try{}catch{}
모든 것을 포장했다 전체 기능에 대해 2 개의 try{}catch{}
을 넣어야하고 각 주문에 대해 다른 하나를 넣어야합니까? 그러나이 경우에도 catch의 함수가 catch되지 않는 예외를 throw 할 수 있습니다.
참고 :
응용 프로그램이 젠드 프레임 워크 1.11.11를 사용하여 구축된다.
미리 감사드립니다.
'$ this-> logError()'는 실제로 예외를 던집니까? 그게 모범 사례가 아닌 것처럼 보입니다. 무한 루프에 쉽게 끝날 수 있습니다. 오류 로깅/관리자 전자 메일 전송에 실패하면 응용 프로그램에서 수행 할 작업을 결정해야합니다. 오류를 무시하고 계속 진행하면 해당 메소드의 예외를 throw하지 마십시오. 스택 트레이스를 가지고 죽고 싶다면, 예외를 잡아서는 안된다. 당신이 그 상황을 어떻게 다루는 지 알고 싶다면, 내부 try/catch를 사용하십시오. IMHO ... – DaveRandom
기본적으로 yeh 또는 위의 코드의 'catch'블록 안에 -'try {throw new Exception(); } catch (Exception $ e) {try {handle_exception ($ e); } catch (Exception $ e2) {}}' – DaveRandom