- This will ensure headers set in BaseModule->run will be carried in httpError scenarios
- Deprecate httpError() method in Core\System
System::exit();
}
+
+ /**
+ * Send HTTP status header and exit.
+ *
+ * @param integer $httpCode HTTP status result value
+ * @param string $message Error message. Optional.
+ * @param mixed $content Response body. Optional.
+ * @throws \Exception
+ */
+ public function httpError(int $httpCode, string $message = '', $content = '')
+ {
+ if ($httpCode >= 400) {
+ $this->logger->debug('Exit with error', ['code' => $httpCode, 'message' => $message, 'callstack' => System::callstack(20), 'method' => $this->args->getMethod(), 'agent' => $this->server['HTTP_USER_AGENT'] ?? '']);
+ }
+
+ $this->response->setStatus($httpCode, $message);
+
+ $this->httpExit($content);
+ }
}
* @param string $message Error message. Optional.
* @param string $content Response body. Optional.
* @throws \Exception
+ * @deprecated since 2023.09 Use BaseModule->httpError instead
*/
public static function httpError($httpCode, $message = '', $content = '')
{
Logger::debug('Exit with error', ['code' => $httpCode, 'message' => $message, 'callstack' => System::callstack(20), 'method' => DI::args()->getMethod(), 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '']);
}
DI::apiResponse()->setStatus($httpCode, $message);
- DI::apiResponse()->addContent($content);
- self::echoResponse(DI::apiResponse()->generate());
- self::exit();
+ self::httpExit($content);
}
/**
protected function post(array $request = [])
{
- System::httpError($this->removeTag($request));
+ $this->httpError($this->removeTag($request));
}
protected function content(array $request = []): string
{
$post_id = $this->parameters['post_id'];
if (!$post_id || !$this->session->getLocalUserId()) {
- System::httpError(403);
+ $this->httpError(403);
}
$item = Post::selectFirst(['private', 'body', 'uri', 'plink', 'network'], ['id' => $post_id]);
if (!$item || $item['private'] == Item::PRIVATE) {
- System::httpError(404);
+ $this->httpError(404);
}
$shared = $this->contentItem->getSharedPost($item, ['uri']);
namespace Friendica\Module\Special;
-use Friendica\App\Arguments;
-use Friendica\App\Request;
+use Friendica\App;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Core\Session\Model\UserSession;
use Friendica\Core\System;
+use Friendica\Module\Response;
use Psr\Log\LoggerInterface;
/**
protected $l10n;
/** @var LoggerInterface */
protected $logger;
- /** @var Arguments */
+ /** @var App\Arguments */
protected $args;
/** @var bool */
protected $isSiteAdmin;
/** @var string */
protected $requestId;
- public function __construct(L10n $l10n, LoggerInterface $logger, Arguments $args, UserSession $session, Request $request, array $server = [])
+ public function __construct(L10n $l10n, LoggerInterface $logger, App\Arguments $args, UserSession $session, App\Request $request, array $server = [])
{
$this->logger = $logger;
$this->l10n = $l10n;
}
}
- System::httpError($e->getCode(), $e->getDescription(), $content);
+ // 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.
+ $response = new Response();
+ $response->setStatus($e->getCode(), $e->getDescription());
+ $response->addContent($content);
+ System::echoResponse($response->generate());
+ System::exit();
}
/**