Merge pull request #14398 from annando/exceptions

Exception handling added at many places
This commit is contained in:
Tobias Diekershoff 2024-08-25 20:45:32 +02:00 committed by GitHub
commit 9bfeebaf03
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 174 additions and 44 deletions

View file

@ -220,7 +220,12 @@ class Search
$return = Contact::searchByName($search, $mode, true);
} else {
$p = $page > 1 ? 'p=' . $page : '';
try {
$curlResult = DI::httpClient()->get(self::getGlobalDirectory() . '/search/people?' . $p . '&q=' . urlencode($search), HttpClientAccept::JSON, [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTDISCOVER]);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return [];
}
if ($curlResult->isSuccess()) {
$searchResult = json_decode($curlResult->getBodyString(), true);
if (!empty($searchResult['profiles'])) {

View file

@ -588,7 +588,12 @@ class Photo
$filename = basename($image_url);
if (!empty($image_url)) {
try {
$ret = DI::httpClient()->get($image_url, HttpClientAccept::IMAGE, [HttpClientOptions::REQUEST => HttpClientRequest::MEDIAPROXY]);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return false;
}
Logger::debug('Got picture', ['Content-Type' => $ret->getHeader('Content-Type'), 'url' => $image_url]);
$img_str = $ret->getBodyString();
$type = $ret->getContentType();
@ -1043,7 +1048,12 @@ class Photo
{
$filename = basename($image_url);
if (!empty($image_url)) {
try {
$ret = DI::httpClient()->get($image_url, HttpClientAccept::IMAGE, [HttpClientOptions::REQUEST => HttpClientRequest::MEDIAPROXY]);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return [];
}
Logger::debug('Got picture', ['Content-Type' => $ret->getHeader('Content-Type'), 'url' => $image_url]);
$img_str = $ret->getBodyString();
$type = $ret->getContentType();

View file

@ -185,9 +185,8 @@ class Media
// Workaround for systems that can't handle a HEAD request
if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() == 405)) {
try {
$curlResult = DI::httpClient()->get($media['url'], HttpClientAccept::DEFAULT, [HttpClientOptions::TIMEOUT => $timeout]);
}
if ($curlResult->isSuccess()) {
if (empty($media['mimetype'])) {
$media['mimetype'] = $curlResult->getContentType() ?? '';
@ -198,6 +197,10 @@ class Media
} else {
Logger::notice('Could not fetch head', ['media' => $media]);
}
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
}
}
}
$filetype = !empty($media['mimetype']) ? strtolower(current(explode('/', $media['mimetype']))) : '';

View file

@ -1411,6 +1411,7 @@ class User
$photo_failure = false;
$filename = basename($photo);
try {
$curlResult = DI::httpClient()->get($photo, HttpClientAccept::IMAGE, [HttpClientOptions::REQUEST => HttpClientRequest::CONTENTTYPE]);
if ($curlResult->isSuccess()) {
Logger::debug('Got picture', ['Content-Type' => $curlResult->getHeader('Content-Type'), 'url' => $photo]);
@ -1420,6 +1421,11 @@ class User
$img_str = '';
$type = '';
}
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
$img_str = '';
$type = '';
}
$image = new Image($img_str, $type, $photo);
if ($image->isValid()) {

View file

@ -109,10 +109,20 @@ class MatchInterests extends BaseModule
continue;
}
try {
$result = $this->httpClient->post($server . '/search/user/tags', $searchParameters, [], 0, HttpClientRequest::CONTACTDISCOVER);
} catch (\Throwable $th) {
$this->logger->notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
continue;
}
if (!$result->isSuccess()) {
// try legacy endpoint
try {
$result = $this->httpClient->post($server . '/msearch', $searchParameters, [], 0, HttpClientRequest::CONTACTDISCOVER);
} catch (\Throwable $th) {
$this->logger->notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
continue;
}
if (!$result->isSuccess()) {
$this->logger->notice('Search-Endpoint not available for server.', ['server' => $server]);
continue;

View file

@ -214,7 +214,12 @@ class Probe
Logger::info('Probing', ['host' => $host, 'ssl_url' => $ssl_url, 'url' => $url]);
$xrd = null;
try {
$curlResult = DI::httpClient()->get($ssl_url, HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return [];
}
$ssl_connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
if ($curlResult->isSuccess()) {
$xml = $curlResult->getBodyString();
@ -231,7 +236,12 @@ class Probe
}
if ($ssl_connection_error && !is_object($xrd) && !empty($url)) {
try {
$curlResult = DI::httpClient()->get($url, HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return [];
}
$connection_error = ($curlResult->getErrorNumber() == CURLE_COULDNT_CONNECT) || ($curlResult->getReturnCode() == 0);
if ($curlResult->isTimeout()) {
Logger::info('Probing timeout', ['url' => $url]);
@ -447,7 +457,12 @@ class Probe
*/
private static function getHideStatus(string $url): bool
{
try {
$curlResult = DI::httpClient()->get($url, HttpClientAccept::HTML, [HttpClientOptions::CONTENT_LENGTH => 1000000, HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return false;
}
if (!$curlResult->isSuccess()) {
return false;
}
@ -886,7 +901,12 @@ class Probe
private static function pollZot(string $url, array $data): array
{
try {
$curlResult = DI::httpClient()->get($url, 'application/x-zot+json', [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return $data;
}
if ($curlResult->isTimeout()) {
return $data;
}
@ -1067,7 +1087,12 @@ class Probe
*/
private static function pollNoscrape(string $noscrape_url, array $data): array
{
try {
$curlResult = DI::httpClient()->get($noscrape_url, HttpClientAccept::JSON, [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return $data;
}
if ($curlResult->isTimeout()) {
self::$isTimeout = true;
return $data;
@ -1227,7 +1252,12 @@ class Probe
*/
private static function pollHcard(string $hcard_url, array $data): array
{
try {
$curlResult = DI::httpClient()->get($hcard_url, HttpClientAccept::HTML, [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return [];
}
if ($curlResult->isTimeout()) {
self::$isTimeout = true;
return [];
@ -1517,7 +1547,12 @@ class Probe
}
// Fetch all additional data from the feed
try {
$curlResult = DI::httpClient()->get($data['poll'], HttpClientAccept::FEED_XML, [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return [];
}
if ($curlResult->isTimeout()) {
self::$isTimeout = true;
return [];
@ -1904,7 +1939,12 @@ class Probe
return '';
}
try {
$curlResult = DI::httpClient()->get($gserver['noscrape'] . '/' . $data['nick'], HttpClientAccept::JSON, [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return '';
}
if ($curlResult->isSuccess() && !empty($curlResult->getBodyString())) {
$noscrape = json_decode($curlResult->getBodyString(), true);
@ -1980,7 +2020,12 @@ class Probe
private static function updateFromFeed(array $data): string
{
// Search for the newest entry in the feed
try {
$curlResult = DI::httpClient()->get($data['poll'], HttpClientAccept::ATOM_XML, [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTINFO]);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return '';
}
if (!$curlResult->isSuccess() || !$curlResult->getBodyString()) {
return '';
}

View file

@ -115,10 +115,16 @@ class Delivery
$data = ActivityPub\Transmitter::createCachedActivityFromItem($item_id);
if (!empty($data)) {
$timestamp = microtime(true);
try {
$response = HTTPSignature::post($data, $inbox, $owner);
$runtime = microtime(true) - $timestamp;
$success = $response->isSuccess();
$serverfail = $response->isTimeout();
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
$success = false;
$serverfail = true;
}
$runtime = microtime(true) - $timestamp;
if (!$success) {
// 5xx errors are problems on the server. We don't need to continue delivery then.
if (!$serverfail && ($response->getReturnCode() >= 500) && ($response->getReturnCode() <= 599)) {

View file

@ -996,7 +996,12 @@ class DFRN
$content_type = ($public_batch ? 'application/magic-envelope+xml' : 'application/json');
try {
$postResult = DI::httpClient()->post($dest_url, $envelope, ['Content-Type' => $content_type], 0, HttpClientRequest::DFRN);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return -25;
}
Item::incrementOutbound(Protocol::DFRN);
$xml = $postResult->getBodyString();

View file

@ -2955,7 +2955,12 @@ class Diaspora
if (!intval(DI::config()->get('system', 'diaspora_test'))) {
$content_type = (($public_batch) ? 'application/magic-envelope+xml' : 'application/json');
try {
$postResult = DI::httpClient()->post($dest_url . '/', $envelope, ['Content-Type' => $content_type], 0, HttpClientRequest::DIASPORA);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return 0;
}
$return_code = $postResult->getReturnCode();
Item::incrementOutbound(Protocol::DIASPORA);
} else {

View file

@ -230,7 +230,11 @@ class ExAuth
$url = ($ssl ? 'https' : 'http') . '://' . $host . '/noscrape/' . $user;
try {
$curlResult = DI::httpClient()->get($url, HttpClientAccept::JSON, [HttpClientOptions::REQUEST => HttpClientRequest::CONTACTVERIFIER]);
} catch (\Throwable $th) {
return false;
}
if (!$curlResult->isSuccess()) {
return false;

View file

@ -366,7 +366,12 @@ class HTTPSignature
return true;
}
try {
$postResult = self::post($data, $target, $owner);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return false;
}
$return_code = $postResult->getReturnCode();
return ($return_code >= 200) && ($return_code <= 299);

View file

@ -79,6 +79,7 @@ class Network
try {
$curlResult = DI::httpClient()->get($url, HttpClientAccept::DEFAULT, $options);
} catch (\Exception $e) {
Logger::notice('Got exception', ['code' => $e->getCode(), 'message' => $e->getMessage()]);
return false;
}
}

View file

@ -65,7 +65,12 @@ class ParseUrl
// Workaround for systems that can't handle a HEAD request. Don't retry on timeouts.
if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() >= 400) && !in_array($curlResult->getReturnCode(), [408, 504])) {
try {
$curlResult = DI::httpClient()->get($url, $accept, array_merge([HttpClientOptions::CONTENT_LENGTH => 1000000], $options));
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return [];
}
}
if (!$curlResult->isSuccess()) {

View file

@ -51,7 +51,12 @@ class CheckRelMeProfileLink
}
$xrd_timeout = DI::config()->get('system', 'xrd_timeout');
try {
$curlResult = DI::httpClient()->get($owner['homepage'], HttpClientAccept::HTML, [HttpClientOptions::TIMEOUT => $xrd_timeout, HttpClientOptions::REQUEST => HttpClientRequest::CONTACTVERIFIER]);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return;
}
if (!$curlResult->isSuccess()) {
Logger::notice('Could not cURL the homepage URL', ['owner homepage' => $owner['homepage']]);
return;

View file

@ -152,7 +152,12 @@ class OnePoll
$cookiejar = tempnam(System::getTempPath(), 'cookiejar-onepoll-');
Item::incrementInbound(Protocol::FEED);
try {
$curlResult = DI::httpClient()->get($contact['poll'], HttpClientAccept::FEED_XML, [HttpClientOptions::COOKIEJAR => $cookiejar, HttpClientOptions::REQUEST => HttpClientRequest::FEEDFETCHER]);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return false;
}
unlink($cookiejar);
Logger::debug('Polled feed', ['url' => $contact['poll'], 'http-code' => $curlResult->getReturnCode(), 'redirect-url' => $curlResult->getRedirectUrl()]);
@ -492,7 +497,12 @@ class OnePoll
Contact::update(['hub-verify' => $verify_token], ['id' => $contact['id']]);
}
try {
$postResult = DI::httpClient()->post($url, $params, [], 0, HttpClientRequest::PUBSUB);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return false;
}
Logger::info('Hub subscription done', ['result' => $postResult->getReturnCode()]);

View file

@ -32,7 +32,12 @@ class UpdateServerPeers
return;
}
try {
$ret = DI::httpClient()->get($url . '/api/v1/instance/peers', HttpClientAccept::JSON, [HttpClientOptions::REQUEST => HttpClientRequest::SERVERDISCOVER]);
} catch (\Throwable $th) {
Logger::notice('Got exception', ['code' => $th->getCode(), 'message' => $th->getMessage()]);
return;
}
if (!$ret->isSuccess() || empty($ret->getBodyString())) {
Logger::info('Server is not reachable or does not offer the "peers" endpoint', ['url' => $url]);
return;