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;
}
$fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
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,17 +189,22 @@ class APContact
if (empty($data)) {
$local_owner = [];
$curlResult = HTTPSignature::fetchRaw($url);
$failed = empty($curlResult) || empty($curlResult->getBody()) ||
(!$curlResult->isSuccess() && ($curlResult->getReturnCode() != 410));
try {
$curlResult = HTTPSignature::fetchRaw($url);
$failed = empty($curlResult) || empty($curlResult->getBody()) ||
(!$curlResult->isSuccess() && ($curlResult->getReturnCode() != 410));
if (!$failed) {
$data = json_decode($curlResult->getBody(), true);
$failed = empty($data) || !is_array($data);
}
if (!$failed) {
$data = json_decode($curlResult->getBody(), true);
$failed = empty($data) || !is_array($data);
}
if (!$failed && ($curlResult->getReturnCode() == 410)) {
$data = ['@context' => ActivityPub::CONTEXT, 'id' => $url, 'type' => 'Tombstone'];
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) {

View File

@ -2216,16 +2216,21 @@ class Contact
if (($contact['avatar'] != $avatar) || empty($contact['blurhash'])) {
$update_fields = ['avatar' => $avatar];
if (!Network::isLocalLink($avatar)) {
$fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
try {
$fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
$img_str = $fetchResult->getBody();
if (!empty($img_str)) {
$image = new Image($img_str, Images::getMimeTypeByData($img_str));
if ($image->isValid()) {
$update_fields['blurhash'] = $image->getBlurHash();
} else {
return;
$img_str = $fetchResult->getBody();
if (!empty($img_str)) {
$image = new Image($img_str, Images::getMimeTypeByData($img_str));
if ($image->isValid()) {
$update_fields['blurhash'] = $image->getBlurHash();
} else {
return;
}
}
} catch (\Throwable $th) {
Logger::notice('Error fetching avatar', ['avatar' => $avatar, 'error' => $th]);
return;
}
} elseif (!empty($contact['blurhash'])) {
$update_fields['blurhash'] = null;

View File

@ -125,8 +125,13 @@ class Link
{
$timeout = DI::config()->get('system', 'xrd_timeout');
$curlResult = HTTPSignature::fetchRaw($url, 0, [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => $accept]);
if (empty($curlResult) || !$curlResult->isSuccess()) {
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]];

View File

@ -83,13 +83,18 @@ class Proxy extends BaseModule
$request['url'] = str_replace(' ', '+', $request['url']);
// Fetch the content with the local user
$fetchResult = HTTPSignature::fetchRaw($request['url'], DI::userSession()->getLocalUserId(), [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE], 'timeout' => 10]);
$img_str = $fetchResult->getBody();
try {
$fetchResult = HTTPSignature::fetchRaw($request['url'], DI::userSession()->getLocalUserId(), [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE], 'timeout' => 10]);
$img_str = $fetchResult->getBody();
if (!$fetchResult->isSuccess() || empty($img_str)) {
Logger::notice('Error fetching image', ['image' => $request['url'], 'return' => $fetchResult->getReturnCode(), 'empty' => empty($img_str)]);
if (!$fetchResult->isSuccess() || empty($img_str)) {
Logger::notice('Error fetching image', ['image' => $request['url'], 'return' => $fetchResult->getReturnCode(), 'empty' => empty($img_str)]);
self::responseError();
// stop.
}
} catch (\Throwable $th) {
Logger::notice('Error fetching image', ['image' => $request['url'], 'error' => $th]);
self::responseError();
// stop.
}
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
{
$curlResult = HTTPSignature::fetchRaw($url, 0);
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
{
$curlResult = self::fetchRaw($request, $uid);
try {
$curlResult = self::fetchRaw($request, $uid);
} catch (\Throwable $th) {
Logger::notice('Error fetching url', ['url' => $request, 'error' => $th]);
return [];
}
if (empty($curlResult)) {
return [];