Spaces:
Sleeping
Sleeping
Better error parsing in frontend
Browse files- index.html +16 -3
index.html
CHANGED
|
@@ -115,11 +115,24 @@
|
|
| 115 |
body: JSON.stringify({ text: text })
|
| 116 |
});
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
}
|
| 121 |
|
| 122 |
-
|
|
|
|
|
|
|
| 123 |
|
| 124 |
if (data.predicted_sentiment) {
|
| 125 |
displayResult(data.predicted_sentiment);
|
|
|
|
| 115 |
body: JSON.stringify({ text: text })
|
| 116 |
});
|
| 117 |
|
| 118 |
+
let data;
|
| 119 |
+
const contentType = response.headers.get("content-type");
|
| 120 |
+
|
| 121 |
+
if (contentType && contentType.indexOf("application/json") !== -1) {
|
| 122 |
+
data = await response.json();
|
| 123 |
+
} else {
|
| 124 |
+
// If not JSON, probably an error page
|
| 125 |
+
const text = await response.text();
|
| 126 |
+
console.error('Non-JSON response:', text);
|
| 127 |
+
if (!response.ok) {
|
| 128 |
+
throw new Error('Server Error: ' + response.status + ' ' + response.statusText);
|
| 129 |
+
}
|
| 130 |
+
data = {}; // safe fallback
|
| 131 |
}
|
| 132 |
|
| 133 |
+
if (!response.ok) {
|
| 134 |
+
throw new Error(data.detail || 'Network response was not ok');
|
| 135 |
+
}
|
| 136 |
|
| 137 |
if (data.predicted_sentiment) {
|
| 138 |
displayResult(data.predicted_sentiment);
|