On an Angular 7 component I am calling a service:
this.postService.getByCategoryId(10)
.subscribe((response: GetPostsByCategoryIdResponse>) => {
this.posts = response.map((post: PostModel) => ({
id: response.id,
title: response.title,
category: response.category.name
}));
});
I am mapping the GetPostsByCategoryIdResponse, which mimics the data structure returned by the API, to a PostModel which is used in the component.
The PostService's GetByCategoryId is calling an API:
public GetByCategoryId(id: number): Observable<GetPostsByCategoryIdResponse> {
return this.httpClient.get<GetPostsByCategoryIdResponse>(url);
}
How to handle errors that might occurred when calling the API?