Skip to content

Commit

Permalink
Report actual server errors in via JS
Browse files Browse the repository at this point in the history
  • Loading branch information
asdil12 committed Jan 27, 2025
1 parent 8e8bddc commit d62514b
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions assets/javascripts/needleeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,11 +502,23 @@ function saveNeedle(overwrite) {

fetchWithCSRF(form.action, {method: 'POST', body: new FormData(form)})
.then(response => {
if (!response.ok) throw `Server returned ${response.status}: ${response.statusText}`;
return response.json();
return response
.json()
.then(json => {
// Attach the parsed JSON to the response object for further use
return {response, json};
})
.catch(() => {
// If parsing fails, handle it as a non-JSON response
throw `Server returned ${response.status}: ${response.statusText}`;
});
})
.then(({response, json}) => {
if (!response.ok) throw `Server returned ${response.status}: ${response.statusText}<br>${json.error || ''}`;
if (json.error) throw json.error;
return json;
})
.then(response => {
if (response.error) throw response.error;
if (response.requires_overwrite) {
delete response.requires_overwrite;
const modalElement = document.getElementById('modal-overwrite');
Expand Down

0 comments on commit d62514b

Please sign in to comment.