PageRenderTime 63ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/Classes/TYPO3/FLOW3/Error/DebugExceptionHandler.php

https://github.com/christianjul/FLOW3-Composer
PHP | 210 lines | 148 code | 21 blank | 41 comment | 19 complexity | eda73e691555bc820103de3041052ce3 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace TYPO3\FLOW3\Error;
  3. /* *
  4. * This script belongs to the FLOW3 framework. *
  5. * *
  6. * It is free software; you can redistribute it and/or modify it under *
  7. * the terms of the GNU Lesser General Public License, either version 3 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * The TYPO3 project - inspiring people to share! *
  11. * */
  12. use TYPO3\FLOW3\Annotations as FLOW3;
  13. /**
  14. * A basic but solid exception handler which catches everything which
  15. * falls through the other exception handlers and provides useful debugging
  16. * information.
  17. *
  18. * @FLOW3\Scope("singleton")
  19. */
  20. class DebugExceptionHandler extends \TYPO3\FLOW3\Error\AbstractExceptionHandler {
  21. /**
  22. * Formats and echoes the exception as XHTML.
  23. *
  24. * @param \Exception $exception The exception object
  25. * @return void
  26. */
  27. protected function echoExceptionWeb(\Exception $exception) {
  28. $statusCode = 500;
  29. if ($exception instanceof \TYPO3\FLOW3\Exception) {
  30. $statusCode = $exception->getStatusCode();
  31. }
  32. $statusMessage = \TYPO3\FLOW3\Http\Response::getStatusMessageByCode($statusCode);
  33. if (!headers_sent()) {
  34. header(sprintf('HTTP/1.1 %s %s', $statusCode, $statusMessage));
  35. }
  36. $renderingOptions = $this->resolveCustomRenderingOptions($exception);
  37. if ($renderingOptions !== NULL && isset($renderingOptions['fluidTemplate'])) {
  38. $referenceCode = ($exception instanceof \TYPO3\FLOW3\Exception) ? $exception->getReferenceCode() : NULL;
  39. echo $this->buildCustomFluidView($renderingOptions, $statusCode, $referenceCode)->render();
  40. } else {
  41. echo $this->renderStatically($statusCode, $exception);
  42. }
  43. }
  44. /**
  45. * Returns the statically rendered exception message
  46. *
  47. * @param integer $statusCode
  48. * @param \Exception $exception
  49. * @return string
  50. */
  51. protected function renderStatically($statusCode, $exception) {
  52. $statusMessage = \TYPO3\FLOW3\Http\Response::getStatusMessageByCode($statusCode);
  53. $exceptionHeader = '';
  54. while (true) {
  55. $pathPosition = strpos($exception->getFile(), 'Packages/');
  56. $filePathAndName = ($pathPosition !== FALSE) ? substr($exception->getFile(), $pathPosition) : $exception->getFile();
  57. $exceptionCodeNumber = ($exception->getCode() > 0) ? '#' . $exception->getCode() . ': ' : '';
  58. $moreInformationLink = ($exceptionCodeNumber != '') ? '(<a href="http://typo3.org/go/exception/' . $exception->getCode() . '">More information</a>)' : '';
  59. $createIssueLink = $this->getCreateIssueLink($exception);
  60. $exceptionHeader .= '
  61. <strong style="color: #BE0027;">' . $exceptionCodeNumber . htmlspecialchars($exception->getMessage()) . '</strong> ' . $moreInformationLink . '<br />
  62. <br />
  63. <span class="ExceptionProperty">' . get_class($exception) . '</span> thrown in file<br />
  64. <span class="ExceptionProperty">' . $filePathAndName . '</span> in line
  65. <span class="ExceptionProperty">' . $exception->getLine() . '</span>.<br />';
  66. if ($exception instanceof \TYPO3\FLOW3\Exception) {
  67. $exceptionHeader .= '<span class="ExceptionProperty">Reference code: ' . $exception->getReferenceCode() . '</span><br />';
  68. }
  69. if ($exception->getPrevious() === NULL) {
  70. $exceptionHeader .= '<br /><a href="' . $createIssueLink . '">Go to the FORGE issue tracker and report the issue</a> - <strong>if you think it is a bug!</strong><br />';
  71. break;
  72. } else {
  73. $exceptionHeader .= '<br /><div style="width: 100%; background-color: #515151; color: white; padding: 2px; margin: 0 0 6px 0;">Nested Exception</div>';
  74. $exception = $exception->getPrevious();
  75. }
  76. }
  77. $backtraceCode = \TYPO3\FLOW3\Error\Debugger::getBacktraceCode($exception->getTrace());
  78. echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
  79. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
  80. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
  81. <head>
  82. <title>' . $statusCode . ' ' . $statusMessage . '</title>
  83. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  84. <style>
  85. .ExceptionProperty {
  86. color: #101010;
  87. }
  88. pre {
  89. margin: 0;
  90. font-size: 11px;
  91. color: #515151;
  92. background-color: #D0D0D0;
  93. padding-left: 30px;
  94. }
  95. </style>
  96. </head>
  97. <div style="
  98. position: absolute;
  99. left: 10px;
  100. background-color: #B9B9B9;
  101. outline: 1px solid #515151;
  102. color: #515151;
  103. font-family: Arial, Helvetica, sans-serif;
  104. font-size: 12px;
  105. margin: 10px;
  106. padding: 0;
  107. ">
  108. <div style="width: 100%; background-color: #515151; color: white; padding: 2px; margin: 0 0 6px 0;">Uncaught Exception in FLOW3</div>
  109. <div style="width: 100%; padding: 2px; margin: 0 0 6px 0;">
  110. ' . $exceptionHeader . '
  111. <br />
  112. ' . $backtraceCode . '
  113. </div>
  114. </div>
  115. ';
  116. }
  117. /**
  118. * Formats and echoes the exception for the command line
  119. *
  120. * @param \Exception $exception The exception object
  121. * @return void
  122. */
  123. protected function echoExceptionCli(\Exception $exception) {
  124. $pathPosition = strpos($exception->getFile(), 'Packages/');
  125. $filePathAndName = ($pathPosition !== FALSE) ? substr($exception->getFile(), $pathPosition) : $exception->getFile();
  126. $exceptionCodeNumber = ($exception->getCode() > 0) ? '#' . $exception->getCode() . ': ' : '';
  127. echo PHP_EOL . 'Uncaught Exception in FLOW3 ' . $exceptionCodeNumber . $exception->getMessage() . PHP_EOL;
  128. echo 'thrown in file ' . $filePathAndName . PHP_EOL;
  129. echo 'in line ' . $exception->getLine() . PHP_EOL;
  130. if ($exception instanceof \TYPO3\FLOW3\Exception) {
  131. echo 'Reference code: ' . $exception->getReferenceCode() . PHP_EOL;
  132. }
  133. $indent = ' ';
  134. while (($exception = $exception->getPrevious()) !== NULL) {
  135. echo PHP_EOL . $indent . 'Nested exception:' . PHP_EOL;
  136. $pathPosition = strpos($exception->getFile(), 'Packages/');
  137. $filePathAndName = ($pathPosition !== FALSE) ? substr($exception->getFile(), $pathPosition) : $exception->getFile();
  138. $exceptionCodeNumber = ($exception->getCode() > 0) ? '#' . $exception->getCode() . ': ' : '';
  139. echo PHP_EOL . $indent . 'Uncaught Exception in FLOW3 ' . $exceptionCodeNumber . $exception->getMessage() . PHP_EOL;
  140. echo $indent . 'thrown in file ' . $filePathAndName . PHP_EOL;
  141. echo $indent . 'in line ' . $exception->getLine() . PHP_EOL;
  142. if ($exception instanceof \TYPO3\FLOW3\Exception) {
  143. echo 'Reference code: ' . $exception->getReferenceCode() . PHP_EOL;
  144. }
  145. $indent .= ' ';
  146. }
  147. if (function_exists('xdebug_get_function_stack')) {
  148. $backtraceSteps = xdebug_get_function_stack();
  149. } else {
  150. $backtraceSteps = debug_backtrace();
  151. }
  152. for ($index = 0; $index < count($backtraceSteps); $index ++) {
  153. echo PHP_EOL . '#' . $index . ' ' ;
  154. if (isset($backtraceSteps[$index]['class'])) {
  155. echo $backtraceSteps[$index]['class'];
  156. }
  157. if (isset($backtraceSteps[$index]['function'])) {
  158. echo '::' . $backtraceSteps[$index]['function'] . '()';
  159. }
  160. echo PHP_EOL;
  161. if (isset($backtraceSteps[$index]['file'])) {
  162. echo ' ' . $backtraceSteps[$index]['file'] . (isset($backtraceSteps[$index]['line']) ? ':' . $backtraceSteps[$index]['line'] : '') . PHP_EOL;
  163. }
  164. }
  165. echo PHP_EOL;
  166. exit(1);
  167. }
  168. /**
  169. * Returns a link pointing to Forge to create a new issue.
  170. *
  171. * @param \Exception $exception
  172. * @return string
  173. */
  174. protected function getCreateIssueLink(\Exception $exception) {
  175. $filename = basename($exception->getFile());
  176. return 'http://forge.typo3.org/projects/package-typo3-flow3/issues/new?issue[subject]='
  177. . urlencode(get_class($exception) . ' thrown in file ' . $filename)
  178. . '&issue[description]='
  179. . urlencode(
  180. $exception->getMessage() . chr(10)
  181. . strip_tags(
  182. str_replace(array('<br />', '</pre>'), chr(10), \TYPO3\FLOW3\Error\Debugger::getBacktraceCode($exception->getTrace(), FALSE))
  183. )
  184. . chr(10) . 'Please include more helpful information!'
  185. )
  186. . '&issue[category_id]=554&issue[priority_id]=7';
  187. }
  188. }
  189. ?>