FlattenException.php 11 KB

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