Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 5b4a973

Browse files
Readability improvement of Connection::connect
- Added a private isErrorResponse method to check if response is of -ERR - Created some Exception factory methods to centralise/standardise Exceptions and their messages Fixes repejota#57
1 parent d1fc1da commit 5b4a973

3 files changed

Lines changed: 74 additions & 10 deletions

File tree

src/Nats/Connection.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ private function getStream($address, $timeout)
202202
stream_set_timeout($fp, $seconds, $microseconds);
203203

204204
if (!$fp) {
205-
throw new \Exception($errstr, $errno);
205+
throw Exception::forStreamSocketClientError($errstr, $errno);
206206
}
207207

208208
return $fp;
@@ -239,16 +239,16 @@ public function connect($timeout = null)
239239
$msg = 'CONNECT '.$this->options;
240240
$this->send($msg);
241241
$connect_response = $this->receive();
242-
if (strpos($connect_response, '-ERR')!== false) {
243-
throw new \Exception("Failing connection: $connect_response");
242+
243+
if ($this->isErrorResponse($connect_response)) {
244+
throw Exception::forFailedConnection($connect_response);
244245
}
245246

246247
$this->ping();
247248
$ping_response = $this->receive();
248-
if ($ping_response !== "PONG") {
249-
if (strpos($ping_response, '-ERR')!== false) {
250-
throw new \Exception("Failing on first ping: $ping_response");
251-
}
249+
250+
if ($this->isErrorResponse($ping_response)) {
251+
throw Exception::forFailedPing($ping_response);
252252
}
253253
}
254254

@@ -391,14 +391,14 @@ private function handleMSG($line)
391391
$msg = new Message($subject, $payload, $sid, $this);
392392

393393
if (!isset($this->subscriptions[$sid])) {
394-
throw new Exception('subscription not found');
394+
throw Exception::forSubscriptionNotFound($sid);
395395
}
396396

397397
$func = $this->subscriptions[$sid];
398398
if (is_callable($func)) {
399399
$func($msg);
400400
} else {
401-
throw new Exception('not callable');
401+
throw Exception::forSubscriptionCallbackInvalid($sid);
402402
}
403403

404404
return;
@@ -504,4 +504,15 @@ public function streamSocket()
504504
{
505505
return $this->streamSocket;
506506
}
507+
508+
/**
509+
* Indicates whether $response is an error response.
510+
*
511+
* @param string $response The Nats Server response.
512+
* @return boolean
513+
*/
514+
private function isErrorResponse($response)
515+
{
516+
return false !== strpos('-ERR', $response);
517+
}
507518
}

src/Nats/Exception.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,59 @@
66
*/
77
class Exception extends \Exception
88
{
9+
/**
10+
* Creates an Exception for a failed connection.
11+
*
12+
* @param string $response The failed error response.
13+
* @return Nats\Exception
14+
*/
15+
public static function forFailedConnection($response)
16+
{
17+
return new static(sprintf('Failed to connect: %s', $response));
18+
}
919

20+
/**
21+
* Creates an Exception for a failed PING response.
22+
*
23+
* @param string $response The failed PING response.
24+
* @return Nats\Exception
25+
*/
26+
public static function forFailedPing($response)
27+
{
28+
return new static(sprintf('Failed to ping: %s', $response));
29+
}
30+
31+
/**
32+
* Creates an Exception for an invalid Subscription Identifier (sid).
33+
*
34+
* @param string $subscription The Subscription Identifier (sid).
35+
* @return Nats\Exception
36+
*/
37+
public static function forSubscriptionNotFound($subscription)
38+
{
39+
return new static(sprintf('Subscription not found: %s', $subscription));
40+
}
41+
42+
/**
43+
* Creates an Exception for an invalid Subscription Identifier (sid) callback.
44+
*
45+
* @param string $subscription The Subscription Identifier (sid).
46+
* @return Nats\Exception
47+
*/
48+
public static function forSubscriptionCallbackInvalid($subscription)
49+
{
50+
return new static(sprintf('Subscription callback is invalid: %s', $subscription));
51+
}
52+
53+
/**
54+
* Creates an Exception for the failed creation of a Stream Socket Client.
55+
*
56+
* @param string $message The system level error message.
57+
* @param integer $code The system level error code.
58+
* @return Nats\Exception
59+
*/
60+
public static function forStreamSocketClientError($message, $code)
61+
{
62+
return new static(sprintf('A Stream Socket Client could not be created: (%d) %s', $code, $message), $code);
63+
}
1064
}

test/ConnectionTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ function ($message) use ($contentLen) {
158158

159159
$i++;
160160
} while ($i < 100);
161-
162161
}
163162

164163
/**

0 commit comments

Comments
 (0)