Error Handling

This page describes how to handle errors when using the Maps JavaScript API, and the Place class.

The Google Maps JavaScript API uses the following classes for errors:

  • MapsNetworkError represents a network error from a web service (can include RPCStatus errors).
  • MapsRequestError represents a request error from a web service (i.e. the equivalent of a 4xx code in HTTP).
  • MapsServerError represents a server-side error from a web service (i.e. the equivalent of a 5xx code in HTTP).

The MapsNetworkError, MapsRequestError, and MapsServerError classes belong to the maps core library. Learn more about libraries.

Each of these classes contains the following properties:

The code property identifies the type of error; the endpoint property identifies the endpoint that returned the error (for example PLACES_DETAILS). Since MapsNetworkError is a subclass of Error, other properties including name and message are also available.

The following snippet shows the structure of a Maps error message:

 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 ---> ... ]  

The raw error includes everything in the error string; error.message includes the entire error string excluding error.name.

The following snippet demonstrates error handling when using the Place class. This example uses a try/catch block to handle each of the three error types. Similar code can be used to handle errors for any Maps JavaScript API class.

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);  }  }  // ... }