FlattenException.php 10 KB

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