fix: improve error handling for avatar uploads
- Change invalid file type error from 400 to 422 for better frontend handling - Add specific error message for 422 responses in frontend - Improve error message clarity - Better error classification and user guidance Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
@@ -87,8 +87,8 @@ async def upload_avatar(
|
||||
if not file.content_type.startswith("image/"):
|
||||
print("Invalid file type")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Only image files are allowed"
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="Only image files are allowed (JPG, PNG, GIF, etc.)"
|
||||
)
|
||||
|
||||
# Validate file size (5MB limit for upload endpoint)
|
||||
|
||||
@@ -218,10 +218,16 @@ function SettingsForm({ me, onBack }: { me: MemberRead; onBack: () => void }) {
|
||||
qc.invalidateQueries({ queryKey: ['me'] });
|
||||
} catch (err) {
|
||||
console.error("Upload failed:", err);
|
||||
if (err instanceof Error && err.message.includes('413')) {
|
||||
setError('File too large. Maximum size is 5MB. Please choose a smaller image.');
|
||||
if (err instanceof Error) {
|
||||
if (err.message.includes('413')) {
|
||||
setError('File too large. Maximum size is 5MB. Please choose a smaller image.');
|
||||
} else if (err.message.includes('422')) {
|
||||
setError('Invalid image file. Please upload a valid image (JPG, PNG, etc.).');
|
||||
} else {
|
||||
setError(err.message);
|
||||
}
|
||||
} else {
|
||||
setError(err instanceof Error ? err.message : 'Failed to upload avatar');
|
||||
setError('Failed to upload avatar. Please try again.');
|
||||
}
|
||||
} finally {
|
||||
setUploading(false);
|
||||
|
||||
Reference in New Issue
Block a user