I am working on an Angular app in which I am making a rest call through HTTP as below:
login(email, password) {
let headers = new Headers();
headers.a
rxjs 5
Either
throw response;
or
throw Observable.throw(response);
Usually when you're throwing an error you'll be doing so at the exact moment the problem occurred and you want to raise it immediately, but this may not always be the case.
For instance there is the timeoutWith()
operator, which is perhaps one of the most likely reasons you'll need to do this.
results$ = server.getResults().pipe(timeoutWith(10000, ....) )
This takes an 'error factory', which is a function.
errorFactory = () => 'Your error occurred at exactly ' + new Date()
eg.
results$ = server.searchCustomers(searchCriteria).pipe(timeoutWith(10000,
() => 'Sorry took too long for search ' + JSON.stringify(searchCriteria)) )
Note that when using timeoutWith
you'll never get the actual server response back - so if the server gave a specific error you'd never see it. This above example can be very useful in debugging, but be sure not to display the error to the end user if you use the above example.
AN error factory is helpful because it doesn't evaluate the code until the actual error occurs. So you can put 'expensive' or debugging operations inside that will get executed when the error is actually finally needed.
If you need to use a 'factory' to create your error somewhere other than in timeout you can use the following.
EMPTY.pipe(throwIfEmpty(errorFactory))