I am trying to handle errors in angular. Not sure where exactly it should be handled, but I tried to do it in service.
@Injectable({
providedIn: 'root',
})
export class CaseListService {
public constructor(private httpClient: HttpClient, private router: Router) {}
private get baseUrl() {
return environment.apiBaseUrl || '/api';
}
public getCaseList(): Observable<CaseListModel[]> {
return this.httpClient.get(this.baseUrl + '/cases').pipe(
map((response) => response as Array<CaseListModel>),
catchError((err: any) => {
if (err.status) {
console.log('Service temporarily unavailable' + err.status);
this.router.navigate(['/unavailable']);
}
return throwError(err.statusText);
})
);
}
}
And here is my component.ts where I am calling the service:
@Component({
selector: 'app-case-list',
templateUrl: './case-list.component.html',
styleUrls: ['./case-list.component.css'],
})
export class CaseListComponent implements OnInit {
title = 'List of cases';
readonly CaseList$: Observable<CaseListModel[]>;
constructor(private caseListService: CaseListService) {
this.CaseList$ = caseListService.getCaseList();
}
displayedColumns: string[] = ['ID', 'name', 'actions'];
dataSource = this.caseListService.getCaseList();
ngOnInit(): void {}
}
I disabled backend and tried it. Result is just frontend with empty data. Redirect fails, console log shows nothing too. What am I doing wrong?