I'm currently facing a challenge in detecting request timeouts with Angular http and promises. My code is designed to format API responses, but it fails to handle timeout errors effectively. Despite working when the API returns an error message, it does not respond when the API times out.
My search for documentation on handling timeouts in Angular http has been fruitless so far. Any guidance or solutions would be greatly appreciated, thank you!
Here's the relevant code snippet:
/**
* GET from the API
* @param params What to send
* @param path Where to go
*/
public get(path, params): Promise<any> {
return this.formatResponse(
this.http.get(this.apiUrl + path, params)
);
}
/**
* Takes the API response and converts it to something usable
* @param response Promise of formatted data
*/
public formatResponse(response): Promise<any> {
return response
.toPromise()
.then(r => r.json().data)
.catch(e => {
console.log('hitting error');
const errors = e.json().errors;
let error = 'Something went wrong, please try again in a few minutes.';
if (errors && errors.length > 0) {
error = errors[0].message;
}
// Create alert
this.utilities.createAlert(
'Whoops!',
error
);
// Throw the error
throw new Error(error);
});
}
Observing the network tab, I notice the following message:
Failed to load resource: The request timed out.