From 49dcdb587bae326af23acfef173af423a0ab29b9 Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 9 Jan 2017 19:54:36 +0300 Subject: [PATCH 1/4] prevent command from crashing on GuzzleHttp\Exception\ServerException 5xx errors --- src/Monitors/HttpPingMonitor.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Monitors/HttpPingMonitor.php b/src/Monitors/HttpPingMonitor.php index 9526ef9..79991e7 100644 --- a/src/Monitors/HttpPingMonitor.php +++ b/src/Monitors/HttpPingMonitor.php @@ -6,8 +6,7 @@ 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; class HttpPingMonitor extends BaseMonitor { @@ -71,10 +70,12 @@ 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) { + $this->responseContent = (string)$response->getBody(); + } catch (\Exception $e) { + // To prevent command from crashing and let Notifier handle this } if ($this->responseCode != '200' From 3879d0eeed398723f2bf6f51e44d15a3de1d8a0b Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 9 Jan 2017 22:28:14 +0300 Subject: [PATCH 2/4] if http ping failed with timeout after successful one, it should have 500 status code --- src/Monitors/HttpPingMonitor.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Monitors/HttpPingMonitor.php b/src/Monitors/HttpPingMonitor.php index 79991e7..e26348a 100644 --- a/src/Monitors/HttpPingMonitor.php +++ b/src/Monitors/HttpPingMonitor.php @@ -76,6 +76,8 @@ public function runMonitor() $this->responseContent = (string)$response->getBody(); } catch (\Exception $e) { // To prevent command from crashing and let Notifier handle this + $this->responseCode = 500; + $this->responseContent = $e->getMessage() . PHP_EOL . $e->getTraceAsString(); } if ($this->responseCode != '200' From af505b2c6029d5c5c0569b4cd764185afe2ae035 Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 9 Jan 2017 22:40:42 +0300 Subject: [PATCH 3/4] check that response is not null on RequestException --- src/Monitors/HttpPingMonitor.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Monitors/HttpPingMonitor.php b/src/Monitors/HttpPingMonitor.php index e26348a..ebf7d2e 100644 --- a/src/Monitors/HttpPingMonitor.php +++ b/src/Monitors/HttpPingMonitor.php @@ -7,6 +7,7 @@ use EricMakesStuff\ServerMonitor\Exceptions\InvalidConfiguration; use GuzzleHttp\Client as Guzzle; use GuzzleHttp\Exception\RequestException; +use Psr\Http\Message\ResponseInterface; class HttpPingMonitor extends BaseMonitor { @@ -72,8 +73,15 @@ public function runMonitor() $this->responseContent = (string)$response->getBody(); } catch (RequestException $e) { $response = $e->getResponse(); - $this->responseCode = $response->getStatusCode(); - $this->responseContent = (string)$response->getBody(); + + if ($response instanceof ResponseInterface) { + $this->responseCode = $response->getStatusCode(); + $this->responseContent = (string)$response->getBody(); + } else { + $this->responseCode = 500; + $this->responseContent = $e->getMessage() . PHP_EOL . $e->getTraceAsString(); + } + } catch (\Exception $e) { // To prevent command from crashing and let Notifier handle this $this->responseCode = 500; From 1db8624a8e5afe8f5c90422a022ad1a2b9213b58 Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 9 Jan 2017 22:47:38 +0300 Subject: [PATCH 4/4] set response code to NULL on exception --- src/Monitors/HttpPingMonitor.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Monitors/HttpPingMonitor.php b/src/Monitors/HttpPingMonitor.php index ebf7d2e..1fad99a 100644 --- a/src/Monitors/HttpPingMonitor.php +++ b/src/Monitors/HttpPingMonitor.php @@ -78,14 +78,11 @@ public function runMonitor() $this->responseCode = $response->getStatusCode(); $this->responseContent = (string)$response->getBody(); } else { - $this->responseCode = 500; - $this->responseContent = $e->getMessage() . PHP_EOL . $e->getTraceAsString(); + $this->setResponseCodeAndContentOnException($e); } } catch (\Exception $e) { - // To prevent command from crashing and let Notifier handle this - $this->responseCode = 500; - $this->responseContent = $e->getMessage() . PHP_EOL . $e->getTraceAsString(); + $this->setResponseCodeAndContentOnException($e); } if ($this->responseCode != '200' @@ -96,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) {