1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-26 00:32:42 +01:00

Catch unhandled exceptions in index.php

Summary:
When there is an exception in `index.php` then we currently get only blank screen.
Print it instead.

Test Plan: Thrown exceptions on several places of `index.php` and controller, got best results.

Reviewers: epriestley, btrahan

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D3619
This commit is contained in:
vrana 2012-10-03 23:54:24 -07:00
parent 58206a146f
commit 4a2bcc06fe

View file

@ -109,156 +109,157 @@ try {
PhutilErrorHandler::initialize(); PhutilErrorHandler::initialize();
} catch (Exception $ex) { PhutilErrorHandler::setErrorListener(
phabricator_fatal("[Initialization Exception] ".$ex->getMessage()); array('DarkConsoleErrorLogPluginAPI', 'handleErrors'));
}
PhutilErrorHandler::setErrorListener( foreach (PhabricatorEnv::getEnvConfig('load-libraries') as $library) {
array('DarkConsoleErrorLogPluginAPI', 'handleErrors')); phutil_load_library($library);
foreach (PhabricatorEnv::getEnvConfig('load-libraries') as $library) {
phutil_load_library($library);
}
if (PhabricatorEnv::getEnvConfig('phabricator.setup')) {
try {
PhabricatorSetup::runSetup();
} catch (Exception $ex) {
echo "EXCEPTION!\n";
echo $ex;
} }
return;
}
phabricator_detect_bad_base_uri(); if (PhabricatorEnv::getEnvConfig('phabricator.setup')) {
try {
$translation = PhabricatorEnv::newObjectFromConfig('translation.provider'); PhabricatorSetup::runSetup();
PhutilTranslator::getInstance() } catch (Exception $ex) {
->setLanguage($translation->getLanguage()) echo "EXCEPTION!\n";
->addTranslations($translation->getTranslations()); echo $ex;
$host = $_SERVER['HTTP_HOST'];
$path = $_REQUEST['__path__'];
switch ($host) {
default:
$config_key = 'aphront.default-application-configuration-class';
$application = PhabricatorEnv::newObjectFromConfig($config_key);
break;
}
$application->setHost($host);
$application->setPath($path);
$application->willBuildRequest();
$request = $application->buildRequest();
$write_guard = new AphrontWriteGuard(array($request, 'validateCSRF'));
PhabricatorEventEngine::initialize();
$application->setRequest($request);
list($controller, $uri_data) = $application->buildController();
if ($access_log) {
$access_log->setData(
array(
'U' => (string)$request->getRequestURI()->getPath(),
'C' => get_class($controller),
));
}
// If execution throws an exception and then trying to render that exception
// throws another exception, we want to show the original exception, as it is
// likely the root cause of the rendering exception.
$original_exception = null;
try {
$response = $controller->willBeginExecution();
if ($access_log) {
if ($request->getUser() && $request->getUser()->getPHID()) {
$access_log->setData(
array(
'u' => $request->getUser()->getUserName(),
));
} }
return;
} }
if (!$response) { phabricator_detect_bad_base_uri();
$controller->willProcessRequest($uri_data);
$response = $controller->processRequest(); $translation = PhabricatorEnv::newObjectFromConfig('translation.provider');
} PhutilTranslator::getInstance()
} catch (AphrontRedirectException $ex) { ->setLanguage($translation->getLanguage())
$response = id(new AphrontRedirectResponse()) ->addTranslations($translation->getTranslations());
->setURI($ex->getURI());
} catch (Exception $ex) { $host = $_SERVER['HTTP_HOST'];
$original_exception = $ex; $path = $_REQUEST['__path__'];
$response = $application->handleException($ex);
} switch ($host) {
default:
$config_key = 'aphront.default-application-configuration-class';
$application = PhabricatorEnv::newObjectFromConfig($config_key);
break;
}
$application->setHost($host);
$application->setPath($path);
$application->willBuildRequest();
$request = $application->buildRequest();
$write_guard = new AphrontWriteGuard(array($request, 'validateCSRF'));
PhabricatorEventEngine::initialize();
$application->setRequest($request);
list($controller, $uri_data) = $application->buildController();
try {
$response = $controller->didProcessRequest($response);
$response = $application->willSendResponse($response, $controller);
$response->setRequest($request);
$response_string = $response->buildResponseString();
} catch (Exception $ex) {
$write_guard->dispose();
if ($access_log) { if ($access_log) {
$access_log->write(); $access_log->setData(
}
if ($original_exception) {
$ex = new PhutilAggregateException(
"Multiple exceptions during processing and rendering.",
array( array(
$original_exception, 'U' => (string)$request->getRequestURI()->getPath(),
$ex, 'C' => get_class($controller),
)); ));
} }
phabricator_fatal('[Rendering Exception] '.$ex->getMessage());
}
$write_guard->dispose(); // If execution throws an exception and then trying to render that exception
// throws another exception, we want to show the original exception, as it is
// likely the root cause of the rendering exception.
$original_exception = null;
try {
$response = $controller->willBeginExecution();
// TODO: Share the $sink->writeResponse() pathway here? if ($access_log) {
if ($request->getUser() && $request->getUser()->getPHID()) {
$access_log->setData(
array(
'u' => $request->getUser()->getUserName(),
));
}
}
$sink = new AphrontPHPHTTPSink(); if (!$response) {
$sink->writeHTTPStatus($response->getHTTPResponseCode()); $controller->willProcessRequest($uri_data);
$response = $controller->processRequest();
$headers = $response->getCacheHeaders(); }
$headers = array_merge($headers, $response->getHeaders()); } catch (AphrontRedirectException $ex) {
$response = id(new AphrontRedirectResponse())
$sink->writeHeaders($headers); ->setURI($ex->getURI());
} catch (Exception $ex) {
$sink->writeData($response_string); $original_exception = $ex;
$response = $application->handleException($ex);
if ($access_log) {
$access_log->setData(
array(
'c' => $response->getHTTPResponseCode(),
'T' => (int)(1000000 * (microtime(true) - $__start__)),
));
$access_log->write();
}
if (DarkConsoleXHProfPluginAPI::isProfilerRequested()) {
$profile = DarkConsoleXHProfPluginAPI::stopProfiler();
$profile_sample = id(new PhabricatorXHProfSample())
->setFilePHID($profile);
if (empty($_REQUEST['__profile__'])) {
$sample_rate = PhabricatorEnv::getEnvConfig('debug.profile-rate');
} else {
$sample_rate = 0;
} }
$profile_sample->setSampleRate($sample_rate);
try {
$response = $controller->didProcessRequest($response);
$response = $application->willSendResponse($response, $controller);
$response->setRequest($request);
$response_string = $response->buildResponseString();
} catch (Exception $ex) {
$write_guard->dispose();
if ($access_log) {
$access_log->write();
}
if ($original_exception) {
$ex = new PhutilAggregateException(
"Multiple exceptions during processing and rendering.",
array(
$original_exception,
$ex,
));
}
phabricator_fatal('[Rendering Exception] '.$ex->getMessage());
}
$write_guard->dispose();
// TODO: Share the $sink->writeResponse() pathway here?
$sink = new AphrontPHPHTTPSink();
$sink->writeHTTPStatus($response->getHTTPResponseCode());
$headers = $response->getCacheHeaders();
$headers = array_merge($headers, $response->getHeaders());
$sink->writeHeaders($headers);
$sink->writeData($response_string);
if ($access_log) { if ($access_log) {
$profile_sample->setUsTotal($access_log->getData('T')) $access_log->setData(
->setHostname($access_log->getData('h')) array(
->setRequestPath($access_log->getData('U')) 'c' => $response->getHTTPResponseCode(),
->setController($access_log->getData('C')) 'T' => (int)(1000000 * (microtime(true) - $__start__)),
->setUserPHID($request->getUser()->getPHID()); ));
$access_log->write();
} }
$profile_sample->save();
if (DarkConsoleXHProfPluginAPI::isProfilerRequested()) {
$profile = DarkConsoleXHProfPluginAPI::stopProfiler();
$profile_sample = id(new PhabricatorXHProfSample())
->setFilePHID($profile);
if (empty($_REQUEST['__profile__'])) {
$sample_rate = PhabricatorEnv::getEnvConfig('debug.profile-rate');
} else {
$sample_rate = 0;
}
$profile_sample->setSampleRate($sample_rate);
if ($access_log) {
$profile_sample->setUsTotal($access_log->getData('T'))
->setHostname($access_log->getData('h'))
->setRequestPath($access_log->getData('U'))
->setController($access_log->getData('C'))
->setUserPHID($request->getUser()->getPHID());
}
$profile_sample->save();
}
} catch (Exception $ex) {
phabricator_fatal("[Exception] ".$ex->getMessage());
} }
/** /**
* @group aphront * @group aphront
*/ */
@ -352,11 +353,15 @@ function phabricator_fatal($msg) {
global $access_log; global $access_log;
if ($access_log) { if ($access_log) {
$access_log->setData( try {
array( $access_log->setData(
'c' => 500, array(
)); 'c' => 500,
$access_log->write(); ));
$access_log->write();
} catch (Exception $ex) {
$msg .= "\nMoreover unable to write to access log.";
}
} }
header( header(