Bu sayfada, Maps JavaScript API ve Place sınıfı kullanılırken hataların nasıl ele alınacağı açıklanmaktadır.
Google Maps JavaScript API, hatalar için aşağıdaki sınıfları kullanır:
-
MapsNetworkError
bir web hizmetinden kaynaklanan ağ hatasını gösterir (RPCStatus hatalarını içerebilir). -
MapsRequestError
bir web hizmetinden gelen istek hatasını (ör. HTTP'deki 4xx koduna eşdeğer) temsil eder. -
MapsServerError
bir web hizmetinden kaynaklanan sunucu tarafı hatasını (ör. HTTP'deki 5xx koduna eşdeğer) temsil eder.
MapsNetworkError
, MapsRequestError
ve MapsServerError
sınıfları Maps Core Kitaplığı'na aittir.
Kitaplıklar hakkında daha fazla bilgi edinin.
Bu sınıfların her biri aşağıdaki özellikleri içerir:
code
özelliği hata türünü, endpoint
özelliği ise hatayı döndüren uç noktayı (ör. PLACES_DETAILS
) tanımlar. MapsNetworkError
, Error
alt sınıfı olduğundan name
ve message
gibi diğer özellikler de kullanılabilir.
Aşağıdaki snippet'te bir Haritalar hata mesajının yapısı gösterilmektedir:
MapsRequestError: PLACES_GET_PLACE: INVALID_ARGUMENT: Error fetching fields: The provided Place ID: ChIJN5Nz71W3j4ARhx5bwpTQEGg**** is not valid. [error.name ] [error.endpoint ] [error.code ] [error.message ---> ... ]
Ham hata, hata dizesindeki her şeyi içerir. error.message
, error.name
hariç olmak üzere hata dizesinin tamamını içerir.
Aşağıdaki snippet, Place sınıfı kullanılırken hata işlemeyi gösterir. Bu örnekte, üç hata türünün her birini işlemek için bir try/catch bloğu kullanılır. Benzer kodlar, herhangi bir Maps JavaScript API sınıfındaki hataları işlemek için kullanılabilir.
async function getPlaceDetails() { const { Place } = await google.maps.importLibrary("places") as google.maps.PlacesLibrary; const { MapsNetworkError, MapsRequestError, MapsServerError } = await google.maps.importLibrary("core") as google.maps.CoreLibrary; // Use place ID to create a new Place instance. const place = new Place({ id: 'ChIJN5Nz71W3j4ARhx5bwpTQEGg****', // Pass a bad Place ID to trigger an error. }); // Error handling for fetchFields. try { // Call fetchFields, passing the desired data fields. await place.fetchFields({ fields: ['displayName', 'formattedAddress', 'location'] }); } catch (error: any) { if (error && error instanceof google.maps.MapsRequestError) { // HTTP 4xx request error. console.error('fetchFields failed: MapsRequestError - check the request parameters', error); } else if (error && error instanceof google.maps.MapsServerError) { // HTTP 5xx server-side error. console.error('fetchFields failed: MapsServerError', error); } else if (error && error instanceof google.maps.MapsNetworkError) { // Network error. console.error('fetchFields failed: MapsNetworkError', error); } else { console.error('fetchFields failed: An unknown error occurred', error); } } // ... }