refactor thrown exceptions to be more specific than just Exception class

pull/2/head
toimtoimtoim 8 years ago
parent 1e1c8ee5ce
commit 8637a19572
  1. 11
      src/IOException.php
  2. 10
      src/ModbusException.php
  3. 11
      src/ModbusMaster.php
  4. 11
      tests/ModbusMaster/ModbusExceptionTest.php

@ -0,0 +1,11 @@
<?php
namespace PHPModbus;
/**
* Exception class thrown when a socket operation failure happens.
*/
class IOException extends \RuntimeException
{
}

@ -0,0 +1,10 @@
<?php
namespace PHPModbus;
/**
* Exception class thrown when a server sends packet with error code. Something is wrong with request.
*/
class ModbusException extends \RuntimeException
{
}

@ -3,6 +3,7 @@
namespace PHPModbus;
use Exception;
use InvalidArgumentException;
/**
* Phpmodbus Copyright (c) 2004, 2013 Jan Krakora
@ -193,13 +194,13 @@ class ModbusMaster
// UDP socket
$this->sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
} else {
throw new Exception("Unknown socket protocol, should be 'TCP' or 'UDP'");
throw new InvalidArgumentException("Unknown socket protocol, should be 'TCP' or 'UDP'");
}
// Bind the client socket to a specific local port
if (strlen($this->client) > 0) {
$result = socket_bind($this->sock, $this->client, $this->client_port);
if ($result === false) {
throw new Exception(
throw new IOException(
"socket_bind() failed. Reason: ($result)" .
socket_strerror(socket_last_error($this->sock))
);
@ -215,7 +216,7 @@ class ModbusMaster
// Connect the socket
$result = @socket_connect($this->sock, $this->host, $this->port);
if ($result === false) {
throw new Exception(
throw new IOException(
"socket_connect() failed. Reason: ($result)" .
socket_strerror(socket_last_error($this->sock))
);
@ -349,7 +350,7 @@ class ModbusMaster
} else {
$timeSpentWaiting = microtime(true) - $lastAccess;
if ($timeSpentWaiting >= $totalReadTimeout) {
throw new Exception(
throw new IOException(
"Watchdog time expired [ $totalReadTimeout sec ]!!! " .
"Connection to $this->host:$this->port is not established."
);
@ -435,7 +436,7 @@ class ModbusMaster
$failure_str = 'UNDEFINED FAILURE CODE';
}
// exception response
throw new Exception("Modbus response error code: $failure_code ($failure_str)");
throw new ModbusException("Modbus response error code: $failure_code ($failure_str)");
} else {
$this->status .= "Modbus response error code: NOERROR\n";
return true;

@ -1,6 +1,9 @@
<?php
namespace Tests\ModbusMaster;
use InvalidArgumentException;
use PHPModbus\IOException;
use PHPModbus\ModbusException;
use PHPModbus\ModbusMaster;
use PHPModbus\ModbusMasterTcp;
@ -8,7 +11,7 @@ class ModbusExceptionTest extends MockServerTestCase
{
public function testThrowProtocolMismatchException()
{
$this->expectException(\Exception::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Unknown socket protocol, should be 'TCP' or 'UDP'");
$modbus = new ModbusMaster('127.0.0.1', 'Mismatch');
@ -17,7 +20,7 @@ class ModbusExceptionTest extends MockServerTestCase
public function testPortClosedException()
{
$this->expectException(\Exception::class);
$this->expectException(IOException::class);
$this->expectExceptionMessage('socket_connect() failed. Reason: ()No connection could be made because the target machine actively refused it.');
$modbus = new ModbusMasterTcp('127.0.0.1');
@ -27,7 +30,7 @@ class ModbusExceptionTest extends MockServerTestCase
public function testTimeoutException()
{
$this->expectException(\Exception::class);
$this->expectException(\RuntimeException::class);
$mockResponse = '89130000000400010101'; // respond with 1 byte (00000001 bits set) [1]
static::executeWithMockServer($mockResponse, function ($port) {
@ -45,7 +48,7 @@ class ModbusExceptionTest extends MockServerTestCase
public function testThrowIllegalDataValueException()
{
$this->expectException(\Exception::class);
$this->expectException(ModbusException::class);
$this->expectExceptionMessage('Modbus response error code: 3 (ILLEGAL DATA VALUE)');
$mockResponse = 'da8700000003008303'; // respond with 1 WORD (2 bytes) [0, 3]

Loading…
Cancel
Save