0

If an observable inside a route resolver fails, the Angular router does not navigate to the route. I want to get around this by wrapping my call in another observable, which always successfully returns, like so:

private fetchUserDataOrReturnNullIfError(url: string, httpOptions: any): Observable<any> { const getLoggedInUserDataOrNullIfCallFails: Observable<any> = new Observable(observer => { this.httpClient.get(url, httpOptions).subscribe(userData => { console.log('Got user data: ', userData); observer.next(userData); }, err => { console.log('Call failed'); observer.next('lol'); }); }); return getLoggedInUserDataOrNullIfCallFails; } 

My resolve looks like this:

 resolve(): Observable<any> { //stuff and things return this.fetchUserDataOrReturnNullIfError(url, httpOptions); } 

My understanding was that the inner observable would error, and the outer observable would successfully call next, thereby the route would successfully resolve. This isn't happening.

What am I doing wrong / how do I solve this?

My goal is for the resolve to return the result of the call, if successful, and to return lol if failing, but to always go to the next route.

1 Answer 1

2

I overcomplicate the **** out of that. The answer is:

return this.httpClient.get(url, httpOptions).pipe( catchError(err => of(null)) ); 

catchError must return a new Observable, and that gets passed on to the resolver. I am actually still not sure why my original method didn't work, but it could have something to do with the fact that I am using old syntax and RxJS compat.

Source: https://blog.angular-university.io/rxjs-error-handling/

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.