Skip to content

Commit

Permalink
Improve errors handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
sibprogrammer committed Jul 10, 2014
1 parent 471176f commit 7dd9f44
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
16 changes: 9 additions & 7 deletions plib/controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@ public function indexDataAction()
public function updateAddressAction()
{
$ips = Modules_Nat_NatManager::getIpAddresses();
// TODO: add validation
$mainIp = $this->_request->getParam('ip');

if (!isset($ips[$mainIp])) {
$this->_redirect('index');
}

$publicIp = $ips[$mainIp];

$this->view->pageTitle = $this->lmsg('updateAddressPageTitle');

$form = new pm_Form_Simple();
$form->addElement('text', 'mainIp', array(
$form->addElement('simpleText', 'mainIpText', array(
'label' => $this->lmsg('mainIp'),
'value' => $mainIp,
'required' => true,
'validators' => array(
array('NotEmpty', true),
array('Ip', true),
),
));
$form->addElement('text', 'publicIp', array(
'label' => $this->lmsg('publicIp'),
Expand All @@ -45,6 +44,9 @@ public function updateAddressAction()
array('Ip', true),
),
));
$form->addElement('hidden', 'mainIp', array(
'value' => $mainIp,
));
$form->addControlButtons(array(
'cancelLink' => pm_Context::getBaseUrl(),
));
Expand Down
10 changes: 9 additions & 1 deletion plib/library/NatManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public static function getIpAddresses()

public static function updateAddress($mainIp, $publicIp)
{
$ips = self::getIpAddresses();

if (!isset($ips[$mainIp])) {
throw new pm_Exception('Unknown IP address.');
}

$apiResponse = pm_ApiRpc::getService()->call(
'<ip>' .
'<set>' .
Expand All @@ -24,7 +30,9 @@ public static function updateAddress($mainIp, $publicIp)
'</set>' .
'</ip>');

// TODO: add error handler
if ('error' === (string)$apiResponse->ip->set->result->status) {
throw new pm_Exception($apiResponse->ip->set->result->errtext);
}
}

}

0 comments on commit 7dd9f44

Please sign in to comment.