logger = $logger; $this->l10n = $l10n; $this->args = $args; $this->isSiteAdmin = $session->isSiteAdmin(); $this->server = $server; $this->requestId = $request->getRequestId(); } /** * Generates the necessary template variables from the caught HTTPException. * * Fills in the blanks if title or descriptions aren't provided by the exception. * * @return array ['$title' => ..., '$description' => ...] */ private function getVars(NetworkHTTPException $e) { // Explanations are mostly taken from https://en.wikipedia.org/wiki/List_of_HTTP_status_codes $vars = [ '$title' => $e->getDescription() ?: 'Error ' . $e->getCode(), '$message' => $e->getMessage() ?: $e->getExplanation(), '$back' => $this->l10n->t('Go back'), '$stack_trace' => $this->l10n->t('Stack trace:'), '$request_id' => $this->requestId, ]; if ($this->isSiteAdmin) { $vars['$thrown'] = $this->l10n->t('Exception thrown in %s:%d', $e->getFile(), $e->getLine()); $vars['$trace'] = $e->getTraceAsString(); } return $vars; } /** * Displays a bare message page with no theming at all. * * @throws \Exception */ public function rawContent(NetworkHTTPException $e) { $content = ''; if ($e->getCode() >= 400) { $vars = $this->getVars($e); try { $tpl = Renderer::getMarkupTemplate('http_status.tpl'); $content = Renderer::replaceMacros($tpl, $vars); } catch (\Exception $e) { $vars = array_map('htmlentities', $vars); $content = "
{$vars['$message']}
"; if ($this->isSiteAdmin) { $content .= "{$vars['$thrown']}
"; $content .= "{$vars['$trace']}";
}
}
}
// We can't use a constructor parameter for this response object because we
// are in an Exception context where we don't want an existing Response.
$reason = ($e instanceof NetworkHTTPException) ? $e->getDescription() : $e->getMessage();
$response = new Response();
$response->setStatus($e->getCode(), $reason);
$response->addContent($content);
System::echoResponse($response->generate());
System::exit();
}
/**
* Returns a content string that can be integrated in the current theme.
*
* @return string
* @throws \Exception
*/
public function content(NetworkHTTPException $e): string
{
if ($e->getCode() >= 400) {
$this->logger->debug('Exit with error',
[
'code' => $e->getCode(),
'description' => $e->getDescription(),
'query' => $this->args->getQueryString(),
'method' => $this->args->getMethod(),
'agent' => $this->server['HTTP_USER_AGENT'] ?? ''
]);
}
$tpl = Renderer::getMarkupTemplate('exception.tpl');
return Renderer::replaceMacros($tpl, $this->getVars($e));
}
}