Fix API routes to return JSON instead of HTML on errors

- Modified exception handlers in app/main.py to check if request path starts with /api/
- HTTPException handler returns JSON for API routes, HTML for frontend routes
- General exception handler (500) also checks and returns appropriate format
- Enhanced frontend deleteFile() to handle non-JSON responses gracefully
- Added content-type checking before parsing JSON
- Added comprehensive tests for API error handling

Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-02-07 19:18:42 +00:00
parent 2b50d164f0
commit 74724973d8
3 changed files with 192 additions and 10 deletions
+14 -3
View File
@@ -501,9 +501,20 @@
})
.then(response => {
if (!response.ok) {
return response.json().then(err => {
throw new Error(err.detail || 'Failed to delete file');
});
// Try to parse JSON error response, but handle non-JSON gracefully
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
return response.json().then(err => {
throw new Error(err.detail || 'Failed to delete file');
});
} else {
// Non-JSON response (likely HTML error page)
return response.text().then(text => {
// Extract a readable error message
const statusText = response.statusText || 'Error';
throw new Error(`${statusText} (${response.status}): Server returned non-JSON response`);
});
}
}
return response.json();
})