Browse Source

Adding more detail ClientExceptions for credential and connection error

pull/50/head
Michal Procházka 5 years ago
parent
commit
cf721efad2
  1. 8
      src/Client.php
  2. 13
      src/Exceptions/BadCredentialsException.php
  3. 12
      src/Exceptions/ConnectException.php
  4. 4
      src/SocketTrait.php
  5. 8
      tests/ClientTest.php

8
src/Client.php

@ -4,6 +4,8 @@ namespace RouterOS;
use DivineOmega\SSHConnection\SSHConnection; use DivineOmega\SSHConnection\SSHConnection;
use RouterOS\Exceptions\ClientException; use RouterOS\Exceptions\ClientException;
use RouterOS\Exceptions\ConnectException;
use RouterOS\Exceptions\BadCredentialsException;
use RouterOS\Exceptions\ConfigException; use RouterOS\Exceptions\ConfigException;
use RouterOS\Interfaces\ClientInterface; use RouterOS\Interfaces\ClientInterface;
use RouterOS\Interfaces\QueryInterface; 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 * @param bool $autoConnect If false it will skip auto-connect stage if not need to instantiate connection
* *
* @throws \RouterOS\Exceptions\ClientException * @throws \RouterOS\Exceptions\ClientException
* @throws \RouterOS\Exceptions\ConnectException
* @throws \RouterOS\Exceptions\ConfigException * @throws \RouterOS\Exceptions\ConfigException
* @throws \RouterOS\Exceptions\QueryException * @throws \RouterOS\Exceptions\QueryException
*/ */
@ -82,7 +85,7 @@ class Client implements Interfaces\ClientInterface
// Throw error if cannot to connect // Throw error if cannot to connect
if (false === $this->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 * @return bool
* @throws \RouterOS\Exceptions\ClientException * @throws \RouterOS\Exceptions\ClientException
* @throws \RouterOS\Exceptions\BadCredentialsException
* @throws \RouterOS\Exceptions\ConfigException * @throws \RouterOS\Exceptions\ConfigException
* @throws \RouterOS\Exceptions\QueryException * @throws \RouterOS\Exceptions\QueryException
*/ */
@ -487,7 +491,7 @@ class Client implements Interfaces\ClientInterface
// If RouterOS answered with invalid credentials then throw error // If RouterOS answered with invalid credentials then throw error
if (!empty($response[0]) && '!trap' === $response[0]) { 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 // Return true if we have only one line from server and this line is !done

13
src/Exceptions/BadCredentialsException.php

@ -0,0 +1,13 @@
<?php
namespace RouterOS\Exceptions;
/**
* Class ClientException
* Exception thrown when a password or login is wrong.
* @package RouterOS\Exceptions
* @since 0.4
*/
class BadCredentialsException extends ClientException
{
}

12
src/Exceptions/ConnectException.php

@ -0,0 +1,12 @@
<?php
namespace RouterOS\Exceptions;
/**
* Class ClientException
* Exception thrown when a connection cannot be established.
* @package RouterOS\Exceptions
*/
class ConnectException extends ClientException
{
}

4
src/SocketTrait.php

@ -3,6 +3,7 @@
namespace RouterOS; namespace RouterOS;
use RouterOS\Exceptions\ClientException; use RouterOS\Exceptions\ClientException;
use RouterOS\Exceptions\ConnectException;
trait SocketTrait trait SocketTrait
{ {
@ -32,6 +33,7 @@ trait SocketTrait
* *
* @return void * @return void
* @throws \RouterOS\Exceptions\ClientException * @throws \RouterOS\Exceptions\ClientException
* @throws \RouterOS\Exceptions\ConnectException
* @throws \RouterOS\Exceptions\ConfigException * @throws \RouterOS\Exceptions\ConfigException
*/ */
private function openSocket(): void private function openSocket(): void
@ -56,7 +58,7 @@ trait SocketTrait
// Throw error is socket is not initiated // Throw error is socket is not initiated
if (false === $socket) { if (false === $socket) {
throw new ClientException('Unable to establish socket session, ' . $this->socket_err_str);
throw new ConnectException('Unable to establish socket session, ' . $this->socket_err_str);
} }
//Timeout read //Timeout read

8
tests/ClientTest.php

@ -10,6 +10,8 @@ use RouterOS\Exceptions\QueryException;
use RouterOS\Query; use RouterOS\Query;
use RouterOS\Config; use RouterOS\Config;
use RouterOS\Exceptions\ClientException; use RouterOS\Exceptions\ClientException;
use RouterOS\Exceptions\ConnectException;
use RouterOS\Exceptions\BadCredentialsException;
class ClientTest extends TestCase class ClientTest extends TestCase
{ {
@ -109,7 +111,7 @@ class ClientTest extends TestCase
public function testConstructExceptionBadHost(): void public function testConstructExceptionBadHost(): void
{ {
$this->expectException(ClientException::class);
$this->expectException(ConnectException::class);
new Client([ new Client([
'host' => '127.0.0.1', 'host' => '127.0.0.1',
@ -159,7 +161,7 @@ class ClientTest extends TestCase
public function testConstructWrongPass(): void public function testConstructWrongPass(): void
{ {
$this->expectException(ClientException::class);
$this->expectException(BadCredentialsException::class);
new Client([ new Client([
'user' => $this->config['user'], 'user' => $this->config['user'],
@ -171,7 +173,7 @@ class ClientTest extends TestCase
public function testConstructWrongNet(): void public function testConstructWrongNet(): void
{ {
$this->expectException(ClientException::class);
$this->expectException(ConnectException::class);
new Client([ new Client([
'user' => $this->config['user'], 'user' => $this->config['user'],

Loading…
Cancel
Save