Fix auth error message

This commit is contained in:
myrmidex 2025-08-09 00:52:14 +02:00
parent 17320ad05a
commit 73ba089e46
2 changed files with 21 additions and 3 deletions

View file

@ -131,8 +131,21 @@ public function createPlatform(Request $request): JsonResponse
'Platform account created successfully.' 'Platform account created successfully.'
); );
} catch (\App\Exceptions\PlatformAuthException $e) {
// Handle authentication-specific errors with cleaner messages
return $this->sendError('Invalid username or password. Please check your credentials and try again.', [], 422);
} catch (\Exception $e) { } catch (\Exception $e) {
return $this->sendError('Failed to create platform account: ' . $e->getMessage(), [], 422); // Handle other errors (network, instance not found, etc.)
$message = 'Unable to connect to the Lemmy instance. Please check the URL and try again.';
// If it's a network/connection issue, provide a more specific message
if (str_contains(strtolower($e->getMessage()), 'connection') ||
str_contains(strtolower($e->getMessage()), 'network') ||
str_contains(strtolower($e->getMessage()), 'timeout')) {
$message = 'Connection failed. Please check the instance URL and your internet connection.';
}
return $this->sendError($message, [], 422);
} }
} }

View file

@ -51,7 +51,8 @@ public function authenticate(string $instanceUrl, string $username, string $pass
$token = $api->login($username, $password); $token = $api->login($username, $password);
if (!$token) { if (!$token) {
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Login failed for user: ' . $username); // Throw a clean exception that will be caught and handled by the controller
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Invalid credentials');
} }
// Get user info with the token // Get user info with the token
@ -67,8 +68,12 @@ public function authenticate(string $instanceUrl, string $username, string $pass
] ]
] ]
]; ];
} catch (PlatformAuthException $e) {
// Re-throw PlatformAuthExceptions as-is to avoid nesting
throw $e;
} catch (Exception $e) { } catch (Exception $e) {
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Authentication failed: ' . $e->getMessage()); // For other exceptions, throw a clean PlatformAuthException
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Connection failed');
} }
} }
} }