From 9047b1cd7d88521379168327e4a2b8b413bcb9ea Mon Sep 17 00:00:00 2001 From: Vaidas Lungis Date: Tue, 17 Feb 2015 12:39:59 +0200 Subject: [PATCH] replace \Exception with\ Box_Exception or \Payment_Exception More exception messages can be translated. \Payment_Exception constructor uses gettext __() for translation. issue #262 --- src/bb-library/Api/Abstract.php | 2 +- src/bb-library/Box/Crypt.php | 2 +- src/bb-library/Model/ProductTable.php | 2 +- src/bb-library/Payment/Adapter/BluePay.php | 2 +- src/bb-library/Payment/Adapter/Interkassa.php | 2 +- src/bb-library/Payment/Adapter/PayPalEmail.php | 6 +++--- src/bb-library/Payment/Adapter/Payza.php | 4 ++-- src/bb-library/Payment/Adapter/TwoCheckout.php | 4 ++-- src/bb-library/Payment/Adapter/WebMoney.php | 2 +- src/bb-library/Payment/Exception.php | 18 +++++++++++++++++- src/bb-library/Registrar/Adapter/Custom.php | 4 ++-- src/bb-library/Registrar/Adapter/Email.php | 4 ++-- .../Spamchecker/akismet.curl.class.php | 9 ++++----- src/bb-modules/Spamchecker/recaptchalib.php | 2 +- 14 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/bb-library/Api/Abstract.php b/src/bb-library/Api/Abstract.php index 8373d925d..12027efea 100644 --- a/src/bb-library/Api/Abstract.php +++ b/src/bb-library/Api/Abstract.php @@ -69,7 +69,7 @@ class Api_Abstract implements InjectionAwareInterface public function getMod() { if(!$this->mod) { - throw new Exception('Mod object is not set for the service'); + throw new Box_Exception('Mod object is not set for the service'); } return $this->mod; } diff --git a/src/bb-library/Box/Crypt.php b/src/bb-library/Box/Crypt.php index 19640a63a..a9255d779 100644 --- a/src/bb-library/Box/Crypt.php +++ b/src/bb-library/Box/Crypt.php @@ -18,7 +18,7 @@ class Box_Crypt implements \Box\InjectionAwareInterface function __construct() { if (!extension_loaded('mcrypt')) { - throw new Exception('php mcrypt extension must be enabled on your server'); + throw new Box_Exception('php mcrypt extension must be enabled on your server'); } } diff --git a/src/bb-library/Model/ProductTable.php b/src/bb-library/Model/ProductTable.php index 1510fb443..9eecf8227 100644 --- a/src/bb-library/Model/ProductTable.php +++ b/src/bb-library/Model/ProductTable.php @@ -88,7 +88,7 @@ class Model_ProductTable implements \Box\InjectionAwareInterface break; default: - throw new Exception('Unknown period selected ' . $code); + throw new Box_Exception('Unknown period selected ' . $code); break; } } diff --git a/src/bb-library/Payment/Adapter/BluePay.php b/src/bb-library/Payment/Adapter/BluePay.php index fa6dd8fc3..18525df1e 100644 --- a/src/bb-library/Payment/Adapter/BluePay.php +++ b/src/bb-library/Payment/Adapter/BluePay.php @@ -201,7 +201,7 @@ To find your ACCOUNT ID and SECRET KEY: public function processTransaction($api_admin, $id, $data, $gateway_id) { if(APPLICATION_ENV != 'testing' && !$this->_isIpnValid($data)) { - throw new Exception('IPN is not valid'); + throw new Payment_Exception('IPN is not valid'); } $ipn = $data['get']; diff --git a/src/bb-library/Payment/Adapter/Interkassa.php b/src/bb-library/Payment/Adapter/Interkassa.php index 1a44a2388..c80de002e 100644 --- a/src/bb-library/Payment/Adapter/Interkassa.php +++ b/src/bb-library/Payment/Adapter/Interkassa.php @@ -179,7 +179,7 @@ class Payment_Adapter_Interkassa extends Payment_AdapterAbstract implements \Box public function processTransaction($api_admin, $id, $data, $gateway_id) { if(!$this->isIpnValid($data)) { - throw new Exception('IPN is not valid'); + throw new Payment_Exception('IPN is not valid'); } $ipn = $data['post']; diff --git a/src/bb-library/Payment/Adapter/PayPalEmail.php b/src/bb-library/Payment/Adapter/PayPalEmail.php index 7ba1b7946..c76eaeaff 100644 --- a/src/bb-library/Payment/Adapter/PayPalEmail.php +++ b/src/bb-library/Payment/Adapter/PayPalEmail.php @@ -31,11 +31,11 @@ class Payment_Adapter_PayPalEmail implements \Box\InjectionAwareInterface $this->config = $config; if(!function_exists('curl_exec')) { - throw new Exception('PHP Curl extension must be enabled in order to use PayPal gateway'); + throw new Payment_Exception('PHP Curl extension must be enabled in order to use PayPal gateway'); } if(!$this->config['email']) { - throw new Exception('Payment gateway "PayPal" is not configured properly. Please update configuration parameter "PayPal Email address" at "Configuration -> Payments".'); + throw new Payment_Exception('Payment gateway "PayPal" is not configured properly. Please update configuration parameter "PayPal Email address" at "Configuration -> Payments".'); } } @@ -147,7 +147,7 @@ class Payment_Adapter_PayPalEmail implements \Box\InjectionAwareInterface public function processTransaction($api_admin, $id, $data, $gateway_id) { if(APPLICATION_ENV != 'testing' && !$this->_isIpnValid($data)) { - throw new Exception('IPN is not valid'); + throw new Payment_Exception('IPN is not valid'); } $ipn = $data['post']; diff --git a/src/bb-library/Payment/Adapter/Payza.php b/src/bb-library/Payment/Adapter/Payza.php index b7e958d7f..8a5845eae 100644 --- a/src/bb-library/Payment/Adapter/Payza.php +++ b/src/bb-library/Payment/Adapter/Payza.php @@ -19,11 +19,11 @@ class Payment_Adapter_Payza extends Payment_AdapterAbstract $this->config = $config; if(!function_exists('curl_exec')) { - throw new Exception('PHP Curl extension must be enabled in order to use Payza gateway'); + throw new Payment_Exception('PHP Curl extension must be enabled in order to use Payza gateway'); } if(!$this->config['email']) { - throw new Exception('Payment gateway "Payza" is not configured properly. Please update configuration parameter "Payza Email address" at "Configuration -> Payments".'); + throw new Payment_Exception('Payment gateway "Payza" is not configured properly. Please update configuration parameter "Payza Email address" at "Configuration -> Payments".'); } } diff --git a/src/bb-library/Payment/Adapter/TwoCheckout.php b/src/bb-library/Payment/Adapter/TwoCheckout.php index 657fd5f8e..ab13c7681 100644 --- a/src/bb-library/Payment/Adapter/TwoCheckout.php +++ b/src/bb-library/Payment/Adapter/TwoCheckout.php @@ -116,7 +116,7 @@ class Payment_Adapter_TwoCheckout implements \Box\InjectionAwareInterface $ipn = array_merge($data['get'], $data['post']); if(APPLICATION_ENV != 'testing' && !$this->_isIpnValid($ipn)) { - throw new Exception('2Checkout IPN is not valid'); + throw new Payment_Exception('2Checkout IPN is not valid'); } if($ipn['message_type'] == 'ORDER_CREATED') { @@ -172,7 +172,7 @@ class Payment_Adapter_TwoCheckout implements \Box\InjectionAwareInterface } if(!$invoice_id) { - throw new Exception('Invoice id could not be determined for this transaction'); + throw new Payment_Exception('Invoice id could not be determined for this transaction'); } $invoice = $api_admin->invoice_get(array('id'=>$invoice_id)); diff --git a/src/bb-library/Payment/Adapter/WebMoney.php b/src/bb-library/Payment/Adapter/WebMoney.php index 73a31fdd3..aedc7edea 100644 --- a/src/bb-library/Payment/Adapter/WebMoney.php +++ b/src/bb-library/Payment/Adapter/WebMoney.php @@ -104,7 +104,7 @@ class Payment_Adapter_WebMoney implements \Box\InjectionAwareInterface public function processTransaction($api_admin, $id, $data, $gateway_id) { if(APPLICATION_ENV != 'testing' && !$this->isIpnValid($data)) { - throw new Exception('WebMoney IPN is not valid'); + throw new Payment_Exception('WebMoney IPN is not valid'); } $ipn = $data['post']; diff --git a/src/bb-library/Payment/Exception.php b/src/bb-library/Payment/Exception.php index b397437b2..dbaa7d541 100644 --- a/src/bb-library/Payment/Exception.php +++ b/src/bb-library/Payment/Exception.php @@ -11,4 +11,20 @@ */ class Payment_Exception extends Exception -{} +{ + /** + * Creates a new translated exception. + * + * @param string error message + * @param array translation variables + * @return Exception|null + */ + public function __construct($message, array $variables = NULL, $code = 0) + { + // Set the message + $message = __($message, $variables); + + // Pass the message to the parent + parent::__construct($message, $code); + } +} diff --git a/src/bb-library/Registrar/Adapter/Custom.php b/src/bb-library/Registrar/Adapter/Custom.php index 3727a1498..0da844c4f 100644 --- a/src/bb-library/Registrar/Adapter/Custom.php +++ b/src/bb-library/Registrar/Adapter/Custom.php @@ -467,14 +467,14 @@ class Whois { // If tldname have not been found if($whois_server == "") { - throw new Exception('No whois server for this tld in list!'); + throw new Box_Exception('No whois server for this tld in list!'); } // Getting whois information $fp = @fsockopen($whois_server,43); if (!$fp) { - throw new Exception('Whois server '. $whois_server .' is not available'); + throw new Box_Exception('Whois server '. $whois_server .' is not available'); } $dom=$domainname.".".$tldname; diff --git a/src/bb-library/Registrar/Adapter/Email.php b/src/bb-library/Registrar/Adapter/Email.php index a3baa5cdc..73273ca8c 100644 --- a/src/bb-library/Registrar/Adapter/Email.php +++ b/src/bb-library/Registrar/Adapter/Email.php @@ -534,14 +534,14 @@ class Whois2 { // If tldname have not been found if($whois_server == "") { - throw new Exception('No whois server for this tld in list!'); + throw new Box_Exception('No whois server for this tld in list!'); } // Getting whois information $fp = @fsockopen($whois_server,43); if (!$fp) { - throw new Exception('Whois server '. $whois_server .' is not available'); + throw new Box_Exception('Whois server '. $whois_server .' is not available'); } $dom=$domainname.".".$tldname; diff --git a/src/bb-modules/Spamchecker/akismet.curl.class.php b/src/bb-modules/Spamchecker/akismet.curl.class.php index a97294534..5bd3cae64 100755 --- a/src/bb-modules/Spamchecker/akismet.curl.class.php +++ b/src/bb-modules/Spamchecker/akismet.curl.class.php @@ -81,8 +81,8 @@ class akismet { if($api_key) { $this->api_key = $api_key; } else { - throw new Exception('Akismet API Key not set.'); $this->error = true; + throw new Exception(__('Akismet API Key not set.')); } //If no site URL was given @@ -124,7 +124,7 @@ class akismet { if(!is_resource($this->connection_handle)) { //Try to create one if(!$this->connection_handle = curl_init()) { - throw new Exception('Could not start new CURL instance'); + throw new Exception(__('Could not start new CURL instance')); $this->error = true; return; } @@ -161,9 +161,8 @@ class akismet { if(is_resource($this->connection_handle)) { //Try to close it if(!curl_close($this->connection_handle)) { - throw new Exception('Could not close the CURL instance'); $this->error = true; - return; + throw new Exception(__('Could not close the CURL instance')); } } return true; @@ -188,8 +187,8 @@ class akismet { //Send Data and grab the result if(!$response = curl_exec($this->connection_handle)) { - throw new Exception('Could not send cURL request'); $this->error = true; + throw new Exception(__('Could not send cURL request')); return; } diff --git a/src/bb-modules/Spamchecker/recaptchalib.php b/src/bb-modules/Spamchecker/recaptchalib.php index 941d7424c..a2f9df223 100644 --- a/src/bb-modules/Spamchecker/recaptchalib.php +++ b/src/bb-modules/Spamchecker/recaptchalib.php @@ -78,7 +78,7 @@ function _recaptcha_http_post($host, $path, $data, $port = 80) { $response = ''; if( false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) { - throw new Exception('Could not open socket'); + throw new Exception(__('Could not open socket')); } fwrite($fs, $http_request);