FlattenException.php 10 KB

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