From de69b8573efcb3824b0b938e816095ad7944ecb1 Mon Sep 17 00:00:00 2001 From: Karim Elshendy Date: Wed, 7 Oct 2015 15:20:40 +0200 Subject: [PATCH 1/2] #47 Ignore IPs for http exceptions --- DependencyInjection/Configuration.php | 5 +++++ Listener/Notifier.php | 4 +++- README.md | 11 +++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 63eccc7..7f15a30 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -71,6 +71,11 @@ public function getConfigTreeBuilder() ->end() ->treatNullLike(array()) ->end() + ->arrayNode('ignoredIPs') + ->prototype('scalar') + ->end() + ->treatNullLike(array()) + ->end() ->end(); return $treeBuilder; diff --git a/Listener/Notifier.php b/Listener/Notifier.php index d63eb07..4fcc53a 100644 --- a/Listener/Notifier.php +++ b/Listener/Notifier.php @@ -46,6 +46,7 @@ class Notifier private $reportErrors = false; private $reportSilent = false; private $repeatTimeout = false; + private $ignoredIPs; private $command; private $commandInput; @@ -74,6 +75,7 @@ public function __construct(Swift_Mailer $mailer, EngineInterface $templating, $ $this->ignoredPhpErrors = $config['ignoredPhpErrors']; $this->repeatTimeout = $config['repeatTimeout']; $this->errorsDir = $cacheDir.'/errors'; + $this->ignoredIPs = $config['ignoredIPs']; if (!is_dir($this->errorsDir)) { mkdir($this->errorsDir); @@ -94,7 +96,7 @@ public function onKernelException(GetResponseForExceptionEvent $event) $exception = $event->getException(); if ($exception instanceof HttpException) { - if (500 === $exception->getStatusCode() || (404 === $exception->getStatusCode() && true === $this->handle404) || (in_array($exception->getStatusCode(), $this->handleHTTPcodes))) { + if (!in_array($event->getRequest()->getClientIp(), $this->ignoredIPs) && (500 === $exception->getStatusCode() || (404 === $exception->getStatusCode() && true === $this->handle404) || (in_array($exception->getStatusCode(), $this->handleHTTPcodes)))) { $this->createMailAndSend($exception, $event->getRequest()); } } else { diff --git a/README.md b/README.md index 124e2ec..bae9f03 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,17 @@ You may control the depth of recursion with a parameter, say foo = array('a'=>ar Default value is 1. (MAX_DEPTH const) +### How to ignore sending HTTP errors if request comes from given IPs ? + +If you want to ignore sending HTTP errors if the request comes from specific IPs, you can now specify the list of ignored IPs. + +```yml +# app/config/config_prod.yml +elao_error_notifier: + ignoredIPs: + - "178.63.45.100" + - ... +``` ## Screenshot From 01828a8497f985189f12dcb061cbc6a2936fd1ac Mon Sep 17 00:00:00 2001 From: Karim Elshendy Date: Wed, 7 Oct 2015 16:00:14 +0200 Subject: [PATCH 2/2] change ignore ips condition place for better readability --- Listener/Notifier.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Listener/Notifier.php b/Listener/Notifier.php index 4fcc53a..0b72c4f 100644 --- a/Listener/Notifier.php +++ b/Listener/Notifier.php @@ -96,7 +96,11 @@ public function onKernelException(GetResponseForExceptionEvent $event) $exception = $event->getException(); if ($exception instanceof HttpException) { - if (!in_array($event->getRequest()->getClientIp(), $this->ignoredIPs) && (500 === $exception->getStatusCode() || (404 === $exception->getStatusCode() && true === $this->handle404) || (in_array($exception->getStatusCode(), $this->handleHTTPcodes)))) { + if (in_array($event->getRequest()->getClientIp(), $this->ignoredIPs)) { + return; + } + + if (500 === $exception->getStatusCode() || (404 === $exception->getStatusCode() && true === $this->handle404) || (in_array($exception->getStatusCode(), $this->handleHTTPcodes))) { $this->createMailAndSend($exception, $event->getRequest()); } } else {