Error handling when fetching addresses.
This commit is contained in:
parent
9e3915c6ce
commit
69148f6e1d
|
@ -1,27 +1,36 @@
|
||||||
import memoize from 'memoizee';
|
import memoize from 'memoizee';
|
||||||
|
|
||||||
const _findAddress = async (lon: number, lat: number, locale: () => string) => {
|
export const _findAddress = async (
|
||||||
const response = await fetch(
|
lon: number,
|
||||||
`https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=jsonv2&addressdetails=1&extratags=1&namedetails=1&accept-language=${locale()}`,
|
lat: number,
|
||||||
{}
|
locale: () => string
|
||||||
);
|
) => {
|
||||||
const data = await response.json();
|
try {
|
||||||
console.log({ caller: 'findAddress', lon, lat, data });
|
const response = await fetch(
|
||||||
return data;
|
`https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=jsonv2&addressdetails=1&extratags=1&namedetails=1&accept-language=${locale()}`,
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
const data = await response.json();
|
||||||
|
console.log({ caller: 'findAddress', lon, lat, data });
|
||||||
|
return data;
|
||||||
|
} catch (error) {
|
||||||
|
console.error({ caller: 'findAddress', lon, lat, error });
|
||||||
|
return {};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const findAddress = memoize(_findAddress, {
|
export const findAddress = memoize(_findAddress, {
|
||||||
promise: true,
|
promise: true,
|
||||||
max: 1000,
|
max: 1000,
|
||||||
maxAge: 360000000,
|
maxAge: 36000000,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const getVillageOrTown = (address: any) => {
|
export const getVillageOrTown = (address: any) => {
|
||||||
if (address?.address.village) {
|
if (address?.address?.village) {
|
||||||
return address?.address.village;
|
return address.address.village;
|
||||||
}
|
}
|
||||||
if (address?.address.city) {
|
if (address?.address?.city) {
|
||||||
return address?.address.city;
|
return address.address.city;
|
||||||
}
|
}
|
||||||
return address?.address.town;
|
return address?.address?.town;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue