I am retrieving data from a JSON file by making an HTTP request. Once I subscribe to the data, I read the JSON content. In this scenario, my goal is to verify whether the path is incorrect and if so, return a 404 error message.
getMapConfig(): Observable<MapReportConfigModel> {
let mapConfigData = `assets/mapReportConfig1.json`;
return this.httpClient.get<MapReportConfigModel>(mapConfigData);
}
Load map function
loadMap(): void {
try {
this.mapReportConfigModel
.getMapConfig()
.subscribe((configData) => {
this.configData = configData;
// Initialize map
mapboxgl!.accessToken = this.configData.accessToken;
this.map = new mapboxgl.Map({
container: this.configData.container,
center: [this.configData.centerLat, this.configData.centerLng],
});
// Check if API key is incorrect.
this.map.on("error", (response) => {
this.showError(
"500",
"mapReport.err_no_map_reports",
"mapReport.err_generating_map_report_message"
).subscribe(() => {
this.router.navigateByUrl("/dashboard");
return;
});
});
})
.add(() => {
// Hide loading spinner
this.loadingService.hide(processId);
});
} catch (error) {
return null;
}
}
In case of a 404 error, I aim to handle it within the catch block.