/** * Intercept exception handling to send a mail before continuing with the default logic * @see \Cake\Error\BaseErrorHandler::handleException() */ public function handleException(Exception $exception) { // send a debug mail with the fatal exception if ($this->email && !in_array(get_class($exception), $this->_skipExceptions)) { $this->email->debug('Application exception', Misc::dump($exception, $exception->getMessage(), true)); } // continue with exception handle logic parent::handleException($exception); }
/** * Send debug emails * * @param string $subject * @param string $body * @param bool $request * @param bool $server */ public function debug($subject = null, $body = null, $request = true, $server = true) { // initialize class $email = new Email('debug'); // get controller and method $location = []; foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $one) { // ignore current function if ($one['function'] == __FUNCTION__) { continue; } // check if private or protected $check = new \ReflectionMethod($one['class'], $one['function']); if ($check->isPrivate() || $check->isProtected()) { $location[3] = $one['function']; continue; } // set class and function $location[1] = str_replace(['Controller', 'App\\\\'], null, $one['class']); $location[2] = $one['function']; break; } // sort before using ksort($location); // overwrite locations if (isset($location[1])) { if ($location[1] == 'Unimatrix\\Utility\\Console\\EmailConsoleErrorHandler') { $location = ['EmailConsoleErrorHandler']; } elseif ($location[1] == 'Unimatrix\\Utility\\Error\\EmailErrorHandler') { $location = ['EmailErrorHandler']; } } // brand $from = $email->from(); $brand = reset($from); // append brand to subject $subject = $brand . ' report: [' . implode('->', $location) . '] ' . $subject; // body start $body = $body == strip_tags($body) ? nl2br($body) : $body; if ($request || $server) { $body .= '<br /><br />'; } // show request if ($request) { if (isset($_POST) && !empty($_POST)) { $body .= Misc::dump($_POST, '$_POST', true); } if (isset($_GET) && !empty($_GET)) { $body .= Misc::dump($_GET, '$_GET', true); } if (isset($_COOKIE) && !empty($_COOKIE)) { $body .= Misc::dump($_COOKIE, '$_COOKIE', true); } if (isset($_SESSION) && !empty($_SESSION)) { $body .= Misc::dump($_SESSION, '$_SESSION', true); } } // show server if ($server) { $body .= Misc::dump($_SERVER, '$_SERVER', true); } // send email $email->subject($subject)->send($body); }