I'm trying to capture all errors from the fetch
function, including the specific red highlighted details as a string:
https://i.sstatic.net/GtHxv.png
But when I catch an error in my code, all I get is "Failed to fetch." Here's what my code looks like:
try {
response = await fetch(url, {
method: method,
body: JSON.stringify(payload),
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw new Error(`RESPONSE NOT OK: ${response.status}`);
}
console.info('got 200 json response:' + (await response?.json()));
} catch (e) {
console.error('CATCH BLOCK:' + e.message); // only gives me "Failed to fetch"
}
So, how can I retrieve the complete error message from the fetch request in a string format so that it can be displayed directly on my web interface?
Update
Just to clarify, I already know how to solve the error itself. This question focuses solely on extracting the full error message as a string.