From 29f40ced3be919805f041be27017005fd54c4acb Mon Sep 17 00:00:00 2001
From: floppydiskette <floppydisk@hyprcat.net>
Date: Sat, 31 Aug 2024 00:47:01 +0100
Subject: Handle any errors if unable to get presence or weather data

---
 app/View/Components/DiscordStatus.php |  4 +++-
 app/View/Components/Weather.php       | 16 +++++++++++-----
 2 files changed, 14 insertions(+), 6 deletions(-)

(limited to 'app/View/Components')

diff --git a/app/View/Components/DiscordStatus.php b/app/View/Components/DiscordStatus.php
index fac06ae..3ad3a3b 100644
--- a/app/View/Components/DiscordStatus.php
+++ b/app/View/Components/DiscordStatus.php
@@ -31,13 +31,15 @@ class DiscordStatus extends Component
 
         $response = Http::get('https://api.lanyard.rest/v1/users/' . Config::get('services.lanyard.user_id'));
         $data = $response->json();
+        if (!isset($data["data"])) return null;
         $presence = $data["data"];
         Cache::put('discord_presence', $presence, now()->addSeconds(60));
         return $presence;
     }
 
-    public function getOnlineStatus(): array {
+    public function getOnlineStatus(): ?array {
         $presence = $this->getDiscordPresence();
+        if ($presence == null) return null;
         return match ($presence["discord_status"]) {
             "online", "dnd" => [
                 "text" => "online",
diff --git a/app/View/Components/Weather.php b/app/View/Components/Weather.php
index 69be9fd..dcf3ff7 100644
--- a/app/View/Components/Weather.php
+++ b/app/View/Components/Weather.php
@@ -3,6 +3,7 @@
 namespace App\View\Components;
 
 use Closure;
+use Exception;
 use Illuminate\Contracts\View\View;
 use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\Config;
@@ -25,11 +26,16 @@ class Weather extends Component
             return Cache::get('weather_data');
         }
 
-        $response = Http::get('http://'. Config::get('services.weatherlink') . '/v1/current_conditions');
-        $data = $response->json();
-        $conditions = $data["data"]["conditions"];
-        Cache::put('weather_data', $conditions, now()->addSeconds(60));
-        return $conditions;
+        try {
+            $response = Http::get('http://' . Config::get('services.weatherlink') . '/v1/current_conditions');
+            $data = $response->json();
+            $conditions = $data["data"]["conditions"];
+            Cache::put('weather_data', $conditions, now()->addSeconds(60));
+            return $conditions;
+        } catch (Exception $ex) {
+            return null;
+        }
+
     }
 
     /**
-- 
cgit v1.2.3-54-g00ecf