I have been reading up on fetch()
and how to catch and print a readable error message from the server. Ideally I would like to throw an error that always ends up in Catch 2
in my example below and that console.log(`OK: ${data}`);
is not ran if there is an error. I can mitigate console.log(`OK: ${data}`);
by running then
directly on response.json();
instead but I would like to know the proper way of achieving this.
https://stackoverflow.com/a/44576265/3850405
https://developers.google.com/web/updates/2015/03/introduction-to-fetch
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
C#:
[HttpGet, Route("api/specific/catalog/test")]
public async Task<IHttpActionResult> Test()
{
return InternalServerError(new Exception("My Exception"));
}
[HttpGet, Route("api/specific/catalog/test2")]
public async Task<IHttpActionResult> Test2()
{
return Ok("My OK Message");
}
Typescript:
fetch('api/specific/catalog/test2')
.then(response => {
if (!response.ok) {
response.text().then(text => {
throw new Error(`Request rejected with status ${response.status} and message ${text}`);
})
.catch(error =>
console.log(`Catch 1: ${error}`)
);
}
else {
return response.json();
}
})
.then(data => {
console.log(`OK: ${data}`);
})
.catch(error =>
console.log(`Catch 2: ${error}`)
);
OK:
Exception:
I guess I could do something like this to catch all errors but it seems weird:
fetch('api/specific/catalog/test')
.then(response => {
if (!response.ok) {
response.text().then(text => {
throw new Error(`Request rejected with status ${response.status} and message ${text}`);
})
.catch(error =>
console.log(`Catch: ${error}`)
);
}
else {
return response.json().then(data => {
console.log(`OK: ${data}`);
})
.catch(error =>
console.log(`Catch 2: ${error}`)
);
}
})
.catch(error =>
console.log(`Catch 3: ${error}`)
);
The problem is that you swallow the error inside then, also you don't need multiple catches you only need one at the end like this:
fetch('api/specific/catalog/test')
.then(response => {
if (!response.ok) {
return response.text().then(text => {
throw new Error(`Request rejected with status ${response.status} and message ${text}`);
})
}
else {
return response.json()
}
})
.then(data => {
console.log(`OK: ${data}`);
})
.catch(error =>
console.log(`Catch 3: ${error}`)
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With