FlattenException.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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, int $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_class($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. * @return $this
  107. */
  108. public function setStatusCode(int $code): self
  109. {
  110. $this->statusCode = $code;
  111. return $this;
  112. }
  113. public function getHeaders(): array
  114. {
  115. return $this->headers;
  116. }
  117. /**
  118. * @return $this
  119. */
  120. public function setHeaders(array $headers): self
  121. {
  122. $this->headers = $headers;
  123. return $this;
  124. }
  125. public function getClass(): string
  126. {
  127. return $this->class;
  128. }
  129. /**
  130. * @return $this
  131. */
  132. public function setClass(string $class): self
  133. {
  134. $this->class = false !== strpos($class, "@anonymous\0") ? (get_parent_class($class) ?: key(class_implements($class)) ?: 'class').'@anonymous' : $class;
  135. return $this;
  136. }
  137. public function getFile(): string
  138. {
  139. return $this->file;
  140. }
  141. /**
  142. * @return $this
  143. */
  144. public function setFile(string $file): self
  145. {
  146. $this->file = $file;
  147. return $this;
  148. }
  149. public function getLine(): int
  150. {
  151. return $this->line;
  152. }
  153. /**
  154. * @return $this
  155. */
  156. public function setLine(int $line): self
  157. {
  158. $this->line = $line;
  159. return $this;
  160. }
  161. public function getStatusText(): string
  162. {
  163. return $this->statusText;
  164. }
  165. /**
  166. * @return $this
  167. */
  168. public function setStatusText(string $statusText): self
  169. {
  170. $this->statusText = $statusText;
  171. return $this;
  172. }
  173. public function getMessage(): string
  174. {
  175. return $this->message;
  176. }
  177. /**
  178. * @return $this
  179. */
  180. public function setMessage(string $message): self
  181. {
  182. if (false !== strpos($message, "@anonymous\0")) {
  183. $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) {
  184. return class_exists($m[0], false) ? (get_parent_class($m[0]) ?: key(class_implements($m[0])) ?: 'class').'@anonymous' : $m[0];
  185. }, $message);
  186. }
  187. $this->message = $message;
  188. return $this;
  189. }
  190. /**
  191. * @return int|string int most of the time (might be a string with PDOException)
  192. */
  193. public function getCode()
  194. {
  195. return $this->code;
  196. }
  197. /**
  198. * @param int|string $code
  199. *
  200. * @return $this
  201. */
  202. public function setCode($code): self
  203. {
  204. $this->code = $code;
  205. return $this;
  206. }
  207. public function getPrevious(): ?self
  208. {
  209. return $this->previous;
  210. }
  211. /**
  212. * @return $this
  213. */
  214. public function setPrevious(?self $previous): self
  215. {
  216. $this->previous = $previous;
  217. return $this;
  218. }
  219. /**
  220. * @return self[]
  221. */
  222. public function getAllPrevious(): array
  223. {
  224. $exceptions = [];
  225. $e = $this;
  226. while ($e = $e->getPrevious()) {
  227. $exceptions[] = $e;
  228. }
  229. return $exceptions;
  230. }
  231. public function getTrace(): array
  232. {
  233. return $this->trace;
  234. }
  235. /**
  236. * @return $this
  237. */
  238. public function setTraceFromThrowable(\Throwable $throwable): self
  239. {
  240. $this->traceAsString = $throwable->getTraceAsString();
  241. return $this->setTrace($throwable->getTrace(), $throwable->getFile(), $throwable->getLine());
  242. }
  243. /**
  244. * @return $this
  245. */
  246. public function setTrace(array $trace, ?string $file, ?int $line): self
  247. {
  248. $this->trace = [];
  249. $this->trace[] = [
  250. 'namespace' => '',
  251. 'short_class' => '',
  252. 'class' => '',
  253. 'type' => '',
  254. 'function' => '',
  255. 'file' => $file,
  256. 'line' => $line,
  257. 'args' => [],
  258. ];
  259. foreach ($trace as $entry) {
  260. $class = '';
  261. $namespace = '';
  262. if (isset($entry['class'])) {
  263. $parts = explode('\\', $entry['class']);
  264. $class = array_pop($parts);
  265. $namespace = implode('\\', $parts);
  266. }
  267. $this->trace[] = [
  268. 'namespace' => $namespace,
  269. 'short_class' => $class,
  270. 'class' => $entry['class'] ?? '',
  271. 'type' => $entry['type'] ?? '',
  272. 'function' => $entry['function'] ?? null,
  273. 'file' => $entry['file'] ?? null,
  274. 'line' => $entry['line'] ?? null,
  275. 'args' => isset($entry['args']) ? $this->flattenArgs($entry['args']) : [],
  276. ];
  277. }
  278. return $this;
  279. }
  280. private function flattenArgs(array $args, int $level = 0, int &$count = 0): array
  281. {
  282. $result = [];
  283. foreach ($args as $key => $value) {
  284. if (++$count > 1e4) {
  285. return ['array', '*SKIPPED over 10000 entries*'];
  286. }
  287. if ($value instanceof \__PHP_Incomplete_Class) {
  288. $result[$key] = ['incomplete-object', $this->getClassNameFromIncomplete($value)];
  289. } elseif (\is_object($value)) {
  290. $result[$key] = ['object', \get_class($value)];
  291. } elseif (\is_array($value)) {
  292. if ($level > 10) {
  293. $result[$key] = ['array', '*DEEP NESTED ARRAY*'];
  294. } else {
  295. $result[$key] = ['array', $this->flattenArgs($value, $level + 1, $count)];
  296. }
  297. } elseif (null === $value) {
  298. $result[$key] = ['null', null];
  299. } elseif (\is_bool($value)) {
  300. $result[$key] = ['boolean', $value];
  301. } elseif (\is_int($value)) {
  302. $result[$key] = ['integer', $value];
  303. } elseif (\is_float($value)) {
  304. $result[$key] = ['float', $value];
  305. } elseif (\is_resource($value)) {
  306. $result[$key] = ['resource', get_resource_type($value)];
  307. } else {
  308. $result[$key] = ['string', (string) $value];
  309. }
  310. }
  311. return $result;
  312. }
  313. private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value): string
  314. {
  315. $array = new \ArrayObject($value);
  316. return $array['__PHP_Incomplete_Class_Name'];
  317. }
  318. public function getTraceAsString(): string
  319. {
  320. return $this->traceAsString;
  321. }
  322. /**
  323. * @return $this
  324. */
  325. public function setAsString(?string $asString): self
  326. {
  327. $this->asString = $asString;
  328. return $this;
  329. }
  330. public function getAsString(): string
  331. {
  332. if (null !== $this->asString) {
  333. return $this->asString;
  334. }
  335. $message = '';
  336. $next = false;
  337. foreach (array_reverse(array_merge([$this], $this->getAllPrevious())) as $exception) {
  338. if ($next) {
  339. $message .= 'Next ';
  340. } else {
  341. $next = true;
  342. }
  343. $message .= $exception->getClass();
  344. if ('' != $exception->getMessage()) {
  345. $message .= ': '.$exception->getMessage();
  346. }
  347. $message .= ' in '.$exception->getFile().':'.$exception->getLine().
  348. "\nStack trace:\n".$exception->getTraceAsString()."\n\n";
  349. }
  350. return rtrim($message);
  351. }
  352. }