Server exception error pages by default with a 500 status code

Signed-off-by: Morris Jobke <hey@morrisjobke.de>
This commit is contained in:
Morris Jobke 2018-06-26 10:27:43 +02:00
parent 8c155cd51c
commit 1399f6bece
No known key found for this signature in database
GPG key ID: FE03C3A163FEDE68
5 changed files with 14 additions and 44 deletions

View file

@ -45,8 +45,7 @@ try {
\OC::$server->getLogger()->logException($ex, array('app' => 'index'));
//show the user a detailed error page
OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE);
OC_Template::printExceptionErrorPage($ex);
OC_Template::printExceptionErrorPage($ex, \OC_Response::STATUS_SERVICE_UNAVAILABLE);
} catch (\OC\HintException $ex) {
try {
OC_Template::printErrorPage($ex->getMessage(), $ex->getHint(), OC_Response::STATUS_SERVICE_UNAVAILABLE);
@ -55,8 +54,7 @@ try {
\OC::$server->getLogger()->logException($ex2, array('app' => 'index'));
//show the user a detailed error page
OC_Response::setStatus(OC_Response::STATUS_INTERNAL_SERVER_ERROR);
OC_Template::printExceptionErrorPage($ex);
OC_Template::printExceptionErrorPage($ex, \OC_Response::STATUS_INTERNAL_SERVER_ERROR);
}
} catch (\OC\User\LoginException $ex) {
OC_Template::printErrorPage($ex->getMessage(), $ex->getMessage(), OC_Response::STATUS_FORBIDDEN);
@ -90,6 +88,5 @@ try {
throw $e;
}
OC_Response::setStatus(OC_Response::STATUS_INTERNAL_SERVER_ERROR);
OC_Template::printExceptionErrorPage($ex);
OC_Template::printExceptionErrorPage($ex, \OC_Response::STATUS_INTERNAL_SERVER_ERROR);
}

View file

@ -434,8 +434,7 @@ class OC {
} catch (Exception $e) {
\OC::$server->getLogger()->logException($e, ['app' => 'base']);
//show the user a detailed error page
OC_Response::setStatus(OC_Response::STATUS_INTERNAL_SERVER_ERROR);
OC_Template::printExceptionErrorPage($e);
OC_Template::printExceptionErrorPage($e, \OC_Response::STATUS_INTERNAL_SERVER_ERROR);
die();
}

View file

@ -304,9 +304,10 @@ class OC_Template extends \OC\Template\Base {
* Print a fatal error page and terminates the script
* @param string $error_msg The error message to show
* @param string $hint An optional hint message - needs to be properly escape
* @param int $statusCode
* @suppress PhanAccessMethodInternal
*/
public static function printErrorPage( $error_msg, $hint = '', $statusCode = \OC_Response::STATUS_INTERNAL_SERVER_ERROR ) {
public static function printErrorPage( $error_msg, $hint = '', $statusCode = 500) {
if (\OC::$server->getAppManager()->isEnabledForUser('theming') && !\OC_App::isAppLoaded('theming')) {
\OC_App::loadApp('theming');
}
@ -337,11 +338,12 @@ class OC_Template extends \OC\Template\Base {
/**
* print error page using Exception details
* @param Exception|Throwable $exception
* @param bool $fetchPage
* @param int $statusCode
* @return bool|string
* @suppress PhanAccessMethodInternal
*/
public static function printExceptionErrorPage($exception, $fetchPage = false) {
public static function printExceptionErrorPage($exception, $statusCode = 503) {
http_response_code($statusCode);
try {
$request = \OC::$server->getRequest();
$content = new \OC_Template('', 'exception', 'error', false);
@ -354,16 +356,12 @@ class OC_Template extends \OC\Template\Base {
$content->assign('debugMode', \OC::$server->getSystemConfig()->getValue('debug', false));
$content->assign('remoteAddr', $request->getRemoteAddress());
$content->assign('requestID', $request->getId());
if ($fetchPage) {
return $content->fetchPage();
}
$content->printPage();
} catch (\Exception $e) {
$logger = \OC::$server->getLogger();
$logger->logException($exception, ['app' => 'core']);
$logger->logException($e, ['app' => 'core']);
header(self::getHttpProtocol() . ' 500 Internal Server Error');
header('Content-Type: text/plain; charset=utf-8');
print("Internal Server Error\n\n");
print("The server encountered an internal error and was unable to complete your request.\n");
@ -372,26 +370,4 @@ class OC_Template extends \OC\Template\Base {
}
die();
}
/**
* This is only here to reduce the dependencies in case of an exception to
* still be able to print a plain error message.
*
* Returns the used HTTP protocol.
*
* @return string HTTP protocol. HTTP/2, HTTP/1.1 or HTTP/1.0.
* @internal Don't use this - use AppFramework\Http\Request->getHttpProtocol instead
*/
protected static function getHttpProtocol() {
$claimedProtocol = strtoupper($_SERVER['SERVER_PROTOCOL']);
$validProtocols = [
'HTTP/1.0',
'HTTP/1.1',
'HTTP/2',
];
if(in_array($claimedProtocol, $validProtocols, true)) {
return $claimedProtocol;
}
return 'HTTP/1.1';
}
}

View file

@ -79,16 +79,15 @@ try {
} catch (Exception $ex) {
if ($ex instanceof \OC\ServiceUnavailableException) {
OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE);
$status = OC_Response::STATUS_SERVICE_UNAVAILABLE;
} else {
OC_Response::setStatus(OC_Response::STATUS_INTERNAL_SERVER_ERROR);
$status = OC_Response::STATUS_INTERNAL_SERVER_ERROR;
}
//show the user a detailed error page
\OC::$server->getLogger()->logException($ex, ['app' => 'public']);
OC_Template::printExceptionErrorPage($ex);
OC_Template::printExceptionErrorPage($ex, $status);
} catch (Error $ex) {
//show the user a detailed error page
OC_Response::setStatus(OC_Response::STATUS_INTERNAL_SERVER_ERROR);
\OC::$server->getLogger()->logException($ex, ['app' => 'public']);
OC_Template::printExceptionErrorPage($ex);
OC_Template::printExceptionErrorPage($ex, OC_Response::STATUS_INTERNAL_SERVER_ERROR);
}

View file

@ -80,8 +80,7 @@ function handleException($e) {
OC_Template::printErrorPage($e->getMessage(), '', $e->getCode());
} else {
\OC::$server->getLogger()->logException($e, ['app' => 'remote']);
OC_Response::setStatus($statusCode);
OC_Template::printExceptionErrorPage($e);
OC_Template::printExceptionErrorPage($e, $statusCode);
}
}
}