From b574785d1eb73cbe58b1212534c99986fc3494cf Mon Sep 17 00:00:00 2001 From: myrmidex Date: Wed, 25 Feb 2026 20:36:34 +0100 Subject: [PATCH] 75 - Fix error handling for invalid instance URLs --- app/Livewire/Onboarding.php | 16 ++++++++-------- app/Modules/Lemmy/Services/LemmyApiService.php | 4 +++- app/Services/Auth/LemmyAuthService.php | 4 ++++ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/app/Livewire/Onboarding.php b/app/Livewire/Onboarding.php index 2c738c8..0d3ff55 100644 --- a/app/Livewire/Onboarding.php +++ b/app/Livewire/Onboarding.php @@ -142,7 +142,14 @@ public function createPlatformAccount(): void $fullInstanceUrl = 'https://' . $this->instanceUrl; try { - // Create or get platform instance + // Authenticate with Lemmy API first (before creating any records) + $authResponse = $this->lemmyAuthService->authenticate( + $fullInstanceUrl, + $this->username, + $this->password + ); + + // Only create platform instance after successful authentication $platformInstance = PlatformInstance::firstOrCreate([ 'url' => $fullInstanceUrl, 'platform' => 'lemmy', @@ -151,13 +158,6 @@ public function createPlatformAccount(): void 'is_active' => true, ]); - // Authenticate with Lemmy API - $authResponse = $this->lemmyAuthService->authenticate( - $fullInstanceUrl, - $this->username, - $this->password - ); - // Create platform account $platformAccount = PlatformAccount::create([ 'platform' => 'lemmy', diff --git a/app/Modules/Lemmy/Services/LemmyApiService.php b/app/Modules/Lemmy/Services/LemmyApiService.php index d0ebb55..7c3bb5f 100644 --- a/app/Modules/Lemmy/Services/LemmyApiService.php +++ b/app/Modules/Lemmy/Services/LemmyApiService.php @@ -73,10 +73,12 @@ public function login(string $username, string $password): ?string if ($idx === 0 && in_array('http', $schemesToTry, true)) { continue; } - return null; + // Connection failed - throw exception to distinguish from auth failure + throw new Exception('Connection failed: ' . $e->getMessage()); } } + // If we get here without a token, it's an auth failure (not connection) return null; } diff --git a/app/Services/Auth/LemmyAuthService.php b/app/Services/Auth/LemmyAuthService.php index dfd6c11..2bd972e 100644 --- a/app/Services/Auth/LemmyAuthService.php +++ b/app/Services/Auth/LemmyAuthService.php @@ -65,6 +65,10 @@ public function authenticate(string $instanceUrl, string $username, string $pass if (str_contains($e->getMessage(), 'Rate limited by')) { throw new PlatformAuthException(PlatformEnum::LEMMY, $e->getMessage()); } + // Check if it's a connection failure + if (str_contains($e->getMessage(), 'Connection failed')) { + throw new PlatformAuthException(PlatformEnum::LEMMY, 'Connection failed'); + } // For other exceptions, throw a clean PlatformAuthException throw new PlatformAuthException(PlatformEnum::LEMMY, 'Connection failed'); }