Catch all errors thrown by "fetchRaw"

This commit is contained in:
Michael 2023-02-26 14:08:33 +00:00
parent 6870ccc00e
commit 2b513a48c7
8 changed files with 64 additions and 28 deletions

View File

@ -73,7 +73,12 @@ class Avatar
return $fields;
}
try {
$fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
} catch (\Throwable $th) {
Logger::notice('Avatar is invalid', ['avatar' => $avatar, 'error' => $th]);
return $fields;
}
$img_str = $fetchResult->getBody();
if (empty($img_str)) {

View File

@ -57,6 +57,7 @@ class ExternalResource implements ICanReadFromStorage
try {
$fetchResult = HTTPSignature::fetchRaw($data->url, $data->uid, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
} catch (Exception $exception) {
Logger::notice('URL is invalid', ['url' => $data->url, 'error' => $exception]);
throw new ReferenceStorageException(sprintf('External resource failed to get %s', $reference), $exception->getCode(), $exception);
}
if (!empty($fetchResult) && $fetchResult->isSuccess()) {

View File

@ -189,6 +189,7 @@ class APContact
if (empty($data)) {
$local_owner = [];
try {
$curlResult = HTTPSignature::fetchRaw($url);
$failed = empty($curlResult) || empty($curlResult->getBody()) ||
(!$curlResult->isSuccess() && ($curlResult->getReturnCode() != 410));
@ -201,6 +202,10 @@ class APContact
if (!$failed && ($curlResult->getReturnCode() == 410)) {
$data = ['@context' => ActivityPub::CONTEXT, 'id' => $url, 'type' => 'Tombstone'];
}
} catch (\Throwable $th) {
Logger::notice('Error fetching url', ['url' => $url, 'error' => $th]);
$failed = true;
}
if ($failed) {
self::markForArchival($fetched_contact ?: []);

View File

@ -2216,6 +2216,7 @@ class Contact
if (($contact['avatar'] != $avatar) || empty($contact['blurhash'])) {
$update_fields = ['avatar' => $avatar];
if (!Network::isLocalLink($avatar)) {
try {
$fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
$img_str = $fetchResult->getBody();
@ -2227,6 +2228,10 @@ class Contact
return;
}
}
} catch (\Throwable $th) {
Logger::notice('Error fetching avatar', ['avatar' => $avatar, 'error' => $th]);
return;
}
} elseif (!empty($contact['blurhash'])) {
$update_fields['blurhash'] = null;
} else {

View File

@ -125,10 +125,15 @@ class Link
{
$timeout = DI::config()->get('system', 'xrd_timeout');
try {
$curlResult = HTTPSignature::fetchRaw($url, 0, [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => $accept]);
if (empty($curlResult) || !$curlResult->isSuccess()) {
return [];
}
} catch (\Throwable $th) {
Logger::notice('Error fetching url', ['url' => $url, 'error' => $th]);
return [];
}
$fields = ['mimetype' => $curlResult->getHeader('Content-Type')[0]];
$img_str = $curlResult->getBody();

View File

@ -83,6 +83,7 @@ class Proxy extends BaseModule
$request['url'] = str_replace(' ', '+', $request['url']);
// Fetch the content with the local user
try {
$fetchResult = HTTPSignature::fetchRaw($request['url'], DI::userSession()->getLocalUserId(), [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE], 'timeout' => 10]);
$img_str = $fetchResult->getBody();
@ -91,6 +92,10 @@ class Proxy extends BaseModule
self::responseError();
// stop.
}
} catch (\Throwable $th) {
Logger::notice('Error fetching image', ['image' => $request['url'], 'error' => $th]);
self::responseError();
}
Logger::debug('Got picture', ['Content-Type' => $fetchResult->getHeader('Content-Type'), 'uid' => DI::userSession()->getLocalUserId(), 'image' => $request['url']]);

View File

@ -570,7 +570,12 @@ class Processor
*/
public static function isActivityGone(string $url): bool
{
try {
$curlResult = HTTPSignature::fetchRaw($url, 0);
} catch (\Throwable $th) {
Logger::notice('Error fetching url', ['url' => $url, 'error' => $th]);
return true;
}
if (Network::isUrlBlocked($url)) {
return true;

View File

@ -422,7 +422,12 @@ class HTTPSignature
*/
public static function fetch(string $request, int $uid): array
{
try {
$curlResult = self::fetchRaw($request, $uid);
} catch (\Throwable $th) {
Logger::notice('Error fetching url', ['url' => $request, 'error' => $th]);
return [];
}
if (empty($curlResult)) {
return [];