Issue
I have a service that will return http query, my caller will by default receive the result and assign it to a News[] object.
this.service.getNews(url).subscribe((res) => this.news = res);
now I want to throw a custom error if any error occured:
getNews(url:string): Observable<News[]> {
return this.http.get<News[]>(url).catch(err => {
throw new Error("My Custom Error");
});
But I got a compling error that "cannot assign the {} to a Observable < News[] >" because the error is not a type of News[].
So how can I modify the code to return an error without changing the returning type Observable < News[] >?
Solution
Instead of throwing your error, try returning an observable of it using Observable.throw
.
The docs specifically remind to return an observable from the catch
operator.
Answered By - Mikhail Burshteyn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.