Enhance CivitaiClient error handling for unauthorized access

- Updated handling of 401 unauthorized responses to differentiate between API key issues and early access restrictions.
- Improved logging for unauthorized access attempts.
- Refactored condition to check for early access restrictions based on response headers.
- Adjusted logic in DownloadManager to check for early access using a more concise method.
This commit is contained in:
Will Miao
2025-03-28 04:11:08 +08:00
parent fd1435537f
commit 1420ab31a2
2 changed files with 9 additions and 3 deletions

View File

@@ -76,9 +76,15 @@ class CivitaiClient:
headers = self._get_request_headers()
async with session.get(url, headers=headers, allow_redirects=True) as response:
if response.status != 200:
# Handle early access 401 unauthorized responses
# Handle 401 unauthorized responses
if response.status == 401:
logger.warning(f"Unauthorized access to resource: {url} (Status 401)")
# Check if this is an API key issue (has Set-Cookie headers)
if 'Set-Cookie' in response.headers:
return False, "Invalid or missing CivitAI API key. Please check your API key in settings."
# Otherwise it's an early access restriction
return False, "Early access restriction: You must purchase early access to download this LoRA."
# Handle other client errors that might be permission-related
@@ -251,4 +257,4 @@ class CivitaiClient:
return None
except Exception as e:
logger.error(f"Error getting hash from Civitai: {e}")
return None
return None

View File

@@ -29,7 +29,7 @@ class DownloadManager:
return {'success': False, 'error': 'Failed to fetch model metadata'}
# Check if this is an early access LoRA
if 'earlyAccessEndsAt' in version_info:
if version_info.get('earlyAccessEndsAt'):
early_access_date = version_info.get('earlyAccessEndsAt', '')
# Convert to a readable date if possible
try: