FlattenException.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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_class($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 = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_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, "class@anonymous\0")) {
  168. $message = preg_replace_callback('/class@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', function ($m) {
  169. return class_exists($m[0], false) ? get_parent_class($m[0]).'@anonymous' : $m[0];
  170. }, $message);
  171. }
  172. $this->message = $message;
  173. return $this;
  174. }
  175. public function getCode(): int
  176. {
  177. return $this->code;
  178. }
  179. /**
  180. * @return $this
  181. */
  182. public function setCode($code): self
  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): self
  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): self
  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($trace, $file, $line): self
  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' => isset($entry['class']) ? $entry['class'] : '',
  251. 'type' => isset($entry['type']) ? $entry['type'] : '',
  252. 'function' => isset($entry['function']) ? $entry['function'] : null,
  253. 'file' => isset($entry['file']) ? $entry['file'] : null,
  254. 'line' => isset($entry['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. // is_object() returns false on PHP<=7.1
  269. $result[$key] = ['incomplete-object', $this->getClassNameFromIncomplete($value)];
  270. } elseif (\is_object($value)) {
  271. $result[$key] = ['object', \get_class($value)];
  272. } elseif (\is_array($value)) {
  273. if ($level > 10) {
  274. $result[$key] = ['array', '*DEEP NESTED ARRAY*'];
  275. } else {
  276. $result[$key] = ['array', $this->flattenArgs($value, $level + 1, $count)];
  277. }
  278. } elseif (null === $value) {
  279. $result[$key] = ['null', null];
  280. } elseif (\is_bool($value)) {
  281. $result[$key] = ['boolean', $value];
  282. } elseif (\is_int($value)) {
  283. $result[$key] = ['integer', $value];
  284. } elseif (\is_float($value)) {
  285. $result[$key] = ['float', $value];
  286. } elseif (\is_resource($value)) {
  287. $result[$key] = ['resource', get_resource_type($value)];
  288. } else {
  289. $result[$key] = ['string', (string) $value];
  290. }
  291. }
  292. return $result;
  293. }
  294. private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value): string
  295. {
  296. $array = new \ArrayObject($value);
  297. return $array['__PHP_Incomplete_Class_Name'];
  298. }
  299. public function getTraceAsString(): string
  300. {
  301. return $this->traceAsString;
  302. }
  303. /**
  304. * @return $this
  305. */
  306. public function setAsString(?string $asString): self
  307. {
  308. $this->asString = $asString;
  309. return $this;
  310. }
  311. public function getAsString(): string
  312. {
  313. if (null !== $this->asString) {
  314. return $this->asString;
  315. }
  316. $message = '';
  317. $next = false;
  318. foreach (array_reverse(array_merge([$this], $this->getAllPrevious())) as $exception) {
  319. if ($next) {
  320. $message .= 'Next ';
  321. } else {
  322. $next = true;
  323. }
  324. $message .= $exception->getClass();
  325. if ('' != $exception->getMessage()) {
  326. $message .= ': '.$exception->getMessage();
  327. }
  328. $message .= ' in '.$exception->getFile().':'.$exception->getLine().
  329. "\nStack trace:\n".$exception->getTraceAsString()."\n\n";
  330. }
  331. return rtrim($message);
  332. }
  333. }