Skip to content

Commit f65d346

Browse files
webvimarkeblount
authored andcommitted
prevent HttpPing from crashing on GuzzleHttp\Exception\ServerException (#12)
* prevent command from crashing on GuzzleHttp\Exception\ServerException 5xx errors * if http ping failed with timeout after successful one, it should have 500 status code * check that response is not null on RequestException * set response code to NULL on exception
1 parent 2c0bc36 commit f65d346

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/Monitors/HttpPingMonitor.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
use EricMakesStuff\ServerMonitor\Events\HttpPingUp;
77
use EricMakesStuff\ServerMonitor\Exceptions\InvalidConfiguration;
88
use GuzzleHttp\Client as Guzzle;
9-
use GuzzleHttp\Exception\ClientException;
10-
use GuzzleHttp\Exception\ConnectException;
9+
use GuzzleHttp\Exception\RequestException;
10+
use Psr\Http\Message\ResponseInterface;
1111

1212
class HttpPingMonitor extends BaseMonitor
1313
{
@@ -71,10 +71,18 @@ public function runMonitor()
7171
$response = $guzzle->get($this->url);
7272
$this->responseCode = $response->getStatusCode();
7373
$this->responseContent = (string)$response->getBody();
74-
} catch (ClientException $e) {
74+
} catch (RequestException $e) {
7575
$response = $e->getResponse();
76-
$this->responseCode = $response->getStatusCode();
77-
} catch (ConnectException $e) {
76+
77+
if ($response instanceof ResponseInterface) {
78+
$this->responseCode = $response->getStatusCode();
79+
$this->responseContent = (string)$response->getBody();
80+
} else {
81+
$this->setResponseCodeAndContentOnException($e);
82+
}
83+
84+
} catch (\Exception $e) {
85+
$this->setResponseCodeAndContentOnException($e);
7886
}
7987

8088
if ($this->responseCode != '200'
@@ -85,6 +93,15 @@ public function runMonitor()
8593
}
8694
}
8795

96+
/**
97+
* @param \Exception $e
98+
*/
99+
protected function setResponseCodeAndContentOnException(\Exception $e)
100+
{
101+
$this->responseCode = null;
102+
$this->responseContent = $e->getMessage() . PHP_EOL . $e->getTraceAsString();
103+
}
104+
88105
protected function checkResponseContains($html, $phrase)
89106
{
90107
if (!$phrase) {

0 commit comments

Comments
 (0)