FlattenException.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\ErrorHandler\Exception;
  11. use Symfony\Component\Debug\Exception\FatalThrowableError;
  12. use Symfony\Component\Debug\Exception\FlattenException as LegacyFlattenException;
  13. use Symfony\Component\HttpFoundation\Exception\RequestExceptionInterface;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  16. /**
  17. * FlattenException wraps a PHP Error or Exception to be able to serialize it.
  18. *
  19. * Basically, this class removes all objects from the trace.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. */
  23. class FlattenException extends LegacyFlattenException
  24. {
  25. private $message;
  26. private $code;
  27. private $previous;
  28. private $trace;
  29. private $traceAsString;
  30. private $class;
  31. private $statusCode;
  32. private $statusText;
  33. private $headers;
  34. private $file;
  35. private $line;
  36. private $asString;
  37. public static function create(\Exception $exception, $statusCode = null, array $headers = []): self
  38. {
  39. return static::createFromThrowable($exception, $statusCode, $headers);
  40. }
  41. public static function createFromThrowable(\Throwable $exception, int $statusCode = null, array $headers = []): self
  42. {
  43. $e = new static();
  44. $e->setMessage($exception->getMessage());
  45. $e->setCode($exception->getCode());
  46. if ($exception instanceof HttpExceptionInterface) {
  47. $statusCode = $exception->getStatusCode();
  48. $headers = array_merge($headers, $exception->getHeaders());
  49. } elseif ($exception instanceof RequestExceptionInterface) {
  50. $statusCode = 400;
  51. }
  52. if (null === $statusCode) {
  53. $statusCode = 500;
  54. }
  55. if (class_exists(Response::class) && isset(Response::$statusTexts[$statusCode])) {
  56. $statusText = Response::$statusTexts[$statusCode];
  57. } else {
  58. $statusText = 'Whoops, looks like something went wrong.';
  59. }
  60. $e->setStatusText($statusText);
  61. $e->setStatusCode($statusCode);
  62. $e->setHeaders($headers);
  63. $e->setTraceFromThrowable($exception);
  64. $e->setClass($exception instanceof FatalThrowableError ? $exception->getOriginalClassName() : \get_class($exception));
  65. $e->setFile($exception->getFile());
  66. $e->setLine($exception->getLine());
  67. $previous = $exception->getPrevious();
  68. if ($previous instanceof \Throwable) {
  69. $e->setPrevious(static::createFromThrowable($previous));
  70. }
  71. return $e;
  72. }
  73. public function toArray(): array
  74. {
  75. $exceptions = [];
  76. foreach (array_merge([$this], $this->getAllPrevious()) as $exception) {
  77. $exceptions[] = [
  78. 'message' => $exception->getMessage(),
  79. 'class' => $exception->getClass(),
  80. 'trace' => $exception->getTrace(),
  81. ];
  82. }
  83. return $exceptions;
  84. }
  85. public function getStatusCode(): int
  86. {
  87. return $this->statusCode;
  88. }
  89. /**
  90. * @return $this
  91. */
  92. public function setStatusCode($code): self
  93. {
  94. $this->statusCode = $code;
  95. return $this;
  96. }
  97. public function getHeaders(): array
  98. {
  99. return $this->headers;
  100. }
  101. /**
  102. * @return $this
  103. */
  104. public function setHeaders(array $headers): self
  105. {
  106. $this->headers = $headers;
  107. return $this;
  108. }
  109. public function getClass(): string
  110. {
  111. return $this->class;
  112. }
  113. /**
  114. * @return $this
  115. */
  116. public function setClass($class): self
  117. {
  118. $this->class = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
  119. return $this;
  120. }
  121. public function getFile(): string
  122. {
  123. return $this->file;
  124. }
  125. /**
  126. * @return $this
  127. */
  128. public function setFile($file): self
  129. {
  130. $this->file = $file;
  131. return $this;
  132. }
  133. public function getLine(): int
  134. {
  135. return $this->line;
  136. }
  137. /**
  138. * @return $this
  139. */
  140. public function setLine($line): self
  141. {
  142. $this->line = $line;
  143. return $this;
  144. }
  145. public function getStatusText(): string
  146. {
  147. return $this->statusText;
  148. }
  149. public function setStatusText(string $statusText): self
  150. {
  151. $this->statusText = $statusText;
  152. return $this;
  153. }
  154. public function getMessage(): string
  155. {
  156. return $this->message;
  157. }
  158. /**
  159. * @return $this
  160. */
  161. public function setMessage($message): self
  162. {
  163. if (false !== strpos($message, "class@anonymous\0")) {
  164. $message = preg_replace_callback('/class@anonymous\x00.*?\.php(?:0x?|:)[0-9a-fA-F]++/', function ($m) {
  165. return class_exists($m[0], false) ? get_parent_class($m[0]).'@anonymous' : $m[0];
  166. }, $message);
  167. }
  168. $this->message = $message;
  169. return $this;
  170. }
  171. public function getCode(): int
  172. {
  173. return $this->code;
  174. }
  175. /**
  176. * @return $this
  177. */
  178. public function setCode($code): self
  179. {
  180. $this->code = $code;
  181. return $this;
  182. }
  183. /**
  184. * @return self|null
  185. */
  186. public function getPrevious()
  187. {
  188. return $this->previous;
  189. }
  190. /**
  191. * @return $this
  192. */
  193. final public function setPrevious(LegacyFlattenException $previous): self
  194. {
  195. $this->previous = $previous;
  196. return $this;
  197. }
  198. /**
  199. * @return self[]
  200. */
  201. public function getAllPrevious(): array
  202. {
  203. $exceptions = [];
  204. $e = $this;
  205. while ($e = $e->getPrevious()) {
  206. $exceptions[] = $e;
  207. }
  208. return $exceptions;
  209. }
  210. public function getTrace(): array
  211. {
  212. return $this->trace;
  213. }
  214. /**
  215. * @deprecated since 4.1, use {@see setTraceFromThrowable()} instead.
  216. */
  217. public function setTraceFromException(\Exception $exception)
  218. {
  219. @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.1, use "setTraceFromThrowable()" instead.', __METHOD__), E_USER_DEPRECATED);
  220. $this->setTraceFromThrowable($exception);
  221. }
  222. /**
  223. * @return $this
  224. */
  225. public function setTraceFromThrowable(\Throwable $throwable): self
  226. {
  227. $this->traceAsString = $throwable->getTraceAsString();
  228. return $this->setTrace($throwable->getTrace(), $throwable->getFile(), $throwable->getLine());
  229. }
  230. /**
  231. * @return $this
  232. */
  233. public function setTrace($trace, $file, $line): self
  234. {
  235. $this->trace = [];
  236. $this->trace[] = [
  237. 'namespace' => '',
  238. 'short_class' => '',
  239. 'class' => '',
  240. 'type' => '',
  241. 'function' => '',
  242. 'file' => $file,
  243. 'line' => $line,
  244. 'args' => [],
  245. ];
  246. foreach ($trace as $entry) {
  247. $class = '';
  248. $namespace = '';
  249. if (isset($entry['class'])) {
  250. $parts = explode('\\', $entry['class']);
  251. $class = array_pop($parts);
  252. $namespace = implode('\\', $parts);
  253. }
  254. $this->trace[] = [
  255. 'namespace' => $namespace,
  256. 'short_class' => $class,
  257. 'class' => isset($entry['class']) ? $entry['class'] : '',
  258. 'type' => isset($entry['type']) ? $entry['type'] : '',
  259. 'function' => isset($entry['function']) ? $entry['function'] : null,
  260. 'file' => isset($entry['file']) ? $entry['file'] : null,
  261. 'line' => isset($entry['line']) ? $entry['line'] : null,
  262. 'args' => isset($entry['args']) ? $this->flattenArgs($entry['args']) : [],
  263. ];
  264. }
  265. return $this;
  266. }
  267. private function flattenArgs(array $args, int $level = 0, int &$count = 0): array
  268. {
  269. $result = [];
  270. foreach ($args as $key => $value) {
  271. if (++$count > 1e4) {
  272. return ['array', '*SKIPPED over 10000 entries*'];
  273. }
  274. if ($value instanceof \__PHP_Incomplete_Class) {
  275. // is_object() returns false on PHP<=7.1
  276. $result[$key] = ['incomplete-object', $this->getClassNameFromIncomplete($value)];
  277. } elseif (\is_object($value)) {
  278. $result[$key] = ['object', \get_class($value)];
  279. } elseif (\is_array($value)) {
  280. if ($level > 10) {
  281. $result[$key] = ['array', '*DEEP NESTED ARRAY*'];
  282. } else {
  283. $result[$key] = ['array', $this->flattenArgs($value, $level + 1, $count)];
  284. }
  285. } elseif (null === $value) {
  286. $result[$key] = ['null', null];
  287. } elseif (\is_bool($value)) {
  288. $result[$key] = ['boolean', $value];
  289. } elseif (\is_int($value)) {
  290. $result[$key] = ['integer', $value];
  291. } elseif (\is_float($value)) {
  292. $result[$key] = ['float', $value];
  293. } elseif (\is_resource($value)) {
  294. $result[$key] = ['resource', get_resource_type($value)];
  295. } else {
  296. $result[$key] = ['string', (string) $value];
  297. }
  298. }
  299. return $result;
  300. }
  301. private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value): string
  302. {
  303. $array = new \ArrayObject($value);
  304. return $array['__PHP_Incomplete_Class_Name'];
  305. }
  306. public function getTraceAsString(): string
  307. {
  308. return $this->traceAsString;
  309. }
  310. /**
  311. * @return $this
  312. */
  313. public function setAsString(?string $asString): self
  314. {
  315. $this->asString = $asString;
  316. return $this;
  317. }
  318. public function getAsString(): string
  319. {
  320. if (null !== $this->asString) {
  321. return $this->asString;
  322. }
  323. $message = '';
  324. $next = false;
  325. foreach (array_reverse(array_merge([$this], $this->getAllPrevious())) as $exception) {
  326. if ($next) {
  327. $message .= 'Next ';
  328. } else {
  329. $next = true;
  330. }
  331. $message .= $exception->getClass();
  332. if ('' != $exception->getMessage()) {
  333. $message .= ': '.$exception->getMessage();
  334. }
  335. $message .= ' in '.$exception->getFile().':'.$exception->getLine().
  336. "\nStack trace:\n".$exception->getTraceAsString()."\n\n";
  337. }
  338. return rtrim($message);
  339. }
  340. }