Path: blob/master/src/aphront/response/AphrontUnhandledExceptionResponse.php
12241 views
<?php12final class AphrontUnhandledExceptionResponse3extends AphrontStandaloneHTMLResponse {45private $exception;6private $showStackTraces;78public function setShowStackTraces($show_stack_traces) {9$this->showStackTraces = $show_stack_traces;10return $this;11}1213public function getShowStackTraces() {14return $this->showStackTraces;15}1617public function setException($exception) {18// NOTE: We accept an Exception or a Throwable.1920// Log the exception unless it's specifically a silent malformed request21// exception.2223$should_log = true;24if ($exception instanceof AphrontMalformedRequestException) {25if ($exception->getIsUnlogged()) {26$should_log = false;27}28}2930if ($should_log) {31phlog($exception);32}3334$this->exception = $exception;35return $this;36}3738public function getHTTPResponseCode() {39return 500;40}4142protected function getResources() {43return array(44'css/application/config/config-template.css',45'css/application/config/unhandled-exception.css',46);47}4849protected function getResponseTitle() {50$ex = $this->exception;5152if ($ex instanceof AphrontMalformedRequestException) {53return $ex->getTitle();54} else {55return pht('Unhandled Exception');56}57}5859protected function getResponseBodyClass() {60return 'unhandled-exception';61}6263private function getExceptionList() {64return $this->expandException($this->exception);65}6667private function expandException($root) {68if ($root instanceof PhutilAggregateException) {69$list = array();7071$list[] = $root;7273foreach ($root->getExceptions() as $ex) {74foreach ($this->expandException($ex) as $child) {75$list[] = $child;76}77}7879return $list;80}8182return array($root);83}8485protected function getResponseBody() {86$body = array();8788foreach ($this->getExceptionList() as $ex) {89$body[] = $this->newHTMLMessage($ex);90}9192return $body;93}9495private function newHTMLMessage($ex) {96if ($ex instanceof AphrontMalformedRequestException) {97$title = $ex->getTitle();98} else {99$title = get_class($ex);100}101102$body = $ex->getMessage();103$body = phutil_escape_html_newlines($body);104105$classes = array();106$classes[] = 'unhandled-exception-detail';107108$stack = null;109if ($this->getShowStackTraces()) {110try {111$stack = id(new AphrontStackTraceView())112->setTrace($ex->getTrace());113114$stack = hsprintf('%s', $stack);115116$stack = phutil_tag(117'div',118array(119'class' => 'unhandled-exception-stack',120),121$stack);122123$classes[] = 'unhandled-exception-with-stack';124} catch (Exception $trace_exception) {125$stack = null;126} catch (Throwable $trace_exception) {127$stack = null;128}129}130131return phutil_tag(132'div',133array(134'class' => implode(' ', $classes),135),136array(137phutil_tag(138'h1',139array(140'class' => 'unhandled-exception-title',141),142$title),143phutil_tag(144'div',145array(146'class' => 'unhandled-exception-body',147),148$body),149$stack,150));151}152153protected function buildPlainTextResponseString() {154$messages = array();155156foreach ($this->getExceptionList() as $exception) {157$messages[] = $this->newPlainTextMessage($exception);158}159160return implode("\n\n", $messages);161}162163private function newPlainTextMessage($exception) {164return pht(165'%s: %s',166get_class($exception),167$exception->getMessage());168}169170}171172173