Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/Monitors/HttpPingMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use EricMakesStuff\ServerMonitor\Events\HttpPingUp;
use EricMakesStuff\ServerMonitor\Exceptions\InvalidConfiguration;
use GuzzleHttp\Client as Guzzle;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;

class HttpPingMonitor extends BaseMonitor
{
Expand Down Expand Up @@ -71,10 +71,18 @@ public function runMonitor()
$response = $guzzle->get($this->url);
$this->responseCode = $response->getStatusCode();
$this->responseContent = (string)$response->getBody();
} catch (ClientException $e) {
} catch (RequestException $e) {
$response = $e->getResponse();
$this->responseCode = $response->getStatusCode();
} catch (ConnectException $e) {

if ($response instanceof ResponseInterface) {
$this->responseCode = $response->getStatusCode();
$this->responseContent = (string)$response->getBody();
} else {
$this->setResponseCodeAndContentOnException($e);
}

} catch (\Exception $e) {
$this->setResponseCodeAndContentOnException($e);
}

if ($this->responseCode != '200'
Expand All @@ -85,6 +93,15 @@ public function runMonitor()
}
}

/**
* @param \Exception $e
*/
protected function setResponseCodeAndContentOnException(\Exception $e)
{
$this->responseCode = null;
$this->responseContent = $e->getMessage() . PHP_EOL . $e->getTraceAsString();
}

protected function checkResponseContains($html, $phrase)
{
if (!$phrase) {
Expand Down