Example #1
0
 /**
  * @inheritDoc
  */
 public function get()
 {
     if (!isset($this->throwable)) {
         throw new LogicException('Throwable must be set to get view.');
     }
     ob_start();
     var_dump($this->throwable->getTrace());
     $fullTrace = ob_get_clean();
     // If xdebug is installed then var_dump is pretty printed for html already.
     if (function_exists('xdebug_var_dump')) {
         $viewTrace = new Text();
         $viewTrace->setHTML5($fullTrace);
         $fullTrace = ['div', ['class' => 'full_trace'], $viewTrace->get()];
     } else {
         $fullTrace = ['pre', ['class' => 'full_trace'], $fullTrace];
     }
     return ['div', ['class' => 'Throwable'], [['div', ['class' => 'location'], 'Thrown at ' . $this->throwable->getFile() . ' line ' . $this->throwable->getLine()], ['pre', [], $this->throwable->getMessage()], ['h2', [], 'Basic Trace'], ['pre', ['class' => 'basic_trace'], $this->throwable->getTraceAsString()], ['h2', [], 'Full Trace'], $fullTrace]];
 }
 /**
  * @dataProvider providerTestPrepareStep
  * @param HtmlHelper $helper
  * @param \Exception|\Throwable $exception
  */
 public function testPrepareStep(HtmlHelper $helper, $exception)
 {
     foreach ($exception->getTrace() as $step) {
         $colorized = $helper->prepareStep($step);
         $this->assertTrue(is_array($colorized));
         foreach (array('title', 'source', 'key', 'arguments') as $key) {
             $this->assertTrue(isset($colorized[$key]));
         }
         $this->assertTrue(is_array($colorized['arguments']));
     }
 }
 /**
  * @param \Exception|\Throwable $exception
  */
 public function handle_exception($exception)
 {
     if (getenv('APP_ENV') === 'dev') {
         list($code, $file, $line, $message, $previous, $trace, $trace_string) = [$exception->getCode(), $exception->getFile(), $exception->getLine(), $exception->getMessage(), $exception->getPrevious(), $exception->getTrace(), $exception->getTraceAsString()];
         $trace_info = "<b>file</b>: {$trace[0]['file']} <b>in line</b> ({$trace[0]['line']})";
         echo "<h2>COGS Runtime Exception: [::{$code}] {$message}</h2>";
         echo "<b>Trace:</b><br>";
         echo "<pre>{$trace_string}</pre>";
         echo "<b>Debug:</b><br>";
         dump(compact('code', 'file', 'line', 'message', 'previous', 'trace'));
     }
 }
 public function __construct(\Throwable $e)
 {
     if ($e instanceof \ParseError) {
         $message = 'Parse error: ' . $e->getMessage();
         $severity = E_PARSE;
     } elseif ($e instanceof \TypeError) {
         $message = 'Type error: ' . $e->getMessage();
         $severity = E_RECOVERABLE_ERROR;
     } else {
         $message = $e->getMessage();
         $severity = E_ERROR;
     }
     \ErrorException::__construct($message, $e->getCode(), $severity, $e->getFile(), $e->getLine());
     $this->setTrace($e->getTrace());
 }
Example #5
0
 /**
  * 记录起始请求日志
  * 记录成功返回true,失败或没记录日志返回false
  *
  * @param  \Exception|\Throwable   $ex
  * @return  bool
  */
 public static function error_log($ex)
 {
     if (!BaseLog::isLog('error')) {
         return false;
     }
     if (!Config::getEnv("app.framework_error_log")) {
         return false;
     }
     $data = Request::nonPostParam();
     if (Config::getEnv("app.request_log_post")) {
         $data = Request::param();
     }
     $log_msg = "\r\nQP->Main最外层捕捉到Exception异常:\r\n请求参数:{Param}\r\n异常信息:{E_Msg}\r\n异常位置:{E_Point}\r\n更多异常队列信息:{E_Trace}\r\n";
     $log_data = ['Param' => json_encode($data), 'E_Msg' => $ex->getMessage(), 'E_Point' => $ex->getFile() . ":" . $ex->getLine(), 'E_Trace' => json_encode($ex->getTrace())];
     return Log::error($log_msg, $log_data, true, 'framework');
 }
Example #6
0
 /**
  * Render the backtrace
  *
  * @return  string  The contents of the backtrace
  *
  * @since   11.1
  */
 public function renderBacktrace()
 {
     // If no error object is set return null
     if (!isset($this->_error)) {
         return;
     }
     $contents = null;
     $backtrace = $this->_error->getTrace();
     if (is_array($backtrace)) {
         ob_start();
         $j = 1;
         echo '<table cellpadding="0" cellspacing="0" class="Table">';
         echo '	<tr>';
         echo '		<td colspan="3" class="TD"><strong>Call stack</strong></td>';
         echo '	</tr>';
         echo '	<tr>';
         echo '		<td class="TD"><strong>#</strong></td>';
         echo '		<td class="TD"><strong>Function</strong></td>';
         echo '		<td class="TD"><strong>Location</strong></td>';
         echo '	</tr>';
         for ($i = count($backtrace) - 1; $i >= 0; $i--) {
             echo '	<tr>';
             echo '		<td class="TD">' . $j . '</td>';
             if (isset($backtrace[$i]['class'])) {
                 echo '	<td class="TD">' . $backtrace[$i]['class'] . $backtrace[$i]['type'] . $backtrace[$i]['function'] . '()</td>';
             } else {
                 echo '	<td class="TD">' . $backtrace[$i]['function'] . '()</td>';
             }
             if (isset($backtrace[$i]['file'])) {
                 echo '		<td class="TD">' . $backtrace[$i]['file'] . ':' . $backtrace[$i]['line'] . '</td>';
             } else {
                 echo '		<td class="TD">&#160;</td>';
             }
             echo '	</tr>';
             $j++;
         }
         echo '</table>';
         $contents = ob_get_contents();
         ob_end_clean();
     }
     return $contents;
 }
Example #7
0
 public function exceptionHandler(\Throwable $exception)
 {
     $message = $exception->getMessage();
     $values = $exception->getTrace();
     $traces = [];
     $trace = new Trace();
     $trace->setFile($exception->getFile());
     $trace->setLine($exception->getLine());
     $trace->setClass(get_class($exception));
     $traces[] = $trace;
     foreach ($values as $value) {
         $trace = new Trace();
         $trace->setFile($this->getArrayValue($value, "file"));
         $trace->setLine($this->getArrayValue($value, "line"));
         $trace->setFunction($this->getArrayValue($value, "function"));
         $trace->setClass($this->getArrayValue($value, "class"));
         $traces[] = $trace;
     }
     $this->log($message, $traces);
 }
 /**
  * Method that returns a response in the request format
  *
  * @todo Customize according to project
  * @param string $message Error message
  * @param int $status Status code
  * @param \Exception $exception Exception caught
  * @return JsonResponse|HtmlResponse
  */
 public function getResponseFormat($message, $status, \Throwable $exception)
 {
     if (count($this->accept) > 0 && $this->accept[0] == 'application/json') {
         $data = ['status' => $status, 'error' => ['msg' => $message]];
         if ($this->debug) {
             $data['error']['debug']['msg'] = $exception->getMessage();
             $data['error']['debug']['file'] = $exception->getFile();
             $data['error']['debug']['line'] = $exception->getLine();
             $data['error']['debug']['trace'] = $exception->getTrace();
         }
         return new JsonResponse($data, $status);
     }
     $msg = $message;
     if ($this->debug) {
         $msg .= '<br />Description: ' . $exception->getMessage();
         $msg .= '<br />File: ' . $exception->getFile();
         $msg .= '<br />Line: ' . $exception->getLine();
         $msg .= '<br />Trace: ' . $exception->getTraceAsString();
     }
     return new HtmlResponse($msg, $status);
 }
Example #9
0
 /**
  * @param DefinitionInterface $definition
  * @param \Throwable          $exception
  * @return string
  */
 private function getTraceAsString(DefinitionInterface $definition, $exception) : string
 {
     $stringParts = [];
     $previousStep = [];
     $enableColoredOutput = $this->getEnableColoredOutput();
     $stackTrace = $exception->getTrace();
     $stackTraceCount = count($stackTrace);
     for ($i = 0; $i < $stackTraceCount; $i++) {
         $step = $stackTrace[$i];
         if ($this->shouldStopTraceAtStep($step)) {
             break;
         }
         $stepAsString = $this->getTraceStepAsString($step, $i);
         if ($enableColoredOutput && $this->getTraceStepIsTestMethod($definition, $step, $previousStep)) {
             $stringParts[] = self::NORMAL . self::RED_BACKGROUND . $stepAsString . self::RED;
         } else {
             $stringParts[] = $stepAsString;
         }
         $previousStep = $step;
     }
     return implode("\n", $stringParts);
 }
Example #10
0
 /**
  * Decodes an exception and retrieves the correct caller.
  *
  * @param \Exception|\Throwable $exception
  *   The exception object that was thrown.
  *
  * @return array
  *   An error in the format expected by _drupal_log_error().
  */
 public static function decodeException($exception)
 {
     $message = $exception->getMessage();
     $backtrace = $exception->getTrace();
     // Add the line throwing the exception to the backtrace.
     array_unshift($backtrace, array('line' => $exception->getLine(), 'file' => $exception->getFile()));
     // For PDOException errors, we try to return the initial caller,
     // skipping internal functions of the database layer.
     if ($exception instanceof \PDOException || $exception instanceof DatabaseExceptionWrapper) {
         // 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]) && (isset($caller['class']) && (strpos($caller['class'], 'Query') !== FALSE || strpos($caller['class'], 'Database') !== FALSE || strpos($caller['class'], 'PDO') !== FALSE) || in_array($caller['function'], $db_functions))) {
             // We remove that call.
             array_shift($backtrace);
         }
         if (isset($exception->query_string, $exception->args)) {
             $message .= ": " . $exception->query_string . "; " . print_r($exception->args, TRUE);
         }
     }
     $caller = static::getLastCaller($backtrace);
     return array('%type' => get_class($exception), '@message' => $message, '%function' => $caller['function'], '%file' => $caller['file'], '%line' => $caller['line'], 'severity_level' => static::ERROR, 'backtrace' => $backtrace, '@backtrace_string' => $exception->getTraceAsString());
 }
Example #11
0
 public function logException(\Throwable $e, $trace = null)
 {
     if ($trace === null) {
         $trace = $e->getTrace();
     }
     $errstr = $e->getMessage();
     $errfile = $e->getFile();
     $errno = $e->getCode();
     $errline = $e->getLine();
     $errorConversion = [0 => "EXCEPTION", E_ERROR => "E_ERROR", E_WARNING => "E_WARNING", E_PARSE => "E_PARSE", E_NOTICE => "E_NOTICE", E_CORE_ERROR => "E_CORE_ERROR", E_CORE_WARNING => "E_CORE_WARNING", E_COMPILE_ERROR => "E_COMPILE_ERROR", E_COMPILE_WARNING => "E_COMPILE_WARNING", E_USER_ERROR => "E_USER_ERROR", E_USER_WARNING => "E_USER_WARNING", E_USER_NOTICE => "E_USER_NOTICE", E_STRICT => "E_STRICT", E_RECOVERABLE_ERROR => "E_RECOVERABLE_ERROR", E_DEPRECATED => "E_DEPRECATED", E_USER_DEPRECATED => "E_USER_DEPRECATED"];
     if ($errno === 0) {
         $type = LogLevel::CRITICAL;
     } else {
         $type = ($errno === E_ERROR or $errno === E_USER_ERROR) ? LogLevel::ERROR : (($errno === E_USER_WARNING or $errno === E_WARNING) ? LogLevel::WARNING : LogLevel::NOTICE);
     }
     $errno = isset($errorConversion[$errno]) ? $errorConversion[$errno] : $errno;
     if (($pos = strpos($errstr, "\n")) !== false) {
         $errstr = substr($errstr, 0, $pos);
     }
     $errfile = \pocketmine\cleanPath($errfile);
     $this->log($type, get_class($e) . ": \"{$errstr}\" ({$errno}) in \"{$errfile}\" at line {$errline}");
     foreach (@\pocketmine\getTrace(1, $trace) as $i => $line) {
         $this->debug($line);
     }
 }
Example #12
0
/**
 * Convert an Exception or Error into an array (for logging)
 *
 * @param \Throwable $ex
 * @return array
 */
function throwableToArray(\Throwable $ex) : array
{
    $prev = $ex->getPrevious();
    return ['line' => $ex->getLine(), 'file' => $ex->getFile(), 'message' => $ex->getMessage(), 'code' => $ex->getCode(), 'trace' => $ex->getTrace(), 'previous' => $prev ? throwableToArray($prev) : null];
}
 /**
  * Log the Exception / Error thrown
  *
  * @param      \Throwable  $t      The Exception / Error thrown
  */
 public function log(\Throwable $t)
 {
     $this->logger->log($t->getCode(), $t->getMessage(), $t->getTrace());
 }
Example #14
0
 /**
  * @param \Throwable $exception
  *
  * @return array
  */
 protected function exceptionStackTrace(\Throwable $exception) : array
 {
     $return = [];
     $line = $exception->getLine();
     $file = $exception->getFile();
     $trace = $exception->getTrace();
     foreach (explode("\n", $exception->getTraceAsString()) as $currentLine) {
         $row = explode(' ', $currentLine);
         $stack = ['file' => null];
         $buffer = null;
         $index = trim(array_shift($row), '#');
         foreach ($row as $current) {
             // current file
             if (is_null($stack['file'])) {
                 if (substr($current, -1) === ':') {
                     $stack['file'] = trim($buffer ? $buffer . $current : $current, ':');
                     if (preg_match('`([^\\(]+)\\(([0-9]+)\\)`', $stack['file'], $matches)) {
                         $stack['file'] = $matches[1];
                         $stack['line'] = (int) $matches[2];
                     }
                 } else {
                     $buffer .= $current . ' ';
                 }
             } elseif (!isset($stack['function'])) {
                 $stack['function'] = strstr($current, '(', true);
                 $explodeString = strpos($stack['function'], '::') !== false ? '::' : '->';
                 $explode = explode($explodeString, $stack['function']);
                 if (sizeof($explode) > 1) {
                     $stack['class'] = $explode[0];
                     $stack['type'] = $explodeString;
                     $stack['function'] = $explode[1];
                     if (strpos($stack['class'], '\\') !== false) {
                         $explode = explode('\\', $stack['class']);
                         $stack['class'] = array_pop($explode);
                         $stack['namespace'] = implode('\\', $explode);
                     }
                 } elseif (strpos($stack['function'], '\\') !== false) {
                     $explode = explode('\\', $stack['function']);
                     $stack['function'] = array_pop($explode);
                     $stack['namespace'] = implode('\\', $explode);
                 }
                 $stack['args'] = strstr($current, '(') . ' ';
             } else {
                 $stack['args'] .= $current . ' ';
             }
         }
         if (is_null($stack['file'])) {
             $stack['file'] = $row[0];
         }
         $skip = false;
         // hack to hide error handler
         if (isset($stack['namespace']) && isset($stack['class'])) {
             if ($stack['namespace'] == 'Cawa\\Error' && $stack['class'] == 'Handler') {
                 $skip = true;
             }
         }
         // hack to hide backtrace call
         if (isset($stack['function']) && $stack['function'] == 'backtrace') {
             $skip = true;
             $file = $stack['file'];
             $line = (int) $stack['line'];
         }
         if ($skip == false) {
             if (isset($trace[$index]) && isset($trace[$index]['args'])) {
                 $stack['fullargs'] = $trace[$index]['args'];
             }
             $return[] = $stack;
         }
     }
     $return[0]['line'] = $line;
     $return[0]['file'] = $file;
     return $return;
 }
Example #15
0
 /**
  * @param \Throwable|\Exception $exc
  * @return array
  */
 protected function build_exception_frames($exc)
 {
     $frames = array();
     foreach ($exc->getTrace() as $frame) {
         $framedata = array('filename' => isset($frame['file']) ? $frame['file'] : '<internal>', 'lineno' => isset($frame['line']) ? $frame['line'] : 0, 'method' => $frame['function']);
         if ($this->include_exception_code_context && isset($frame['file']) && isset($frame['line'])) {
             $this->add_frame_code_context($frame['file'], $frame['line'], $framedata);
         }
         $frames[] = $framedata;
     }
     // rollbar expects most recent call to be last, not first
     $frames = array_reverse($frames);
     // add top-level file and line to end of the reversed array
     $file = $exc->getFile();
     $line = $exc->getLine();
     $framedata = array('filename' => $file, 'lineno' => $line);
     if ($this->include_exception_code_context) {
         $this->add_frame_code_context($file, $line, $framedata);
     }
     $frames[] = $framedata;
     $this->shift_method($frames);
     return $frames;
 }
Example #16
0
 public static function getExceptionTraceAsString(\Throwable $exception)
 {
     $rtn = "";
     $count = 0;
     foreach ($exception->getTrace() as $frame) {
         $args = "";
         if (isset($frame['args'])) {
             $args = [];
             foreach ($frame['args'] as $arg) {
                 if (is_string($arg)) {
                     $args[] = "'" . $arg . "'";
                 } elseif (is_array($arg)) {
                     $args[] = "Array";
                 } elseif (is_null($arg)) {
                     $args[] = 'NULL';
                 } elseif (is_bool($arg)) {
                     $args[] = $arg ? "true" : "false";
                 } elseif (is_object($arg)) {
                     $args[] = get_class($arg);
                 } elseif (is_resource($arg)) {
                     $args[] = get_resource_type($arg);
                 } else {
                     $args[] = $arg;
                 }
             }
             $args = join(", ", $args);
         }
         $rtn .= sprintf("#%s %s(%s): %s(%s)\n", $count, isset($frame['file']) ? $frame['file'] : '', isset($frame['line']) ? $frame['line'] : '', $frame['function'], $args);
         $count++;
     }
     return $rtn;
 }
Example #17
0
 /**
  * @param Exception|Throwable $e
  * @return bool Should the exception use $wgOut to output the error?
  */
 private static function useOutputPage($e)
 {
     // Can the extension use the Message class/wfMessage to get i18n-ed messages?
     foreach ($e->getTrace() as $frame) {
         if (isset($frame['class']) && $frame['class'] === 'LocalisationCache') {
             return false;
         }
     }
     return !empty($GLOBALS['wgFullyInitialised']) && !empty($GLOBALS['wgOut']) && !defined('MEDIAWIKI_INSTALL');
 }
Example #18
0
 /**
  * Handle exception.
  *
  * @param \Throwable $e
  *
  * @return Response
  */
 public static function handleException(\Throwable $e) : Response
 {
     self::render($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine(), null, $e->getTrace());
 }
 /**
  * Return a copy of an exception's backtrace as an array.
  *
  * Like Exception::getTrace, but replaces each element in each frame's
  * argument array with the name of its class (if the element is an object)
  * or its type (if the element is a PHP primitive).
  *
  * @since 1.22
  * @param Exception|Throwable $e
  * @return array
  */
 public static function getRedactedTrace($e)
 {
     return static::redactTrace($e->getTrace());
 }
Example #20
0
/**
 * Default exception handler
 *
 * @param  Throwable|Exception $exception
 */
function COM_handleException($exception)
{
    COM_handleError((int) $exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine(), $exception->getTrace());
    die(1);
}
 /**
  * Outputs the full stack trace with enhanced information.
  *
  * @internal
  * @param \Exception|\Throwable $exception
  */
 function exceptionHandler($exception)
 {
     $NL = PHP_EOL;
     $stack = $exception->getTrace();
     if ($exception instanceof \ErrorException) {
         array_shift($stack);
     }
     $c = count($stack);
     echo sprintf("{$NL}Unhandled exception: %s{$NL}{$NL}Stack trace:{$NL}{$NL}%4d. Throw %s{$NL}      from %s, line %d{$NL}{$NL}", color('red', $exception->getMessage()), $c + 1, color('yellow', get_class($exception)), $exception->getFile(), $exception->getLine());
     foreach ($stack as $i => $l) {
         echo sprintf("%4d. Call %s{$NL}      from %s, line %d{$NL}{$NL}", $c - $i, color('yellow', sprintf('%s%s (%s)', isset($l['class']) ? $l['class'] . get($l, 'type', '::') : '', $l['function'], implode(',', map($l['args'], [__CLASS__, 'formatErrorArg'])))), get($l, 'file', 'an unknown location'), get($l, 'line'));
     }
 }
Example #22
0
 /**
  * @param \Exception|\Throwable $e
  * @return string|NULL
  */
 private static function resolveExceptionTable($e)
 {
     if (!$e instanceof Doctrine\DBAL\DBALException) {
         return NULL;
     }
     if ($caused = Tracy\Helpers::findTrace($e->getTrace(), 'Doctrine\\DBAL\\DBALException::driverExceptionDuringQuery')) {
         if (preg_match('~(?:INSERT|UPDATE|REPLACE)(?:[A-Z_\\s]*)`?([^\\s`]+)`?\\s*~', is_string($caused['args'][1]) ? $caused['args'][1] : $caused['args'][2], $m)) {
             return $m[1];
         }
     }
     return NULL;
 }
Example #23
0
 public function exceptionHandler(\Throwable $e, $trace = null)
 {
     if ($e === null) {
         return;
     }
     global $lastError;
     if ($trace === null) {
         $trace = $e->getTrace();
     }
     $errstr = $e->getMessage();
     $errfile = $e->getFile();
     $errno = $e->getCode();
     $errline = $e->getLine();
     $type = ($errno === E_ERROR or $errno === E_USER_ERROR) ? \LogLevel::ERROR : (($errno === E_USER_WARNING or $errno === E_WARNING) ? \LogLevel::WARNING : \LogLevel::NOTICE);
     if (($pos = strpos($errstr, "\n")) !== false) {
         $errstr = substr($errstr, 0, $pos);
     }
     $errfile = cleanPath($errfile);
     if ($this->logger instanceof MainLogger) {
         $this->logger->logException($e, $trace);
     }
     $lastError = ["type" => $type, "message" => $errstr, "fullFile" => $e->getFile(), "file" => $errfile, "line" => $errline, "trace" => @getTrace(1, $trace)];
     global $lastExceptionError, $lastError;
     $lastExceptionError = $lastError;
     $this->crashDump();
 }
Example #24
0
 /**
  * Handles an uncaught exception. The exception must extend the \Exception class to be handled.
  *
  * @param \Exception|\Throwable $exception   The exception to handle.
  * @param string                $errorId     The internal ID of the error.
  */
 public function handleException($exception, $errorId)
 {
     $this->handleError(ErrorHandlerHelper::E_EXCEPTION, $exception->getMessage(), $exception->getFile(), $exception->getLine(), array('Exception' => $exception), $errorId, $exception->getTrace());
 }
 /**
  * Prepare a response in case an error occurred.
  *
  * @param \Throwable $exception
  * @param Http\Response $response
  * @return void
  */
 protected function prepareErrorResponse($exception, Http\Response $response)
 {
     $pathPosition = strpos($exception->getFile(), 'Packages/');
     $filePathAndName = $pathPosition !== false ? substr($exception->getFile(), $pathPosition) : $exception->getFile();
     $exceptionCodeNumber = $exception->getCode() > 0 ? '#' . $exception->getCode() . ': ' : '';
     $content = PHP_EOL . 'Uncaught Exception in Flow ' . $exceptionCodeNumber . $exception->getMessage() . PHP_EOL;
     $content .= 'thrown in file ' . $filePathAndName . PHP_EOL;
     $content .= 'in line ' . $exception->getLine() . PHP_EOL . PHP_EOL;
     $content .= Debugger::getBacktraceCode($exception->getTrace(), false, true) . PHP_EOL;
     if ($exception instanceof Exception) {
         $statusCode = $exception->getStatusCode();
     } else {
         $statusCode = 500;
     }
     $response->setStatus($statusCode);
     $response->setContent($content);
     $response->setHeader('X-Flow-ExceptionCode', $exception->getCode());
     $response->setHeader('X-Flow-ExceptionMessage', $exception->getMessage());
 }
Example #26
0
 /**
  * Exception handler function
  *
  * @param Exception $e
  */
 public static function exception_handler(Throwable $e)
 {
     self::$errors[] = ['errno' => $e->getCode(), 'error' => [$e->getMessage()], 'file' => $e->getFile(), 'line' => $e->getLine(), 'code' => self::get_code($e->getFile(), $e->getLine()), 'backtrace' => self::debug_backtrace_string($e->getTrace())];
     self::$flag_exception = true;
     exit;
 }