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:
@@ -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();
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user