Merge pull request #12843 from annando/fetchraw-attachments

Catch all errors thrown by "fetchRaw"
This commit is contained in:
Philipp 2023-02-27 08:24:05 +01:00 committed by GitHub
commit a40ecb3902
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 104 additions and 60 deletions

View file

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

View file

@ -168,7 +168,7 @@ class StorageManager
return $data['storage_config'];
} catch (InternalServerErrorException $exception) {
throw new StorageException(sprintf('Failed calling hook::storage_config for backend %s', $name), $exception);
throw new StorageException(sprintf('Failed calling hook::storage_config for backend %s', $name), $exception->__toString());
}
}
}
@ -208,7 +208,7 @@ class StorageManager
$this->backendInstances[$name] = new Type\SystemResource();
break;
case Type\ExternalResource::getName():
$this->backendInstances[$name] = new Type\ExternalResource();
$this->backendInstances[$name] = new Type\ExternalResource($this->logger);
break;
default:
$data = [
@ -223,7 +223,7 @@ class StorageManager
$this->backendInstances[$data['name'] ?? $name] = $data['storage'];
} catch (InternalServerErrorException $exception) {
throw new StorageException(sprintf('Failed calling hook::storage_instance for backend %s', $name), $exception);
throw new StorageException(sprintf('Failed calling hook::storage_instance for backend %s', $name), $exception->__toString());
}
break;
}

View file

@ -22,12 +22,12 @@
namespace Friendica\Core\Storage\Type;
use Exception;
use Friendica\Core\Logger;
use Friendica\Core\Storage\Exception\ReferenceStorageException;
use Friendica\Core\Storage\Capability\ICanReadFromStorage;
use Friendica\Network\HTTPClient\Client\HttpClientAccept;
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
use Friendica\Util\HTTPSignature;
use Psr\Log\LoggerInterface;
/**
* External resource storage class
@ -39,6 +39,14 @@ class ExternalResource implements ICanReadFromStorage
{
const NAME = 'ExternalResource';
/** @var LoggerInterface */
protected $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* @inheritDoc
*/
@ -57,10 +65,11 @@ class ExternalResource implements ICanReadFromStorage
try {
$fetchResult = HTTPSignature::fetchRaw($data->url, $data->uid, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]);
} catch (Exception $exception) {
$this->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()) {
Logger::debug('Got picture', ['Content-Type' => $fetchResult->getHeader('Content-Type'), 'uid' => $data->uid, 'url' => $data->url]);
$this->logger->debug('Got picture', ['Content-Type' => $fetchResult->getHeader('Content-Type'), 'uid' => $data->uid, 'url' => $data->url]);
return $fetchResult->getBody();
} else {
if (empty($fetchResult)) {

View file

@ -64,7 +64,7 @@ class Notification extends BaseFactory
if ($Notification->targetUriId) {
try {
$status = $this->mstdnStatusFactory->createFromUriId($Notification->targetUriId, $Notification->uid, $display_quotes);
} catch (\Throwable $th) {
} catch (\Exception $exception) {
$status = null;
}
} else {

View file

@ -269,8 +269,8 @@ class Status extends BaseFactory
if ($is_reshare) {
try {
$reshare = $this->createFromUriId($uriId, $uid, $display_quote, false, false)->toArray();
} catch (\Throwable $th) {
Logger::info('Reshare not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]);
} catch (\Exception $exception) {
Logger::info('Reshare not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]);
$reshare = [];
}
} else {
@ -280,8 +280,8 @@ class Status extends BaseFactory
if ($in_reply_status && ($item['gravity'] == Item::GRAVITY_COMMENT)) {
try {
$in_reply = $this->createFromUriId($item['thr-parent-id'], $uid, $display_quote, false, false)->toArray();
} catch (\Throwable $th) {
Logger::info('Reply post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]);
} catch (\Exception $exception) {
Logger::info('Reply post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]);
$in_reply = [];
}
} else {
@ -315,8 +315,8 @@ class Status extends BaseFactory
if (!empty($quote_id)) {
try {
$quote = $this->createFromUriId($quote_id, $uid, false, false, false)->toArray();
} catch (\Throwable $th) {
Logger::info('Quote not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]);
} catch (\Exception $exception) {
Logger::info('Quote not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]);
$quote = [];
}
} else {

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 (\Exception $exception) {
Logger::notice('Error fetching url', ['url' => $url, 'exception' => $exception]);
$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 (\Exception $exception) {
Logger::notice('Error fetching avatar', ['avatar' => $avatar, 'exception' => $exception]);
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 (\Exception $exception) {
Logger::notice('Error fetching url', ['url' => $url, 'exception' => $exception]);
return [];
}
$fields = ['mimetype' => $curlResult->getHeader('Content-Type')[0]];
$img_str = $curlResult->getBody();

View file

@ -120,8 +120,8 @@ class Statuses extends BaseApi
self::setBoundaries($item['uri-id']);
try {
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes);
} catch (\Throwable $th) {
Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]);
} catch (\Exception $exception) {
Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]);
}
}
DBA::close($items);

View file

@ -77,8 +77,8 @@ class Bookmarks extends BaseApi
self::setBoundaries($item['uri-id']);
try {
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes);
} catch (\Throwable $th) {
Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]);
} catch (\Exception $exception) {
Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]);
}
}
DBA::close($items);

View file

@ -79,8 +79,8 @@ class Favourited extends BaseApi
self::setBoundaries($item['thr-parent-id']);
try {
$statuses[] = DI::mstdnStatus()->createFromUriId($item['thr-parent-id'], $uid, $display_quotes);
} catch (\Throwable $th) {
Logger::info('Post not fetchable', ['uri-id' => $item['thr-parent-id'], 'uid' => $uid, 'error' => $th]);
} catch (\Exception $exception) {
Logger::info('Post not fetchable', ['uri-id' => $item['thr-parent-id'], 'uid' => $uid, 'exception' => $exception]);
}
}
DBA::close($items);

View file

@ -117,7 +117,7 @@ class Accounts extends BaseApi
self::setBoundaries($member['contact-id']);
try {
$accounts[] = DI::mstdnAccount()->createFromContactId($member['contact-id'], $uid);
} catch (\Throwable $th) {
} catch (\Exception $exception) {
}
}
DBA::close($members);

View file

@ -183,8 +183,8 @@ class Search extends BaseApi
self::setBoundaries($item['uri-id']);
try {
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes);
} catch (\Throwable $th) {
Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]);
} catch (\Exception $exception) {
Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]);
}
}
DBA::close($items);

View file

@ -99,8 +99,8 @@ class Home extends BaseApi
self::setBoundaries($item['uri-id']);
try {
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes);
} catch (\Throwable $th) {
Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]);
} catch (\Exception $exception) {
Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]);
}
}
DBA::close($items);

View file

@ -104,8 +104,8 @@ class ListTimeline extends BaseApi
self::setBoundaries($item['uri-id']);
try {
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes);
} catch (\Throwable $th) {
Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]);
} catch (\Exception $exception) {
Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]);
}
}
DBA::close($items);

View file

@ -99,8 +99,8 @@ class PublicTimeline extends BaseApi
self::setBoundaries($item['uri-id']);
try {
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes);
} catch (\Throwable $th) {
Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]);
} catch (\Exception $exception) {
Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]);
}
}
DBA::close($items);

View file

@ -120,8 +120,8 @@ class Tag extends BaseApi
self::setBoundaries($item['uri-id']);
try {
$statuses[] = DI::mstdnStatus()->createFromUriId($item['uri-id'], $uid, $display_quotes);
} catch (\Throwable $th) {
Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'error' => $th]);
} catch (\Exception $exception) {
Logger::info('Post not fetchable', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'exception' => $exception]);
}
}
DBA::close($items);

View file

@ -57,8 +57,8 @@ class Statuses extends BaseApi
while ($status = Post::fetch($statuses)) {
try {
$trending[] = DI::mstdnStatus()->createFromUriId($status['uri-id'], $uid, $display_quotes);
} catch (\Throwable $th) {
Logger::info('Post not fetchable', ['uri-id' => $status['uri-id'], 'uid' => $uid, 'error' => $th]);
} catch (\Exception $exception) {
Logger::info('Post not fetchable', ['uri-id' => $status['uri-id'], 'uid' => $uid, 'exception' => $exception]);
}
}
DBA::close($statuses);

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 (\Exception $exception) {
Logger::notice('Error fetching image', ['image' => $request['url'], 'exception' => $exception]);
self::responseError();
}
Logger::debug('Got picture', ['Content-Type' => $fetchResult->getHeader('Content-Type'), 'uid' => DI::userSession()->getLocalUserId(), 'image' => $request['url']]);

View file

@ -718,7 +718,7 @@ class Image
if ($image->isImagick()) {
try {
$colors = $image->image->getImagePixelColor($x, $y)->getColor();
} catch (\Throwable $th) {
} catch (\Exception $exception) {
return '';
}
$row[] = [$colors['r'], $colors['g'], $colors['b']];

View file

@ -570,7 +570,12 @@ class Processor
*/
public static function isActivityGone(string $url): bool
{
try {
$curlResult = HTTPSignature::fetchRaw($url, 0);
} catch (\Exception $exception) {
Logger::notice('Error fetching url', ['url' => $url, 'exception' => $exception]);
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 (\Exception $exception) {
Logger::notice('Error fetching url', ['url' => $request, 'exception' => $exception]);
return [];
}
if (empty($curlResult)) {
return [];