GROUP_API_ERROR_HANDLING_FIX
Version Implemented: 0.229.001
Issue Description
When users have group workspaces enabled but haven't selected an active group yet, the chat interface was generating JavaScript console errors from failed API calls to /api/group_documents and /api/group_prompts. These endpoints return HTTP 400 status with "No active group selected" error messages, but the JavaScript wasn't handling these specific error conditions gracefully.
Fixed in version: 0.227.009
Root Cause Analysis
- Backend Behavior: Both group API endpoints (
/api/group_documentsand/api/group_prompts) require an active group to be selected and return HTTP 400 with specific error messages when no active group is available - JavaScript Error Handling: The original
loadGroupDocs()andloadGroupPrompts()functions were directly calling.json()on the response without checking HTTP status codes first - User Experience Impact: Users would see console errors even though the application functionality was working correctly
Technical Details
Files Modified
static/js/chat/chat-documents.jsstatic/js/chat/chat-prompts.jsconfig.py(version update)
Backend API Behavior (Preserved)
The backend APIs correctly return:
- Group Documents:
{'error': 'No active group selected'}with HTTP 400 - Group Prompts:
{"error":"No active group selected"}with HTTP 400
These APIs require:
- User authentication (
@login_required) - User validation (
@user_required) - Feature enabled (
@enabled_required("enable_group_workspaces")) - Active group selected in user settings
JavaScript Improvements
Before (Problematic Code)
// chat-documents.js
export function loadGroupDocs() {
return fetch("/api/group_documents?page_size=1000")
.then((r) => r.json()) // ❌ Direct .json() call without status check
.then((data) => {
// Handle data...
})
.catch((err) => {
console.error("Error loading group docs:", err);
groupDocs = [];
});
}
// chat-prompts.js
export function loadGroupPrompts() {
return fetch("/api/group_prompts")
.then(r => r.json()) // ❌ Direct .json() call without status check
.then(data => {
// Handle data...
})
.catch(err => console.error("Error loading group prompts:", err));
}
After (Improved Code)
// chat-documents.js
export function loadGroupDocs() {
return fetch("/api/group_documents?page_size=1000")
.then((r) => {
if (!r.ok) {
// Handle 400 errors gracefully (e.g., no active group selected)
if (r.status === 400) {
console.log("No active group selected for group documents");
groupDocs = [];
return { documents: [] }; // Return empty result to avoid further errors
}
throw new Error(`HTTP ${r.status}: ${r.statusText}`);
}
return r.json();
})
.then((data) => {
// Handle data...
})
.catch((err) => {
console.error("Error loading group docs:", err);
groupDocs = [];
});
}
// chat-prompts.js
export function loadGroupPrompts() {
return fetch("/api/group_prompts")
.then(r => {
if (!r.ok) {
// Handle 400 errors gracefully (e.g., no active group selected)
if (r.status === 400) {
console.log("No active group selected for group prompts");
groupPrompts = [];
return { prompts: [] }; // Return empty result to avoid further errors
}
throw new Error(`HTTP ${r.status}: ${r.statusText}`);
}
return r.json();
})
.then(data => {
// Handle data...
})
.catch(err => console.error("Error loading group prompts:", err));
}
Benefits
- Improved User Experience: No more JavaScript console errors when groups are enabled but no active group is selected
- Better Error Messaging: Clear, informative console logging instead of cryptic JSON parsing errors
- Graceful Degradation: Application continues to function normally with empty group data arrays
- Consistent Error Handling: Both group APIs now handle the "no active group" scenario consistently
Validation
The fix includes comprehensive functional testing (test_group_error_handling_fix.py) that validates:
- ✅ Proper HTTP status checking in JavaScript
- ✅ Specific 400 error handling for "no active group" scenarios
- ✅ Informative console logging instead of errors
- ✅ Array reset on error conditions
- ✅ Proper empty result structure return
- ✅ Backend API error conditions remain intact
- ✅ Version consistency in config.py
User Impact
- Before: Users would see JavaScript console errors when groups were enabled but no active group selected
- After: Clean console output with informative logging, no errors visible to users
- Functionality: No change in application behavior, only improved error handling
Integration Notes
This fix works seamlessly with the existing group workspace functionality:
- When a user selects an active group, the APIs will work normally
- When no group is selected, the JavaScript gracefully handles the 400 responses
- The group document and prompt dropdowns will show empty states appropriately
- No changes required to backend logic or API contracts
Testing Recommendations
When testing group workspaces:
- Enable group workspaces in settings
- Visit chat interface before selecting any group
- Check browser console - should show informative logs instead of errors
- Select an active group - functionality should work normally
- Deselect active group - should gracefully return to empty state without errors