diff --git a/src/IOException.php b/src/IOException.php new file mode 100644 index 0000000..53e73a9 --- /dev/null +++ b/src/IOException.php @@ -0,0 +1,11 @@ +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; diff --git a/tests/ModbusMaster/ModbusExceptionTest.php b/tests/ModbusMaster/ModbusExceptionTest.php index 0d31651..f1b04fc 100644 --- a/tests/ModbusMaster/ModbusExceptionTest.php +++ b/tests/ModbusMaster/ModbusExceptionTest.php @@ -1,6 +1,9 @@ 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]