Fix: Add graceful error handling for territories not in World Bank API
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { HttpClient } from '@angular/common/http';
|
import { HttpClient } from '@angular/common/http';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable, of } from 'rxjs';
|
||||||
import { map } from 'rxjs/operators';
|
import { map, catchError } from 'rxjs/operators';
|
||||||
|
|
||||||
export interface CountryData {
|
export interface CountryData {
|
||||||
name: string;
|
name: string;
|
||||||
@@ -25,6 +25,11 @@ export class WorldBankService {
|
|||||||
|
|
||||||
return this.http.get<any>(url).pipe(
|
return this.http.get<any>(url).pipe(
|
||||||
map(response => {
|
map(response => {
|
||||||
|
// Check if API returned an error message
|
||||||
|
if (response[0]?.message) {
|
||||||
|
throw new Error(`World Bank API: ${response[0].message[0].value}`);
|
||||||
|
}
|
||||||
|
|
||||||
const country = response[1][0];
|
const country = response[1][0];
|
||||||
return {
|
return {
|
||||||
name: country.name || 'N/A',
|
name: country.name || 'N/A',
|
||||||
@@ -34,6 +39,18 @@ export class WorldBankService {
|
|||||||
longitude: country.longitude || 'N/A',
|
longitude: country.longitude || 'N/A',
|
||||||
latitude: country.latitude || 'N/A'
|
latitude: country.latitude || 'N/A'
|
||||||
};
|
};
|
||||||
|
}),
|
||||||
|
catchError(error => {
|
||||||
|
console.error(`Failed to fetch data for ${countryCode}:`, error);
|
||||||
|
// Return placeholder data instead of failing completely
|
||||||
|
return of({
|
||||||
|
name: `Country Code: ${countryCode}`,
|
||||||
|
capital: 'Data not available',
|
||||||
|
region: 'Not available in World Bank API',
|
||||||
|
incomeLevel: 'N/A',
|
||||||
|
longitude: 'N/A',
|
||||||
|
latitude: 'N/A'
|
||||||
|
});
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user