From cf721efad28cde6a621e2566415887ff895c6c84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Proch=C3=A1zka?= Date: Mon, 28 Dec 2020 12:42:12 +0100 Subject: [PATCH 1/5] Adding more detail ClientExceptions for credential and connection error --- src/Client.php | 8 ++++++-- src/Exceptions/BadCredentialsException.php | 13 +++++++++++++ src/Exceptions/ConnectException.php | 12 ++++++++++++ src/SocketTrait.php | 4 +++- tests/ClientTest.php | 8 +++++--- 5 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 src/Exceptions/BadCredentialsException.php create mode 100644 src/Exceptions/ConnectException.php diff --git a/src/Client.php b/src/Client.php index ba714a9..e49f0ce 100644 --- a/src/Client.php +++ b/src/Client.php @@ -4,6 +4,8 @@ namespace RouterOS; use DivineOmega\SSHConnection\SSHConnection; use RouterOS\Exceptions\ClientException; +use RouterOS\Exceptions\ConnectException; +use RouterOS\Exceptions\BadCredentialsException; use RouterOS\Exceptions\ConfigException; use RouterOS\Interfaces\ClientInterface; use RouterOS\Interfaces\QueryInterface; @@ -57,6 +59,7 @@ class Client implements Interfaces\ClientInterface * @param bool $autoConnect If false it will skip auto-connect stage if not need to instantiate connection * * @throws \RouterOS\Exceptions\ClientException + * @throws \RouterOS\Exceptions\ConnectException * @throws \RouterOS\Exceptions\ConfigException * @throws \RouterOS\Exceptions\QueryException */ @@ -82,7 +85,7 @@ class Client implements Interfaces\ClientInterface // Throw error if cannot to connect if (false === $this->connect()) { - throw new ClientException('Unable to connect to ' . $config->get('host') . ':' . $config->get('port')); + throw new ConnectException('Unable to connect to ' . $config->get('host') . ':' . $config->get('port')); } } @@ -445,6 +448,7 @@ class Client implements Interfaces\ClientInterface * * @return bool * @throws \RouterOS\Exceptions\ClientException + * @throws \RouterOS\Exceptions\BadCredentialsException * @throws \RouterOS\Exceptions\ConfigException * @throws \RouterOS\Exceptions\QueryException */ @@ -487,7 +491,7 @@ class Client implements Interfaces\ClientInterface // If RouterOS answered with invalid credentials then throw error if (!empty($response[0]) && '!trap' === $response[0]) { - throw new ClientException('Invalid user name or password'); + throw new BadCredentialsException('Invalid user name or password'); } // Return true if we have only one line from server and this line is !done diff --git a/src/Exceptions/BadCredentialsException.php b/src/Exceptions/BadCredentialsException.php new file mode 100644 index 0000000..e393ec4 --- /dev/null +++ b/src/Exceptions/BadCredentialsException.php @@ -0,0 +1,13 @@ +socket_err_str); + throw new ConnectException('Unable to establish socket session, ' . $this->socket_err_str); } //Timeout read diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 77682c3..78dbb42 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -10,6 +10,8 @@ use RouterOS\Exceptions\QueryException; use RouterOS\Query; use RouterOS\Config; use RouterOS\Exceptions\ClientException; +use RouterOS\Exceptions\ConnectException; +use RouterOS\Exceptions\BadCredentialsException; class ClientTest extends TestCase { @@ -109,7 +111,7 @@ class ClientTest extends TestCase public function testConstructExceptionBadHost(): void { - $this->expectException(ClientException::class); + $this->expectException(ConnectException::class); new Client([ 'host' => '127.0.0.1', @@ -159,7 +161,7 @@ class ClientTest extends TestCase public function testConstructWrongPass(): void { - $this->expectException(ClientException::class); + $this->expectException(BadCredentialsException::class); new Client([ 'user' => $this->config['user'], @@ -171,7 +173,7 @@ class ClientTest extends TestCase public function testConstructWrongNet(): void { - $this->expectException(ClientException::class); + $this->expectException(ConnectException::class); new Client([ 'user' => $this->config['user'], From 449db20846550d05d6553d9a2edcc4c6f4deb1f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Proch=C3=A1zka?= Date: Mon, 28 Dec 2020 12:55:14 +0100 Subject: [PATCH 2/5] adding missing throws exception in phpdoc --- src/Client.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Client.php b/src/Client.php index e49f0ce..23f7896 100644 --- a/src/Client.php +++ b/src/Client.php @@ -60,6 +60,7 @@ class Client implements Interfaces\ClientInterface * * @throws \RouterOS\Exceptions\ClientException * @throws \RouterOS\Exceptions\ConnectException + * @throws \RouterOS\Exceptions\BadCredentialsException * @throws \RouterOS\Exceptions\ConfigException * @throws \RouterOS\Exceptions\QueryException */ From eccf794c2f2c1255e754c9bc3be8102c43921c55 Mon Sep 17 00:00:00 2001 From: Paul Rock Date: Tue, 29 Dec 2020 01:07:02 +0300 Subject: [PATCH 3/5] Description of new exceptions fixed --- src/Exceptions/BadCredentialsException.php | 6 +++--- src/Exceptions/ConnectException.php | 5 +++-- src/Exceptions/StreamException.php | 1 - 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Exceptions/BadCredentialsException.php b/src/Exceptions/BadCredentialsException.php index e393ec4..1bc958b 100644 --- a/src/Exceptions/BadCredentialsException.php +++ b/src/Exceptions/BadCredentialsException.php @@ -3,10 +3,10 @@ namespace RouterOS\Exceptions; /** - * Class ClientException - * Exception thrown when a password or login is wrong. + * Class BadCredentialsException thrown when a password or login is wrong. + * * @package RouterOS\Exceptions - * @since 0.4 + * @since 1.3.3 */ class BadCredentialsException extends ClientException { diff --git a/src/Exceptions/ConnectException.php b/src/Exceptions/ConnectException.php index 15fea9e..1e7acd8 100644 --- a/src/Exceptions/ConnectException.php +++ b/src/Exceptions/ConnectException.php @@ -3,9 +3,10 @@ namespace RouterOS\Exceptions; /** - * Class ClientException - * Exception thrown when a connection cannot be established. + * Class ConnectException thrown when a connection cannot be established. + * * @package RouterOS\Exceptions + * @since 1.3.3 */ class ConnectException extends ClientException { diff --git a/src/Exceptions/StreamException.php b/src/Exceptions/StreamException.php index c7dca3e..0105db8 100644 --- a/src/Exceptions/StreamException.php +++ b/src/Exceptions/StreamException.php @@ -8,7 +8,6 @@ namespace RouterOS\Exceptions; * @package RouterOS\Exceptions * @since 0.9 */ - class StreamException extends \Exception { } From 061699aa81fa712c7422febaf85750e75cf30979 Mon Sep 17 00:00:00 2001 From: Paul Rock Date: Tue, 29 Dec 2020 01:07:30 +0300 Subject: [PATCH 4/5] Non-used exception removed --- src/SocketTrait.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/SocketTrait.php b/src/SocketTrait.php index 19a00c0..3486256 100644 --- a/src/SocketTrait.php +++ b/src/SocketTrait.php @@ -2,7 +2,6 @@ namespace RouterOS; -use RouterOS\Exceptions\ClientException; use RouterOS\Exceptions\ConnectException; trait SocketTrait From 64ef425bcb597c4db5152e513e8dd041d30fad45 Mon Sep 17 00:00:00 2001 From: Paul Rock Date: Tue, 29 Dec 2020 01:07:59 +0300 Subject: [PATCH 5/5] Few non-important typos fixed --- src/Config.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Config.php b/src/Config.php index eae1b35..46c18be 100644 --- a/src/Config.php +++ b/src/Config.php @@ -184,15 +184,15 @@ class Config implements ConfigInterface * * @throws \RouterOS\Exceptions\ConfigException when parameter is not allowed */ - public function delete(string $name): ConfigInterface + public function delete(string $parameter): ConfigInterface { // Check of key in array - if (ArrayHelper::checkIfKeyNotExist($name, self::ALLOWED)) { - throw new ConfigException("Requested parameter '$name' not found in list [" . implode(',', array_keys(self::ALLOWED)) . ']'); + if (ArrayHelper::checkIfKeyNotExist($parameter, self::ALLOWED)) { + throw new ConfigException("Requested parameter '$parameter' not found in list [" . implode(',', array_keys(self::ALLOWED)) . ']'); } // Save value to array - unset($this->_parameters[$name]); + unset($this->_parameters[$parameter]); return $this; } @@ -202,14 +202,14 @@ class Config implements ConfigInterface * * @throws \RouterOS\Exceptions\ConfigException when parameter is not allowed */ - public function get(string $name) + public function get(string $parameter) { // Check of key in array - if (ArrayHelper::checkIfKeyNotExist($name, self::ALLOWED)) { - throw new ConfigException("Requested parameter '$name' not found in list [" . implode(',', array_keys(self::ALLOWED)) . ']'); + if (ArrayHelper::checkIfKeyNotExist($parameter, self::ALLOWED)) { + throw new ConfigException("Requested parameter '$parameter' not found in list [" . implode(',', array_keys(self::ALLOWED)) . ']'); } - return $this->getPort($name) ?? $this->_parameters[$name]; + return $this->getPort($parameter) ?? $this->_parameters[$parameter]; } /**