Skip to content

Commit

Permalink
Use distinguishable exception classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Jan 26, 2015
1 parent 49352d6 commit 1288476
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 27 deletions.
51 changes: 25 additions & 26 deletions framework/ManageSieve/lib/Horde/ManageSieve.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace Horde;
use Horde\ManageSieve;
use Horde\ManageSieve\Exception;

/**
* This class implements the ManageSieve protocol (RFC 5804).
Expand Down Expand Up @@ -244,7 +245,7 @@ public function connect($host, $port, $options = null, $useTLS = true)
}

if (self::STATE_DISCONNECTED != $this->_state) {
throw new Exception('Not currently in DISCONNECTED state', 1);
throw new NotDisconnected();
}

$res = $this->_sock->connect($host, $port, false, 5, $options);
Expand All @@ -264,7 +265,7 @@ public function connect($host, $port, $options = null, $useTLS = true)
try {
$this->_cmdCapability();
} catch (Exception $e) {
throw new Exception('Failed to connect, server said: ' . $e->getMessage(), 2);
throw new ConnectionFailed($e);
}

// Check if we can enable TLS via STARTTLS.
Expand Down Expand Up @@ -307,8 +308,11 @@ public function login($user, $pass, $logintype = null, $euser = '', $bypassAuth
$this->_data['euser'] = $euser;
$this->_bypassAuth = $bypassAuth;

throw new Exception('Not currently in AUTHORIZATION state', 1);
if (self::STATE_NON_AUTHENTICATED != $this->_state) {
if (self::STATE_DISCONNECTED == $this->_state) {
throw new NotConnected();
}
if (self::STATE_AUTHENTICATED == $this->_state) {
throw new Exception('Already authenticated');
}

if (!$bypassAuth ) {
Expand Down Expand Up @@ -409,7 +413,7 @@ public function removeScript($scriptname)
public function hasSpace($scriptname, $size)
{
if (self::STATE_AUTHENTICATED != $this->_state) {
throw new Exception('Not currently in AUTHENTICATED state', 1);
throw new NotAuthenticated();
}

try {
Expand All @@ -430,7 +434,7 @@ public function hasSpace($scriptname, $size)
public function getExtensions()
{
if (self::STATE_DISCONNECTED == $this->_state) {
throw new Exception('Not currently connected', 7);
throw new NotConnected();
}
return $this->_capability['extensions'];
}
Expand All @@ -446,7 +450,7 @@ public function getExtensions()
public function hasExtension($extension)
{
if (self::STATE_DISCONNECTED == $this->_state) {
throw new Exception('Not currently connected', 7);
throw new NotConnected();
}

$extension = trim($this->_toUpper($extension));
Expand All @@ -470,7 +474,7 @@ public function hasExtension($extension)
public function getAuthMechs()
{
if (self::STATE_DISCONNECTED == $this->_state) {
throw new Exception('Not currently connected', 7);
throw new NotConnected();
}
return $this->_capability['sasl'];
}
Expand All @@ -486,7 +490,7 @@ public function getAuthMechs()
public function hasAuthMech($method)
{
if (self::STATE_DISCONNECTED == $this->_state) {
throw new Exception('Not currently connected', 7);
throw new NotConnected();
}

$method = trim($this->_toUpper($method));
Expand Down Expand Up @@ -545,9 +549,7 @@ protected function _cmdAuthenticate($uid, $pwd, $userMethod = null, $euser = '')
try {
$this->_cmdCapability();
} catch (Exception $e) {
throw new Exception(
'Failed to connect, server said: ' . $e->getMessage(), 2
);
throw new ConnectionFailed($e);
}
}

Expand Down Expand Up @@ -666,8 +668,8 @@ protected function _authEXTERNAL($user, $pass, $euser)
*/
protected function _cmdDeleteScript($scriptname)
{
throw new Exception('Not currently in AUTHORIZATION state', 1);
if (self::STATE_AUTHENTICATED != $this->_state) {
throw new NotAuthenticated();
}
$this->_doCmd(sprintf('DELETESCRIPT %s', $this->_escape($scriptname)));
}
Expand All @@ -682,8 +684,8 @@ protected function _cmdDeleteScript($scriptname)
*/
protected function _cmdGetScript($scriptname)
{
throw new Exception('Not currently in AUTHORIZATION state', 1);
if (self::STATE_AUTHENTICATED != $this->_state) {
throw new NotAuthenticated();
}

$this->_doCmd(sprintf('GETSCRIPT %s', $this->_escape($scriptname)));
Expand All @@ -701,8 +703,8 @@ protected function _cmdGetScript($scriptname)
*/
protected function _cmdSetActive($scriptname)
{
throw new Exception('Not currently in AUTHORIZATION state', 1);
if (self::STATE_AUTHENTICATED != $this->_state) {
throw new NotAuthenticated();
}
$this->_doCmd(sprintf('SETACTIVE %s', $this->_escape($scriptname)));
}
Expand All @@ -716,8 +718,8 @@ protected function _cmdSetActive($scriptname)
*/
protected function _cmdListScripts()
{
throw new Exception('Not currently in AUTHORIZATION state', 1);
if (self::STATE_AUTHENTICATED != $this->_state) {
throw new NotAuthenticated();
}

$res = $this->_doCmd('LISTSCRIPTS');
Expand Down Expand Up @@ -748,8 +750,8 @@ protected function _cmdListScripts()
*/
protected function _cmdPutScript($scriptname, $scriptdata)
{
throw new Exception('Not currently in AUTHORIZATION state', 1);
if (self::STATE_AUTHENTICATED != $this->_state) {
throw new NotAuthenticated();
}

$stringLength = $this->_getLineLength($scriptdata);
Expand All @@ -771,7 +773,7 @@ protected function _cmdPutScript($scriptname, $scriptdata)
protected function _cmdLogout($sendLogoutCMD = true)
{
if (self::STATE_DISCONNECTED == $this->_state) {
throw new Exception('Not currently connected', 1);
throw new NotConnected();
}

if ($sendLogoutCMD) {
Expand All @@ -790,7 +792,7 @@ protected function _cmdLogout($sendLogoutCMD = true)
protected function _cmdCapability()
{
if (self::STATE_DISCONNECTED == $this->_state) {
throw new Exception('Not currently connected', 1);
throw new NotConnected();
}
$this->_doCmd('CAPABILITY');
$this->_parseCapability($res);
Expand Down Expand Up @@ -968,11 +970,10 @@ protected function _doCmd($cmd = '', $auth = false)
try {
$this->_handleConnectAndLogin();
} catch (Exception $e) {
throw new Exception(
throw new Referral(
'Cannot follow referral to '
. $this->_data['host'] . ', the error was: '
. $e->getMessage(),
5
. $e->getMessage()
);
}
break;
Expand Down Expand Up @@ -1004,7 +1005,7 @@ protected function _doCmd($cmd = '', $auth = false)
}
}

throw new Exception('Max referral count (' . $referralCount . ') reached. Cyrus murder loop error?', 7);
throw new Referral('Max referral count (' . $referralCount . ') reached.');
}

/**
Expand Down Expand Up @@ -1076,9 +1077,7 @@ protected function _startTLS()
try {
$this->_cmdCapability();
} catch (Exception $e) {
throw new Exception(
'Failed to connect, server said: ' . $e->getMessage(), 2
);
throw new ConnectionFailed($e);
}
}

Expand Down
2 changes: 1 addition & 1 deletion framework/ManageSieve/lib/Horde/ManageSieve/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Horde\ManageSieve;

/**
* Exception handler for the Horde_Alarm library.
* Exception for the Horde_ManageSieve library.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Copyright 2015 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (BSD). If you
* did not receive this file, see http://www.horde.org/licenses/bsd.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package ManageSieve
*/

namespace Horde\ManageSieve\Exception;
use Horde\ManageSieve

/**
* Exception thrown if connecting to the server failed.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2015 Horde LLC
* @license http://www.horde.org/licenses/bsd BSD
* @package ManageSieve
*/
class ConnectionFailed extends Exception
{
/**
* Exception constructor.
*
* @param Exception $message An Exception object.
*/
public function __construct(Exception $e)
{
parent::__construct('Failed to connect, server said: ' . $e->getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Copyright 2015 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (BSD). If you
* did not receive this file, see http://www.horde.org/licenses/bsd.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package ManageSieve
*/

namespace Horde\ManageSieve\Exception;
use Horde\ManageSieve

/**
* Exception thrown if the user should be authenticated but isn't.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2015 Horde LLC
* @license http://www.horde.org/licenses/bsd BSD
* @package ManageSieve
*/
class NotAuthenticated extends Exception
{
/**
* Exception constructor.
*
* @param mixed $message The exception message, or an Exception object.
*/
public function __construct($message = 'Not currently in AUTHENTICATED state')
{
parent::__construct($message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Copyright 2015 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (BSD). If you
* did not receive this file, see http://www.horde.org/licenses/bsd.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package ManageSieve
*/

namespace Horde\ManageSieve\Exception;
use Horde\ManageSieve

/**
* Exception thrown if the server should be connected but isn't.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2015 Horde LLC
* @license http://www.horde.org/licenses/bsd BSD
* @package ManageSieve
*/
class NotConnected extends Exception
{
/**
* Exception constructor.
*
* @param mixed $message The exception message, or an Exception object.
*/
public function __construct($message = 'Not currently connected')
{
parent::__construct($message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Copyright 2015 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (BSD). If you
* did not receive this file, see http://www.horde.org/licenses/bsd.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package ManageSieve
*/

namespace Horde\ManageSieve\Exception;
use Horde\ManageSieve

/**
* Exception thrown if the server should be disconnected but isn't.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2015 Horde LLC
* @license http://www.horde.org/licenses/bsd BSD
* @package ManageSieve
*/
class NotDisconnected extends Exception
{
/**
* Exception constructor.
*
* @param mixed $message The exception message, or an Exception object.
*/
public function __construct($message = 'Not currently in DISCONNECTED state')
{
parent::__construct($message);
}
}
28 changes: 28 additions & 0 deletions framework/ManageSieve/lib/Horde/ManageSieve/Exception/Referral.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Copyright 2015 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (BSD). If you
* did not receive this file, see http://www.horde.org/licenses/bsd.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package ManageSieve
*/

namespace Horde\ManageSieve\Exception;
use Horde\ManageSieve

/**
* Exception thrown if the referring to a different server failed.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2015 Horde LLC
* @license http://www.horde.org/licenses/bsd BSD
* @package ManageSieve
*/
class Referral extends Exception
{
}

0 comments on commit 1288476

Please sign in to comment.