Use X-REQUEST-ID for Error pages

This commit is contained in:
Philipp 2022-12-26 21:17:32 +01:00
parent 5584e7a4e5
commit 4f1bb0d274
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
55 changed files with 218 additions and 130 deletions

View file

@ -25,6 +25,7 @@ use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Module\BaseApi;
use Friendica\Module\Special\HTTPException;
use Friendica\Util\Network;
use Psr\Http\Message\ResponseInterface;
@ -33,9 +34,9 @@ use Psr\Http\Message\ResponseInterface;
*/
class Apps extends BaseApi
{
public function run(array $request = [], bool $scopecheck = true): ResponseInterface
public function run(HTTPException $httpException, array $request = [], bool $scopecheck = true): ResponseInterface
{
return parent::run($request, false);
return parent::run($httpException, $request, false);
}
/**

View file

@ -33,6 +33,7 @@ use Friendica\Model\Item;
use Friendica\Model\Post;
use Friendica\Model\User;
use Friendica\Module\Api\ApiResponse;
use Friendica\Module\Special\HTTPException as ModuleHTTPException;
use Friendica\Network\HTTPException;
use Friendica\Security\BasicAuth;
use Friendica\Security\OAuth;
@ -80,7 +81,7 @@ class BaseApi extends BaseModule
*
* @throws HTTPException\ForbiddenException
*/
public function run(array $request = [], bool $scopecheck = true): ResponseInterface
public function run(ModuleHTTPException $httpException, array $request = [], bool $scopecheck = true): ResponseInterface
{
if ($scopecheck) {
switch ($this->args->getMethod()) {
@ -97,7 +98,7 @@ class BaseApi extends BaseModule
}
}
return parent::run($request);
return parent::run($httpException, $request);
}
/**

View file

@ -22,13 +22,12 @@
namespace Friendica\Module\HTTPException;
use Friendica\BaseModule;
use Friendica\DI;
use Friendica\Network\HTTPException;
class MethodNotAllowed extends BaseModule
{
protected function content(array $request = []): string
{
throw new HTTPException\MethodNotAllowedException(DI::l10n()->t('Method Not Allowed.'));
throw new HTTPException\MethodNotAllowedException($this->t('Method Not Allowed.'));
}
}

View file

@ -25,8 +25,8 @@ use Friendica\App;
use Friendica\BaseModule;
use Friendica\Core\L10n;
use Friendica\Core\System;
use Friendica\DI;
use Friendica\Module\Response;
use Friendica\Module\Special\HTTPException as ModuleHTTPException;
use Friendica\Network\HTTPException;
use Friendica\Util\Profiler;
use Psr\Http\Message\ResponseInterface;
@ -46,10 +46,10 @@ class PageNotFound extends BaseModule
protected function content(array $request = []): string
{
throw new HTTPException\NotFoundException(DI::l10n()->t('Page not found.'));
throw new HTTPException\NotFoundException($this->t('Page not found.'));
}
public function run(array $request = []): ResponseInterface
public function run(ModuleHTTPException $httpException, array $request = []): ResponseInterface
{
/* The URL provided does not resolve to a valid module.
*
@ -77,6 +77,6 @@ class PageNotFound extends BaseModule
'query' => $this->server['QUERY_STRING']
]);
return parent::run($request); // TODO: Change the autogenerated stub
return parent::run($httpException, $request);
}
}

View file

@ -24,6 +24,7 @@ namespace Friendica\Module\OAuth;
use Friendica\Core\Renderer;
use Friendica\DI;
use Friendica\Module\BaseApi;
use Friendica\Module\Special\HTTPException;
use Psr\Http\Message\ResponseInterface;
/**
@ -31,9 +32,9 @@ use Psr\Http\Message\ResponseInterface;
*/
class Acknowledge extends BaseApi
{
public function run(array $request = [], bool $scopecheck = true): ResponseInterface
public function run(HTTPException $httpException, array $request = [], bool $scopecheck = true): ResponseInterface
{
return parent::run($request, false);
return parent::run($httpException, $request, false);
}
protected function post(array $request = [])

View file

@ -26,6 +26,7 @@ use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Module\BaseApi;
use Friendica\Module\Special\HTTPException;
use Psr\Http\Message\ResponseInterface;
/**
@ -33,9 +34,9 @@ use Psr\Http\Message\ResponseInterface;
*/
class Revoke extends BaseApi
{
public function run(array $request = [], bool $scopecheck = true): ResponseInterface
public function run(HTTPException $httpException, array $request = [], bool $scopecheck = true): ResponseInterface
{
return parent::run($request, false);
return parent::run($httpException, $request, false);
}
protected function post(array $request = [])

View file

@ -26,6 +26,7 @@ use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Module\BaseApi;
use Friendica\Module\Special\HTTPException;
use Friendica\Security\OAuth;
use Friendica\Util\DateTimeFormat;
use Psr\Http\Message\ResponseInterface;
@ -36,9 +37,9 @@ use Psr\Http\Message\ResponseInterface;
*/
class Token extends BaseApi
{
public function run(array $request = [], bool $scopecheck = true): ResponseInterface
public function run(HTTPException $httpException, array $request = [], bool $scopecheck = true): ResponseInterface
{
return parent::run($request, false);
return parent::run($httpException, $request, false);
}
protected function post(array $request = [])

View file

@ -21,10 +21,13 @@
namespace Friendica\Module\Special;
use Friendica\Core\Logger;
use Friendica\App\Arguments;
use Friendica\App\Request;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Core\Session\Model\UserSession;
use Friendica\Core\System;
use Friendica\DI;
use Psr\Log\LoggerInterface;
/**
* This special module displays HTTPException when they are thrown in modules.
@ -33,27 +36,52 @@ use Friendica\DI;
*/
class HTTPException
{
/** @var L10n */
protected $l10n;
/** @var LoggerInterface */
protected $logger;
/** @var Arguments */
protected $args;
/** @var bool */
protected $isSiteAdmin;
/** @var array */
protected $server;
/** @var string */
protected $requestId;
public function __construct(L10n $l10n, LoggerInterface $logger, Arguments $args, UserSession $session, Request $request, array $server = [])
{
$this->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.
*
* @param \Friendica\Network\HTTPException $e
*
* @return array ['$title' => ..., '$description' => ...]
*/
private static function getVars(\Friendica\Network\HTTPException $e)
private function getVars(\Friendica\Network\HTTPException $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' => DI::l10n()->t('Go back'),
'$stack_trace' => DI::l10n()->t('Stack trace:'),
'$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 (DI::app()->isSiteAdmin()) {
$vars['$thrown'] = DI::l10n()->t('Exception thrown in %s:%d', $e->getFile(), $e->getLine());
$vars['$trace'] = $e->getTraceAsString();
if ($this->isSiteAdmin) {
$vars['$thrown'] = $this->l10n->t('Exception thrown in %s:%d', $e->getFile(), $e->getLine());
$vars['$trace'] = $e->getTraceAsString();
}
return $vars;
@ -63,6 +91,7 @@ class HTTPException
* Displays a bare message page with no theming at all.
*
* @param \Friendica\Network\HTTPException $e
*
* @throws \Exception
*/
public function rawContent(\Friendica\Network\HTTPException $e)
@ -70,13 +99,13 @@ class HTTPException
$content = '';
if ($e->getCode() >= 400) {
$vars = self::getVars($e);
$vars = $this->getVars($e);
try {
$tpl = Renderer::getMarkupTemplate('http_status.tpl');
$tpl = Renderer::getMarkupTemplate('http_status.tpl');
$content = Renderer::replaceMacros($tpl, $vars);
} catch (\Exception $e) {
$content = "<h1>{$vars['$title']}</h1><p>{$vars['$message']}</p>";
if (DI::app()->isSiteAdmin()) {
if ($this->isSiteAdmin) {
$content .= "<p>{$vars['$thrown']}</p>";
$content .= "<pre>{$vars['$trace']}</pre>";
}
@ -90,19 +119,28 @@ class HTTPException
* Returns a content string that can be integrated in the current theme.
*
* @param \Friendica\Network\HTTPException $e
*
* @return string
* @throws \Exception
*/
public function content(\Friendica\Network\HTTPException $e): string
{
header($_SERVER["SERVER_PROTOCOL"] . ' ' . $e->getCode() . ' ' . $e->getDescription());
header($this->server['SERVER_PROTOCOL'] ?? 'HTTP/1.0' . ' ' . $e->getCode() . ' ' . $e->getDescription());
if ($e->getCode() >= 400) {
Logger::debug('Exit with error', ['code' => $e->getCode(), 'description' => $e->getDescription(), 'query' => DI::args()->getQueryString(), 'callstack' => System::callstack(20), 'method' => DI::args()->getMethod(), 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
$this->logger->debug('Exit with error',
[
'code' => $e->getCode(),
'description' => $e->getDescription(),
'query' => $this->args->getQueryString(),
'callstack' => System::callstack(20),
'method' => $this->args->getMethod(),
'agent' => $this->server['HTTP_USER_AGENT'] ?? ''
]);
}
$tpl = Renderer::getMarkupTemplate('exception.tpl');
return Renderer::replaceMacros($tpl, self::getVars($e));
return Renderer::replaceMacros($tpl, $this->getVars($e));
}
}