+ await fetch(api_url, { headers: Nominatim_Config.Nominatim_API_Endpoint_Headers || {} })
+ .then(async (response) => {
+ if ((!((response.status >= 200 && response.status < 300) || response.status === 404))
+ || mock_api_error === 'fetch'
+ ) {
+ error_store.set(`Error fetching data from ${api_url} (${response.statusText})`);
+ return undefined;
+ }
+
+ // Parse JSON here instead of returning a promise so we can catch possible
+ // errors.
+ var data;
+ try {
+ if (mock_api_error === 'parse') {
+ data = JSON.parse('{');
+ } else {
+ data = await response.json();
+ }
+ } catch (err) {
+ // e.g. 'JSON.parse: unexpected non-whitespace character after JSON data at line 1'
+ error_store.set(`Error parsing JSON data from ${api_url} (${err})`);
+ return undefined;
+ }
+ return data;
+ })
+ .then((data) => {
+ if (data) {
+ if (data.error) {
+ error_store.set(data.error.message);
+ }
+ callback(data);