75 - Fix error handling for invalid instance URLs

This commit is contained in:
myrmidex 2026-02-25 20:36:34 +01:00
parent 3e23dad5c5
commit b574785d1e
3 changed files with 15 additions and 9 deletions

View file

@ -142,7 +142,14 @@ public function createPlatformAccount(): void
$fullInstanceUrl = 'https://' . $this->instanceUrl; $fullInstanceUrl = 'https://' . $this->instanceUrl;
try { 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([ $platformInstance = PlatformInstance::firstOrCreate([
'url' => $fullInstanceUrl, 'url' => $fullInstanceUrl,
'platform' => 'lemmy', 'platform' => 'lemmy',
@ -151,13 +158,6 @@ public function createPlatformAccount(): void
'is_active' => true, 'is_active' => true,
]); ]);
// Authenticate with Lemmy API
$authResponse = $this->lemmyAuthService->authenticate(
$fullInstanceUrl,
$this->username,
$this->password
);
// Create platform account // Create platform account
$platformAccount = PlatformAccount::create([ $platformAccount = PlatformAccount::create([
'platform' => 'lemmy', 'platform' => 'lemmy',

View file

@ -73,10 +73,12 @@ public function login(string $username, string $password): ?string
if ($idx === 0 && in_array('http', $schemesToTry, true)) { if ($idx === 0 && in_array('http', $schemesToTry, true)) {
continue; 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; return null;
} }

View file

@ -65,6 +65,10 @@ public function authenticate(string $instanceUrl, string $username, string $pass
if (str_contains($e->getMessage(), 'Rate limited by')) { if (str_contains($e->getMessage(), 'Rate limited by')) {
throw new PlatformAuthException(PlatformEnum::LEMMY, $e->getMessage()); 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 // For other exceptions, throw a clean PlatformAuthException
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Connection failed'); throw new PlatformAuthException(PlatformEnum::LEMMY, 'Connection failed');
} }