nomandiu9 commited on
Commit
2463705
·
1 Parent(s): b7212fd

Better error parsing in frontend

Browse files
Files changed (1) hide show
  1. index.html +16 -3
index.html CHANGED
@@ -115,11 +115,24 @@
115
  body: JSON.stringify({ text: text })
116
  });
117
 
118
- if (!response.ok) {
119
- throw new Error('Network response was not ok');
 
 
 
 
 
 
 
 
 
 
 
120
  }
121
 
122
- const data = await response.json();
 
 
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);