add error catches

This commit is contained in:
Kfir Dayan 2023-04-25 02:37:40 +03:00
parent 5f2fe8c77d
commit e52c9b5493
3 changed files with 86 additions and 13 deletions

View file

@ -1 +1,5 @@
GEOCODING_API_KEY='your_api_key' GEOCODING_API_KEY='your_api_key'
NODE_ENV=development
PGUSER=admin
PGHOST=admin

View file

@ -16,4 +16,69 @@ install pg MacOS -
brew install postgresql brew install postgresql
Start Postgres - Start Postgres -
brew services start postgresql brew services start postgresql
knex for migration files
npx knex migrate:make create_addresses_table
npx knex migrate:latest
npx knex migrate:rollback
npx knex seed:make 01_addresses
npx knex seed:run
npx knex migrate:make create_addresses_table
npx knex migrate:latest
npx knex migrate:rollback
npx knex seed:make 01_addresses
npx knex seed:run
npx knex migrate:make create_addresses_table
npx knex migrate:latest
npx knex migrate:rollback
npx knex seed:make 01_addresses
npx knex seed:run
npx knex migrate:make create_addresses_table
npx knex migrate:latest
npx knex migrate:rollback
npx knex seed:make 01_addresses
npx knex seed:run
npx knex migrate:make create_addresses_table
npx knex migrate:latest
npx knex migrate:rollback
npx knex seed:make 01_addresses
npx knex seed:run
npx knex migrate:make create_addresses_table
npx knex migrate:latest
npx knex migrate:rollback
npx knex seed:make 01_addresses
npx knex seed:run
npx knex migrate:make create_addresses_table
npx knex migrate:latest
npx knex migrate:rollback
npx knex seed:make 01_addresses
npx knex seed:run
npx knex migrate:make create_addresses_table
npx knex migrate:latest
npx knex migrate:rollback
npx knex seed:make 01_addresses
npx knex seed:run
npx knex migrate:make create_addresses_table
npx knex migrate:latest
npx knex migrate:rollback
npx knex seed:make 01_addresses
npx knex seed:run

View file

@ -9,17 +9,21 @@ const GEOCODING_API_KEY = process.env.GEOCODING_API_KEY;
// will return at least - street, line1, line2, country, postcode // will return at least - street, line1, line2, country, postcode
export const resolveAddress = async (searchTerm: string): Promise<Address> => { export const resolveAddress = async (searchTerm: string): Promise<Address> => {
const response = await axios.get(`https://api.geoapify.com/v1/geocode/search?text=${searchTerm}&format=json&apiKey=${GEOCODING_API_KEY}`); try {
if (response.data.results.length > 0) { const response = await axios.get(`https://api.geoapify.com/v1/geocode/search?text=${searchTerm}&format=json&apiKey=${GEOCODING_API_KEY}`);
const result = response.data.results[0]; if (response.data.results.length > 0) {
return { const result = response.data.results[0];
country: result.country, return {
street: result.street, country: result.country,
line1: result.address_line1, street: result.street,
line2: result.address_line2, line1: result.address_line1,
postcode: result.postcode line2: result.address_line2,
postcode: result.postcode
}
} else {
throw new Error('No results found');
} }
} else { } catch (error) {
throw new Error('No results found'); throw error.message;
} }
}; };