Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Http timeout

I have a problem posting a custom error message on HTTP timeout.

Here is the simple example:

return this._http.get(url).timeout(5000, new Error("Error message"));

I saw that everybody uses new Error("Error message") but I'm getting the error:

Error function expects type Scheduler. I'm getting this error: Argument of type 'Error' is not assignable to parameter of type 'Scheduler'. Property 'SchedulerAction' is missing in type 'Error'

like image 930
Igor Janković Avatar asked Dec 19 '22 10:12

Igor Janković


1 Answers

In rxjs 4, it was possible to customise the error message that way. However, in latest versions of rxjs 5, timeout only accepts two arguments:

  • due: number | Date
  • scheduler: IScheduler (this is to manage how the Observable processes the timeout)

If you want to customise your error, you may try something like:

return this._http.get(url)
 .timeout(5000)
 .catch(err => {

   if (err.name !== "TimeoutError") {
      return Observable.throw("Timeout has occurred");
   }

   return Observable.throw(err);

});
like image 130
cortopy Avatar answered Dec 30 '22 16:12

cortopy