diff --git a/Listener/Notifier.php b/Listener/Notifier.php index 2285e1c..433fe43 100644 --- a/Listener/Notifier.php +++ b/Listener/Notifier.php @@ -3,11 +3,16 @@ namespace Elao\ErrorNotifierBundle\Listener; use \Swift_Mailer; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Templating\EngineInterface; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\Console\Event\ConsoleExceptionEvent; +use Symfony\Component\Console\Event\ConsoleCommandEvent; use Symfony\Component\HttpKernel\Exception\FlattenException; /** @@ -39,6 +44,8 @@ class Notifier private $reportWarnings = false; private $reportErrors = false; private $repeatTimeout = false; + private $command; + private $commandInput; private static $tmpBuffer = null; @@ -94,6 +101,22 @@ public function onKernelException(GetResponseForExceptionEvent $event) } } + /** + * Handle the event + * + * @param ConsoleExceptionEvent $event event + */ + public function onConsoleException(ConsoleExceptionEvent $event) + { + $exception = $event->getException(); + + $sendMail = !in_array(get_class($exception), $this->ignoredClasses); + + if ($sendMail === true) { + $this->createMailAndSend($exception, null, null, $this->command, $this->commandInput); + } + } + /** * Once we have the request we can use it to show debug details in the email * @@ -111,18 +134,40 @@ public function onKernelRequest(GetResponseEvent $event) $this->request = $event->getRequest(); - // set_error_handler and register_shutdown_function can be triggered on - // both warnings and errors - set_error_handler(array($this, 'handlePhpError'), E_ALL); + $this->setErrorHandlers(); + } + } + + /** + * @param ConsoleCommandEvent $event + */ + public function onConsoleCommand(ConsoleCommandEvent $event) + { + $this->request = null; - // From PHP Documentation: the following error types cannot be handled with - // a user defined function using set_error_handler: *E_ERROR*, *E_PARSE*, *E_CORE_ERROR*, *E_CORE_WARNING*, - // *E_COMPILE_ERROR*, *E_COMPILE_WARNING* - // That is we need to use also register_shutdown_function() - register_shutdown_function(array($this, 'handlePhpFatalErrorAndWarnings')); + $this->command = $event->getCommand(); + $this->commandInput = $event->getInput(); + + if ($this->reportErrors || $this->reportWarnings) { + self::_reserveMemory(); + + $this->setErrorHandlers(); } } + protected function setErrorHandlers() + { + // set_error_handler and register_shutdown_function can be triggered on + // both warnings and errors + set_error_handler(array($this, 'handlePhpError'), E_ALL); + + // From PHP Documentation: the following error types cannot be handled with + // a user defined function using set_error_handler: *E_ERROR*, *E_PARSE*, *E_CORE_ERROR*, *E_CORE_WARNING*, + // *E_COMPILE_ERROR*, *E_COMPILE_WARNING* + // That is we need to use also register_shutdown_function() + register_shutdown_function(array($this, 'handlePhpFatalErrorAndWarnings')); + } + /** * @see http://php.net/set_error_handler * @@ -145,7 +190,7 @@ public function handlePhpError($level, $message, $file, $line, $errcontext) $exception = new \ErrorException(sprintf('%s: %s in %s line %d', $this->getErrorString($level), $message, $file, $line), 0, $level, $file, $line); - $this->createMailAndSend($exception, $this->request, $errcontext); + $this->createMailAndSend($exception, $this->request, $errcontext, $this->command, $this->commandInput); // in order not to bypass the standard PHP error handler return false; @@ -177,7 +222,7 @@ public function handlePhpFatalErrorAndWarnings() if (in_array($lastError['type'], $errors)) { $exception = new \ErrorException(sprintf('%s: %s in %s line %d', @$this->getErrorString(@$lastError['type']), @$lastError['message'], @$lastError['file'], @$lastError['line']), @$lastError['type'], @$lastError['type'], @$lastError['file'], @$lastError['line']); - $this->createMailAndSend($exception, $this->request); + $this->createMailAndSend($exception, $this->request, null, $this->command, $this->commandInput); } } @@ -215,8 +260,10 @@ public function getErrorString($errorNo) * @param ErrorException $exception * @param Request $request * @param array $context + * @param Command $command + * @param InputInterface $commandInput */ - public function createMailAndSend($exception, $request, $context = null) + public function createMailAndSend($exception, Request $request = null, $context = null, Command $command = null, InputInterface $commandInput = null) { if (!$exception instanceof FlattenException) { $exception = FlattenException::create($exception); @@ -229,10 +276,18 @@ public function createMailAndSend($exception, $request, $context = null) 'exception' => $exception, 'request' => $request, 'status_code' => $exception->getCode(), - 'context' => $context + 'context' => $context, + 'command' => $command, + 'command_input' => $commandInput )); - $subject = '[' . $request->headers->get('host') . '] Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage(); + if($this->request) { + $subject = '[' . $request->headers->get('host') . '] Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage(); + } elseif($this->command) { + $subject = '[' . $this->command->getName() . '] Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage(); + } else { + $subject = 'Error ' . $exception->getStatusCode() . ': ' . $exception->getMessage(); + } if (function_exists('mb_substr')) { $subject = mb_substr($subject, 0, 255); diff --git a/Resources/config/services.xml b/Resources/config/services.xml old mode 100644 new mode 100755 index 36e9c1c..3e2ffbe --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -12,6 +12,8 @@ + + %kernel.cache_dir% diff --git a/Resources/views/mail.html.twig b/Resources/views/mail.html.twig old mode 100644 new mode 100755 index f32fe05..d36ce13 --- a/Resources/views/mail.html.twig +++ b/Resources/views/mail.html.twig @@ -326,7 +326,10 @@ Generated at: {{ "now"|date("d-m-Y H:i:s") }}
Class name: {{ exception.class }}
Message: {{ exception.message }}
- Uri: {{ request.uri }}
+ {% if request %}Uri: {{ request.uri }}
{% endif %} + {% if command %} + Command: {{ command.name }}
+ {% endif %} @@ -381,6 +384,59 @@ + {% if command_input %} + {% if command_input.options|length %} + {#-- Command Options Block --#} + + + + +
+

Command Options

+ + + + + + {% for key, value in command_input.options %} + + + + + {% endfor %} +
VariableValue
{{ key }} =
{{ value|dumpy(2) }}
+
+ {% endif %} + {% if command_input.arguments|length %} + {#-- Command Arguments Block --#} + + + + +
+ +

Command Arguments

+ + + + + + + {% for key, value in command_input.arguments %} + + + + + {% endfor %} +
VariableValue
{{ key }} =
{{ value|dumpy(2) }}
+
+ {% endif %} + + + + +
+ {% endif %} {% if context is defined %} {#-- Variables Parameters Block --#} @@ -416,6 +472,7 @@
{% endif %} + {% if request %} {#-- Request GET Parameters Block --#} @@ -574,6 +631,7 @@
+ {% endif %}