fix: Improve submit error handling on API and refactor client with async/await

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
2025-11-21 23:02:29 +00:00
parent 7b84573dd8
commit 3acaf230c4
2 changed files with 20 additions and 26 deletions

View File

@@ -128,7 +128,7 @@ def submit():
else:
raise Exception('Invalid article')
except BaseException as e:
except Exception as e:
logging.error('Problem with article submission: {} - {}'.format(e.__class__.__name__, str(e)))
print(traceback.format_exc())
return {'error': str(e)}, 400

View File

@@ -6,7 +6,7 @@ function Submit() {
const inputRef = useRef(null);
const history = useHistory();
const submitArticle = (event) => {
const submitArticle = async (event) => {
event.preventDefault();
const url = event.target[0].value;
inputRef.current.blur();
@@ -16,31 +16,25 @@ function Submit() {
let data = new FormData();
data.append('url', url);
fetch('/api/submit', { method: 'POST', body: data })
.then(res => {
if (res.ok) {
return res.json().then(data => ({ ok: true, data: data }));
try {
const res = await fetch('/api/submit', { method: 'POST', body: data });
if (res.ok) {
const result = await res.json();
history.replace('/' + result.nid);
} else {
let errorData;
try {
errorData = await res.json();
} catch (jsonError) {
// Not a JSON error from our API, so it's a server issue
throw new Error(`Server responded with ${res.status} ${res.statusText}`);
}
// Handle error responses
return res.json()
.then(data => ({ ok: false, data: data })) // Our API's JSON error
.catch(() => {
// Not a JSON error from our API, so it's a server issue
throw new Error(`Server responded with ${res.status} ${res.statusText}`);
});
})
.then(
({ ok, data }) => {
if (ok) {
history.replace('/' + data.nid);
} else {
setProgress(data.error || 'An unknown error occurred.');
}
},
(error) => {
setProgress(`Error: ${error.toString()}`);
}
);
setProgress(errorData.error || 'An unknown error occurred.');
}
} catch (error) {
setProgress(`Error: ${error.toString()}`);
}
}
return (