/** * This method is a temporary port of _drupal_decode_exception(). * * @todo This should get refactored. FlattenException could use some * improvement as well. * * @param \Symfony\Component\Debug\Exception\FlattenException $exception * The flattened exception. * * @return array * An array of string-substitution tokens for formatting a message about the * exception. */ protected function decodeException(FlattenException $exception) { $message = $exception->getMessage(); $backtrace = $exception->getTrace(); // This value is missing from the stack for some reason in the // FlattenException version of the backtrace. $backtrace[0]['line'] = $exception->getLine(); // For database errors, we try to return the initial caller, // skipping internal functions of the database layer. if (strpos($exception->getClass(), 'DatabaseExceptionWrapper') !== FALSE) { // A DatabaseExceptionWrapper exception is actually just a courier for // the original PDOException. It's the stack trace from that exception // that we care about. $backtrace = $exception->getPrevious()->getTrace(); $backtrace[0]['line'] = $exception->getLine(); // The first element in the stack is the call, the second element gives us the caller. // We skip calls that occurred in one of the classes of the database layer // or in one of its global functions. $db_functions = array('db_query', 'db_query_range'); while (!empty($backtrace[1]) && ($caller = $backtrace[1]) && (strpos($caller['namespace'], 'Drupal\\Core\\Database') !== FALSE || strpos($caller['class'], 'PDO') !== FALSE) || in_array($caller['function'], $db_functions)) { // We remove that call. array_shift($backtrace); } } $caller = Error::getLastCaller($backtrace); return array('%type' => $exception->getClass(), '!message' => String::checkPlain($message), '%function' => $caller['function'], '%file' => $caller['file'], '%line' => $caller['line'], 'severity_level' => WATCHDOG_ERROR); }
/** * Tests the getLastCaller() method. * * @param array $backtrace * The test backtrace array. * @param array $expected * The expected return array. * * @dataProvider providerTestGetLastCaller */ public function testGetLastCaller($backtrace, $expected) { $this->assertSame($expected, Error::getLastCaller($backtrace)); }
/** * Handle exceptions. * * @see set_exception_handler */ protected function exceptionHandler($exception) { $backtrace = $exception->getTrace(); $verbose_backtrace = $backtrace; // Push on top of the backtrace the call that generated the exception. array_unshift($backtrace, array('line' => $exception->getLine(), 'file' => $exception->getFile())); $decoded_exception = Error::decodeException($exception); unset($decoded_exception['backtrace']); $message = SafeMarkup::format('%type: @message in %function (line %line of %file). <pre class="backtrace">@backtrace</pre>', $decoded_exception + array('@backtrace' => Error::formatBacktrace($verbose_backtrace))); $this->error($message, 'Uncaught exception', Error::getLastCaller($backtrace)); }
/** * Handle exceptions. * * @see set_exception_handler */ protected function exceptionHandler($exception) { require_once DRUPAL_ROOT . '/core/includes/errors.inc'; $backtrace = $exception->getTrace(); $verbose_backtrace = $backtrace; // Push on top of the backtrace the call that generated the exception. array_unshift($backtrace, array('line' => $exception->getLine(), 'file' => $exception->getFile())); // \Drupal\Core\Utility\Error::decodeException() runs the exception // message through \Drupal\Component\Utility\String::checkPlain(). $decoded_exception = Error::decodeException($exception); unset($decoded_exception['backtrace']); $message = String::format('%type: !message in %function (line %line of %file). <pre class="backtrace">!backtrace</pre>', $decoded_exception + array('!backtrace' => Error::formatBacktrace($verbose_backtrace))); $this->error($message, 'Uncaught exception', Error::getLastCaller($backtrace)); }