# 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/groupdocuments and /api/groupprompts.

- Skill: `tools-only/issue-description-6` (Agent Skill, multi-file: 3 files)
- Install (CLI): `npx skillmds@latest add tools-only/issue-description-6`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tools-only/issue-description-6/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: tools-only (https://skillmd.com/u/tools-only)
- Updated: 2026-09-29
- Page: https://skillmd.com/skills/tools-only/issue-description-6

---

# 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
1. **Backend Behavior**: Both group API endpoints (`/api/group_documents` and `/api/group_prompts`) require an active group to be selected and return HTTP 400 with specific error messages when no active group is available
2. **JavaScript Error Handling**: The original `loadGroupDocs()` and `loadGroupPrompts()` functions were directly calling `.json()` on the response without checking HTTP status codes first
3. **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.js`
- `static/js/chat/chat-prompts.js`
- `config.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)
```javascript
// 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)
```javascript
// 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
1. **Improved User Experience**: No more JavaScript console errors when groups are enabled but no active group is selected
2. **Better Error Messaging**: Clear, informative console logging instead of cryptic JSON parsing errors
3. **Graceful Degradation**: Application continues to function normally with empty group data arrays
4. **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:
1. Enable group workspaces in settings
2. Visit chat interface before selecting any group
3. Check browser console - should show informative logs instead of errors
4. Select an active group - functionality should work normally
5. Deselect active group - should gracefully return to empty state without errors

