forked from tanner/qotnews
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:
@@ -128,7 +128,7 @@ def submit():
|
|||||||
else:
|
else:
|
||||||
raise Exception('Invalid article')
|
raise Exception('Invalid article')
|
||||||
|
|
||||||
except BaseException as e:
|
except Exception as e:
|
||||||
logging.error('Problem with article submission: {} - {}'.format(e.__class__.__name__, str(e)))
|
logging.error('Problem with article submission: {} - {}'.format(e.__class__.__name__, str(e)))
|
||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
return {'error': str(e)}, 400
|
return {'error': str(e)}, 400
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ function Submit() {
|
|||||||
const inputRef = useRef(null);
|
const inputRef = useRef(null);
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
|
|
||||||
const submitArticle = (event) => {
|
const submitArticle = async (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
const url = event.target[0].value;
|
const url = event.target[0].value;
|
||||||
inputRef.current.blur();
|
inputRef.current.blur();
|
||||||
@@ -16,31 +16,25 @@ function Submit() {
|
|||||||
let data = new FormData();
|
let data = new FormData();
|
||||||
data.append('url', url);
|
data.append('url', url);
|
||||||
|
|
||||||
fetch('/api/submit', { method: 'POST', body: data })
|
try {
|
||||||
.then(res => {
|
const res = await fetch('/api/submit', { method: 'POST', body: data });
|
||||||
|
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
return res.json().then(data => ({ ok: true, data: data }));
|
const result = await res.json();
|
||||||
}
|
history.replace('/' + result.nid);
|
||||||
// Handle error responses
|
} else {
|
||||||
return res.json()
|
let errorData;
|
||||||
.then(data => ({ ok: false, data: data })) // Our API's JSON error
|
try {
|
||||||
.catch(() => {
|
errorData = await res.json();
|
||||||
|
} catch (jsonError) {
|
||||||
// Not a JSON error from our API, so it's a server issue
|
// Not a JSON error from our API, so it's a server issue
|
||||||
throw new Error(`Server responded with ${res.status} ${res.statusText}`);
|
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.');
|
|
||||||
}
|
}
|
||||||
},
|
setProgress(errorData.error || 'An unknown error occurred.');
|
||||||
(error) => {
|
}
|
||||||
|
} catch (error) {
|
||||||
setProgress(`Error: ${error.toString()}`);
|
setProgress(`Error: ${error.toString()}`);
|
||||||
}
|
}
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user