From 2b513a48c796927bcb028d61a763f47e73598351 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 26 Feb 2023 14:08:33 +0000 Subject: [PATCH 1/3] Catch all errors thrown by "fetchRaw" --- src/Contact/Avatar.php | 7 +++++- src/Core/Storage/Type/ExternalResource.php | 1 + src/Model/APContact.php | 25 +++++++++++++--------- src/Model/Contact.php | 21 +++++++++++------- src/Model/Post/Link.php | 9 ++++++-- src/Module/Proxy.php | 15 ++++++++----- src/Protocol/ActivityPub/Processor.php | 7 +++++- src/Util/HTTPSignature.php | 7 +++++- 8 files changed, 64 insertions(+), 28 deletions(-) diff --git a/src/Contact/Avatar.php b/src/Contact/Avatar.php index e039a52799..4843e5d37c 100644 --- a/src/Contact/Avatar.php +++ b/src/Contact/Avatar.php @@ -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)) { diff --git a/src/Core/Storage/Type/ExternalResource.php b/src/Core/Storage/Type/ExternalResource.php index 055db0deaa..54d47e5bf4 100644 --- a/src/Core/Storage/Type/ExternalResource.php +++ b/src/Core/Storage/Type/ExternalResource.php @@ -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()) { diff --git a/src/Model/APContact.php b/src/Model/APContact.php index 215d7e317d..a429a7a5d4 100644 --- a/src/Model/APContact.php +++ b/src/Model/APContact.php @@ -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) { diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 571253f425..64509b5d22 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -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; diff --git a/src/Model/Post/Link.php b/src/Model/Post/Link.php index 343ad815c3..4ac8173799 100644 --- a/src/Model/Post/Link.php +++ b/src/Model/Post/Link.php @@ -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]]; diff --git a/src/Module/Proxy.php b/src/Module/Proxy.php index ab129a82c9..3f1e4b2b80 100644 --- a/src/Module/Proxy.php +++ b/src/Module/Proxy.php @@ -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']]); diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 11267cfaf5..20736504ef 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -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; diff --git a/src/Util/HTTPSignature.php b/src/Util/HTTPSignature.php index f082fe32f7..fe623f2968 100644 --- a/src/Util/HTTPSignature.php +++ b/src/Util/HTTPSignature.php @@ -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 []; From 6e3602591d52771f7f3c8c4c0d926a1690891730 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 26 Feb 2023 22:43:45 +0000 Subject: [PATCH 2/3] Use "Exception" --- src/Contact/Avatar.php | 4 ++-- src/Factory/Api/Mastodon/Notification.php | 2 +- src/Factory/Api/Mastodon/Status.php | 12 ++++++------ src/Model/APContact.php | 4 ++-- src/Model/Contact.php | 4 ++-- src/Model/Post/Link.php | 4 ++-- src/Module/Api/Mastodon/Accounts/Statuses.php | 4 ++-- src/Module/Api/Mastodon/Bookmarks.php | 4 ++-- src/Module/Api/Mastodon/Favourited.php | 4 ++-- src/Module/Api/Mastodon/Lists/Accounts.php | 2 +- src/Module/Api/Mastodon/Search.php | 4 ++-- src/Module/Api/Mastodon/Timelines/Home.php | 4 ++-- src/Module/Api/Mastodon/Timelines/ListTimeline.php | 4 ++-- src/Module/Api/Mastodon/Timelines/PublicTimeline.php | 4 ++-- src/Module/Api/Mastodon/Timelines/Tag.php | 4 ++-- src/Module/Api/Mastodon/Trends/Statuses.php | 4 ++-- src/Module/Proxy.php | 4 ++-- src/Object/Image.php | 2 +- src/Protocol/ActivityPub/Processor.php | 4 ++-- src/Util/HTTPSignature.php | 4 ++-- 20 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/Contact/Avatar.php b/src/Contact/Avatar.php index 4843e5d37c..299c263994 100644 --- a/src/Contact/Avatar.php +++ b/src/Contact/Avatar.php @@ -75,8 +75,8 @@ class Avatar try { $fetchResult = HTTPSignature::fetchRaw($avatar, 0, [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE]]); - } catch (\Throwable $th) { - Logger::notice('Avatar is invalid', ['avatar' => $avatar, 'error' => $th]); + } catch (\Exception $exception) { + Logger::notice('Avatar is invalid', ['avatar' => $avatar, 'exception' => $exception]); return $fields; } diff --git a/src/Factory/Api/Mastodon/Notification.php b/src/Factory/Api/Mastodon/Notification.php index 52241e1fc8..e6e9f4d094 100644 --- a/src/Factory/Api/Mastodon/Notification.php +++ b/src/Factory/Api/Mastodon/Notification.php @@ -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 { diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index 788841a9bc..f653107d40 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -266,8 +266,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 { @@ -277,8 +277,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 { @@ -309,8 +309,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 { diff --git a/src/Model/APContact.php b/src/Model/APContact.php index a429a7a5d4..20457b46e8 100644 --- a/src/Model/APContact.php +++ b/src/Model/APContact.php @@ -202,8 +202,8 @@ 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]); + } catch (\Exception $exception) { + Logger::notice('Error fetching url', ['url' => $url, 'exception' => $exception]); $failed = true; } diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 64509b5d22..f35808f050 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -2228,8 +2228,8 @@ class Contact return; } } - } catch (\Throwable $th) { - Logger::notice('Error fetching avatar', ['avatar' => $avatar, 'error' => $th]); + } catch (\Exception $exception) { + Logger::notice('Error fetching avatar', ['avatar' => $avatar, 'exception' => $exception]); return; } } elseif (!empty($contact['blurhash'])) { diff --git a/src/Model/Post/Link.php b/src/Model/Post/Link.php index 4ac8173799..dc77979511 100644 --- a/src/Model/Post/Link.php +++ b/src/Model/Post/Link.php @@ -130,8 +130,8 @@ class Link if (empty($curlResult) || !$curlResult->isSuccess()) { return []; } - } catch (\Throwable $th) { - Logger::notice('Error fetching url', ['url' => $url, 'error' => $th]); + } catch (\Exception $exception) { + Logger::notice('Error fetching url', ['url' => $url, 'exception' => $exception]); return []; } $fields = ['mimetype' => $curlResult->getHeader('Content-Type')[0]]; diff --git a/src/Module/Api/Mastodon/Accounts/Statuses.php b/src/Module/Api/Mastodon/Accounts/Statuses.php index 067e330271..463411745c 100644 --- a/src/Module/Api/Mastodon/Accounts/Statuses.php +++ b/src/Module/Api/Mastodon/Accounts/Statuses.php @@ -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); diff --git a/src/Module/Api/Mastodon/Bookmarks.php b/src/Module/Api/Mastodon/Bookmarks.php index 615bfc501f..59cf7f54a2 100644 --- a/src/Module/Api/Mastodon/Bookmarks.php +++ b/src/Module/Api/Mastodon/Bookmarks.php @@ -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); diff --git a/src/Module/Api/Mastodon/Favourited.php b/src/Module/Api/Mastodon/Favourited.php index 93c9ef2436..7829f37d70 100644 --- a/src/Module/Api/Mastodon/Favourited.php +++ b/src/Module/Api/Mastodon/Favourited.php @@ -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); diff --git a/src/Module/Api/Mastodon/Lists/Accounts.php b/src/Module/Api/Mastodon/Lists/Accounts.php index 667cd87eab..e19dfb0314 100644 --- a/src/Module/Api/Mastodon/Lists/Accounts.php +++ b/src/Module/Api/Mastodon/Lists/Accounts.php @@ -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); diff --git a/src/Module/Api/Mastodon/Search.php b/src/Module/Api/Mastodon/Search.php index c35e33062b..69e2627682 100644 --- a/src/Module/Api/Mastodon/Search.php +++ b/src/Module/Api/Mastodon/Search.php @@ -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); diff --git a/src/Module/Api/Mastodon/Timelines/Home.php b/src/Module/Api/Mastodon/Timelines/Home.php index 5efaf4930e..7fc179297c 100644 --- a/src/Module/Api/Mastodon/Timelines/Home.php +++ b/src/Module/Api/Mastodon/Timelines/Home.php @@ -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); diff --git a/src/Module/Api/Mastodon/Timelines/ListTimeline.php b/src/Module/Api/Mastodon/Timelines/ListTimeline.php index a8de13056e..1e9a5810c1 100644 --- a/src/Module/Api/Mastodon/Timelines/ListTimeline.php +++ b/src/Module/Api/Mastodon/Timelines/ListTimeline.php @@ -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); diff --git a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php index 968e34dbb9..31750d4a5c 100644 --- a/src/Module/Api/Mastodon/Timelines/PublicTimeline.php +++ b/src/Module/Api/Mastodon/Timelines/PublicTimeline.php @@ -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); diff --git a/src/Module/Api/Mastodon/Timelines/Tag.php b/src/Module/Api/Mastodon/Timelines/Tag.php index 64cd84366e..476c9c2b94 100644 --- a/src/Module/Api/Mastodon/Timelines/Tag.php +++ b/src/Module/Api/Mastodon/Timelines/Tag.php @@ -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); diff --git a/src/Module/Api/Mastodon/Trends/Statuses.php b/src/Module/Api/Mastodon/Trends/Statuses.php index 884319aa48..336f671559 100644 --- a/src/Module/Api/Mastodon/Trends/Statuses.php +++ b/src/Module/Api/Mastodon/Trends/Statuses.php @@ -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); diff --git a/src/Module/Proxy.php b/src/Module/Proxy.php index 3f1e4b2b80..4d1e1c304b 100644 --- a/src/Module/Proxy.php +++ b/src/Module/Proxy.php @@ -92,8 +92,8 @@ class Proxy extends BaseModule self::responseError(); // stop. } - } catch (\Throwable $th) { - Logger::notice('Error fetching image', ['image' => $request['url'], 'error' => $th]); + } catch (\Exception $exception) { + Logger::notice('Error fetching image', ['image' => $request['url'], 'exception' => $exception]); self::responseError(); } diff --git a/src/Object/Image.php b/src/Object/Image.php index d6c897e88d..49196a0c44 100644 --- a/src/Object/Image.php +++ b/src/Object/Image.php @@ -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']]; diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 20736504ef..54acca9df3 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -572,8 +572,8 @@ class Processor { try { $curlResult = HTTPSignature::fetchRaw($url, 0); - } catch (\Throwable $th) { - Logger::notice('Error fetching url', ['url' => $url, 'error' => $th]); + } catch (\Exception $exception) { + Logger::notice('Error fetching url', ['url' => $url, 'exception' => $exception]); return true; } diff --git a/src/Util/HTTPSignature.php b/src/Util/HTTPSignature.php index fe623f2968..d48d02c20e 100644 --- a/src/Util/HTTPSignature.php +++ b/src/Util/HTTPSignature.php @@ -424,8 +424,8 @@ class HTTPSignature { try { $curlResult = self::fetchRaw($request, $uid); - } catch (\Throwable $th) { - Logger::notice('Error fetching url', ['url' => $request, 'error' => $th]); + } catch (\Exception $exception) { + Logger::notice('Error fetching url', ['url' => $request, 'exception' => $exception]); return []; } From bd3120d6cb3b52cf103d4e2f36f0cd93d359d184 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 27 Feb 2023 07:02:06 +0000 Subject: [PATCH 3/3] Use non static logger call --- src/Core/Storage/Repository/StorageManager.php | 6 +++--- src/Core/Storage/Type/ExternalResource.php | 14 +++++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Core/Storage/Repository/StorageManager.php b/src/Core/Storage/Repository/StorageManager.php index fbe910d01d..252454665d 100644 --- a/src/Core/Storage/Repository/StorageManager.php +++ b/src/Core/Storage/Repository/StorageManager.php @@ -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; } diff --git a/src/Core/Storage/Type/ExternalResource.php b/src/Core/Storage/Type/ExternalResource.php index 54d47e5bf4..181312a5bf 100644 --- a/src/Core/Storage/Type/ExternalResource.php +++ b/src/Core/Storage/Type/ExternalResource.php @@ -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,11 +65,11 @@ 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]); + $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)) {