ash-jc-allen / favicon-fetcher

A Laravel package for fetching favicons from websites.
MIT License
167 stars 13 forks source link

Call to a member function cache() on null #15

Closed gjsman closed 2 years ago

gjsman commented 2 years ago

Hello,

If I run the following code which looks like it should work:

use AshAllenDesign\FaviconFetcher\Facades\Favicon;
Favicon::fetch("https://example.com/")->cache(now()->addWeek())->getFaviconUrl();

It will crash with:

[Error] Call to a member function cache() on null
ash-jc-allen commented 2 years ago

Hey @gjsman!

From what I can see, you should only be getting this with URLs where a favicon can't be fetched properly. Are you also getting this error when trying to grab the favicons from any other URLs? 🙂

gjsman commented 2 years ago

https://example.com/ is so far the only URL that I have found that causes this behavior. That would be fine if I could use a try/catch fallback, but the error about the nonexistent member function cannot be caught.

ash-jc-allen commented 2 years ago

In your particular example, the fetch method returns null if the favicon can't be found. So, you could potentially update your code to look like so:

Favicon::fetch("https://example.com/")?->cache(now()->addWeek())->getFaviconUrl();

Does this help a little bit? 🙂

gjsman commented 2 years ago

Huh... that works. Thank you!

ash-jc-allen commented 2 years ago

Perfect! I'm glad that it sorted it for you 😄