1
0
Fork 0

Improved http error handling

This commit is contained in:
Michael 2021-10-29 23:21:07 +00:00
commit 4236a9a105
55 changed files with 282 additions and 135 deletions

View file

@ -21,7 +21,7 @@
namespace Friendica\Core\Cache;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Network\HTTPException\ServiceUnavailableException;
/**
* Trait for Memcache to add a custom version of the
@ -52,7 +52,7 @@ trait TraitMemcacheCommand
*
* @return array All keys of the memcache instance
*
* @throws InternalServerErrorException
* @throws ServiceUnavailableException
*/
protected function getMemcacheKeys()
{
@ -88,13 +88,13 @@ trait TraitMemcacheCommand
*
* @return string The returned buffer result
*
* @throws InternalServerErrorException In case the memcache server isn't available (anymore)
* @throws ServiceUnavailableException In case the memcache server isn't available (anymore)
*/
protected function sendMemcacheCommand(string $command)
{
$s = @fsockopen($this->server, $this->port);
if (!$s) {
throw new InternalServerErrorException("Cant connect to:" . $this->server . ':' . $this->port);
throw new ServiceUnavailableException("Cant connect to:" . $this->server . ':' . $this->port);
}
fwrite($s, $command . "\r\n");

View file

@ -23,7 +23,7 @@ namespace Friendica\Core;
use Exception;
use Friendica\DI;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Network\HTTPException\ServiceUnavailableException;
use Friendica\Render\TemplateEngine;
/**
@ -69,7 +69,7 @@ class Renderer
* @param string $template
* @param array $vars
* @return string
* @throws InternalServerErrorException
* @throws ServiceUnavailableException
*/
public static function replaceMacros(string $template, array $vars = [])
{
@ -87,7 +87,7 @@ class Renderer
$message = is_site_admin() ?
$e->getMessage() :
DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
throw new InternalServerErrorException($message);
throw new ServiceUnavailableException($message);
}
DI::profiler()->stopRecording();
@ -102,7 +102,7 @@ class Renderer
* @param string $subDir Subdirectory (Optional)
*
* @return string template.
* @throws InternalServerErrorException
* @throws ServiceUnavailableException
*/
public static function getMarkupTemplate($file, $subDir = '')
{
@ -116,7 +116,7 @@ class Renderer
$message = is_site_admin() ?
$e->getMessage() :
DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
throw new InternalServerErrorException($message);
throw new ServiceUnavailableException($message);
}
DI::profiler()->stopRecording();
@ -128,7 +128,7 @@ class Renderer
* Register template engine class
*
* @param string $class
* @throws InternalServerErrorException
* @throws ServiceUnavailableException
*/
public static function registerTemplateEngine($class)
{
@ -143,7 +143,7 @@ class Renderer
$message = is_site_admin() ?
$admin_message :
DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
throw new InternalServerErrorException($message);
throw new ServiceUnavailableException($message);
}
}
@ -154,7 +154,7 @@ class Renderer
* or default
*
* @return TemplateEngine Template Engine instance
* @throws InternalServerErrorException
* @throws ServiceUnavailableException
*/
public static function getTemplateEngine()
{
@ -177,7 +177,7 @@ class Renderer
$message = is_site_admin() ?
$admin_message :
DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
throw new InternalServerErrorException($message);
throw new ServiceUnavailableException($message);
}
/**

View file

@ -22,7 +22,10 @@
namespace Friendica\Core;
use Friendica\DI;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Network\HTTPException\BadRequestException;
use Friendica\Network\HTTPException\FoundException;
use Friendica\Network\HTTPException\MovedPermanentlyException;
use Friendica\Network\HTTPException\TemporaryRedirectException;
use Friendica\Util\XML;
/**
@ -122,7 +125,9 @@ class System
*/
public static function httpExit($val, $message = '', $content = '')
{
Logger::log('http_status_exit ' . $val);
if ($val >= 400) {
Logger::debug('Exit with error', ['code' => $val, 'message' => $message, 'callstack' => System::callstack(20), 'method' => $_SERVER['REQUEST_METHOD'], 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
}
header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $message);
echo $content;
@ -132,6 +137,9 @@ class System
public static function jsonError($httpCode, $data, $content_type = 'application/json')
{
if ($httpCode >= 400) {
Logger::debug('Exit with error', ['code' => $httpCode, 'content_type' => $content_type, 'callstack' => System::callstack(20), 'method' => $_SERVER['REQUEST_METHOD'], 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
}
header($_SERVER["SERVER_PROTOCOL"] . ' ' . $httpCode);
self::jsonExit($data, $content_type);
}
@ -222,28 +230,25 @@ class System
* @param string $url The new Location to redirect
* @param int $code The redirection code, which is used (Default is 302)
*
* @throws InternalServerErrorException If the URL is not fully qualified
* @throws BadRequestException If the URL is not fully qualified
*/
public static function externalRedirect($url, $code = 302)
{
if (empty(parse_url($url, PHP_URL_SCHEME))) {
throw new InternalServerErrorException("'$url' is not a fully qualified URL, please use App->internalRedirect() instead");
}
switch ($code) {
case 302:
// this is the default code for a REDIRECT
// We don't need a extra header here
break;
case 301:
header('HTTP/1.1 301 Moved Permanently');
break;
case 307:
header('HTTP/1.1 307 Temporary Redirect');
break;
throw new BadRequestException("'$url' is not a fully qualified URL, please use App->internalRedirect() instead");
}
header("Location: $url");
switch ($code) {
case 302:
throw new FoundException();
case 301:
throw new MovedPermanentlyException();
case 307:
throw new TemporaryRedirectException();
}
exit();
}