Skip to content

Commit 7f49265

Browse files
committed
Added log messages for most thrown Exceptions
1 parent d19e579 commit 7f49265

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.1.1 (2020-06-29)
4+
5+
- Added log messages for most thrown Exceptions
6+
37
## 1.1.0 (2020-06-01)
48

59
- Added a client factory (copied from `Gos\Bundle\WebSocketBundle\Pusher\Wamp\WampConnectionFactoryInterface` and `Gos\Bundle\WebSocketBundle\Pusher\Wamp\WampConnectionFactory`) in the `gos/web-socket-bundle` package

src/Wamp/Client.php

+36
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ public function connect(string $target = '/'): string
9797
$socket = @stream_socket_client($this->endpoint, $errno, $errstr);
9898

9999
if (false === $socket) {
100+
if (null !== $this->logger) {
101+
$this->logger->error('Could not open socket.', ['errno' => $errno, 'errstr' => $errstr]);
102+
}
103+
100104
throw new BadResponseException('Could not open socket. Reason: '.$errstr, $errno);
101105
}
102106

@@ -112,6 +116,10 @@ public function connect(string $target = '/'): string
112116
}
113117

114118
if (Protocol::MSG_WELCOME !== $payload[0]) {
119+
if (null !== $this->logger) {
120+
$this->logger->error('WAMP Server did not send a welcome message.', ['payload' => $payload]);
121+
}
122+
115123
throw new BadResponseException('WAMP Server did not send a welcome message.');
116124
}
117125

@@ -130,6 +138,10 @@ private function upgradeProtocol(string $target)
130138
$key = $this->generateKey();
131139

132140
if (false === strpos($target, '/')) {
141+
if (null !== $this->logger) {
142+
$this->logger->error('Invalid target path for WAMP server.', ['target' => $target]);
143+
}
144+
133145
throw new WebsocketException('WAMP server target must contain a "/"');
134146
}
135147

@@ -159,12 +171,20 @@ private function upgradeProtocol(string $target)
159171
private function verifyResponse($response): void
160172
{
161173
if (false === $response) {
174+
if (null !== $this->logger) {
175+
$this->logger->error('WAMP Server did not respond properly');
176+
}
177+
162178
throw new BadResponseException('WAMP Server did not respond properly');
163179
}
164180

165181
$responseStatus = substr($response, 0, 12);
166182

167183
if ('HTTP/1.1 101' !== $responseStatus) {
184+
if (null !== $this->logger) {
185+
$this->logger->error('Unexpected HTTP response from WAMP server.', ['response' => $response]);
186+
}
187+
168188
throw new BadResponseException(sprintf('Unexpected response status. Expected "HTTP/1.1 101", got "%s".', $responseStatus));
169189
}
170190
}
@@ -181,13 +201,21 @@ private function read(): string
181201
$streamBody = stream_get_contents($this->socket, stream_get_meta_data($this->socket)['unread_bytes']);
182202

183203
if (false === $streamBody) {
204+
if (null !== $this->logger) {
205+
$this->logger->error('The stream buffer could not be read.', ['error' => error_get_last()]);
206+
}
207+
184208
throw new BadResponseException('The stream buffer could not be read.');
185209
}
186210

187211
$startPos = strpos($streamBody, '[');
188212
$endPos = strpos($streamBody, ']');
189213

190214
if (false === $startPos || false === $endPos) {
215+
if (null !== $this->logger) {
216+
$this->logger->error('Could not extract response body from stream.', ['body' => $streamBody]);
217+
}
218+
191219
throw new BadResponseException('Could not extract response body from stream.');
192220
}
193221

@@ -216,13 +244,21 @@ public function disconnect(): bool
216244
$firstByte = fread($this->socket, 1);
217245

218246
if (false === $firstByte) {
247+
if (null !== $this->logger) {
248+
$this->logger->error('Could not extract the payload from the buffer.', ['error' => error_get_last()]);
249+
}
250+
219251
throw new WebsocketException('Could not extract the payload from the buffer.');
220252
}
221253

222254
$payloadLength = \ord($firstByte);
223255
$payload = fread($this->socket, $payloadLength);
224256

225257
if (false === $payload) {
258+
if (null !== $this->logger) {
259+
$this->logger->error('Could not extract the payload from the buffer.', ['error' => error_get_last()]);
260+
}
261+
226262
throw new WebsocketException('Could not extract the payload from the buffer.');
227263
}
228264

0 commit comments

Comments
 (0)