4

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'

4 Answers 4

9

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);

});
Sign up to request clarification or add additional context in comments.

1 Comment

Why has a timeout occurred when the name of the error is NOT "TimeoutError"
2

You should be able to do that with timeoutWith:

return this._http.get(url).timeoutWith(5000, Observable.throw(new Error("Error message")));

Comments

0

As of version rc.5, timeout no longer accepts the errorToSend argument.

https://github.com/ReactiveX/rxjs/blob/master/CHANGELOG.md#500-rc5-2016-12-07

Comments

0

You can use (rxjs 6.6.3)

import { timeoutWith } from 'rxjs/operators'
import { throwError } from 'rxjs'
// ...
return this.http.get(url).pipe(timeoutWith(5000, throwError(new Error("Error message"))))
// ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.