The following JavaScript code sends a file to an embedded LWIP HTTP server in chunks.
As can be seen in the Wireshark trace, a reset sometimes occurs during transmission. In this case, the request is aborted, however the script does not receive any information from this and returns true even in case of an error.
Does anyone have a tip on how to teach the script to recognize such an error?
The function should only return true if the request was successful, otherwise the call will repeated again with the same chunk.
async function uploadChunk(chunk) {
let result = false;
const formData = new FormData();
formData.append("file", chunk);
sleep(10);
try {
const response = await fetch("http://" + target_ip + "/update", {
method: "POST",
mode: "no-cors",
body: formData
});
res = await response;
const ok_string = "OK";
result = (response.ok & (response.statusText === ok_string));
console.log(res);
} catch (error) {
console.error("Failed to upload chunk: ", error);
}
return result;
}

fetchonly throws an error if a network-level failure happens. You need to implement response handling yourself. Start by checking theresponsecode - similar to how you already do withresponse.okandresponse.statusTextres = await response;looks oddhe function should only return truethe function won't ever returntrue... it could return false, but it could also return0or1... but nevertrue- becausetrue & falseis0andtrue & trueis1- did you meanresponse.ok && .....etc? - are you checking if the returned value isfalseand assuming if it's not then there was no issue?mode:no-corsin the request.