Backend added error handling on invalid shortener code

pull/3/head
TZGyn 2 years ago
parent c8fff9574c
commit 1f549ac0c4
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

@ -57,27 +57,32 @@ app.get(
await fetch(`https://api.ipbase.com/v2/info?ip=${ip}`) await fetch(`https://api.ipbase.com/v2/info?ip=${ip}`)
).json() ).json()
const shortener = await db try {
.selectFrom('shortener') const shortener = await db
.selectAll() .selectFrom('shortener')
.where('code', '=', shortenerCode) .selectAll()
.orderBy('created_at', 'desc') .where('code', '=', shortenerCode)
.execute() .orderBy('created_at', 'desc')
.execute()
const visitor_data = {
shortener_id: shortener[0].id, const visitor_data = {
country: geolocation.data.location.country.name as string, shortener_id: shortener[0].id,
country_code: geolocation.data.location.country.alpha2 as string, country: geolocation.data.location.country.name as string,
country_code: geolocation.data.location.country
.alpha2 as string,
}
await db.insertInto('visitor').values(visitor_data).execute()
if (!shortener.length) {
set.redirect = '/invalid'
return
}
set.redirect = shortener[0].link
} catch {
set.redirect = Bun.env.FALLBACK_URL
} }
await db.insertInto('visitor').values(visitor_data).execute()
if (!shortener.length) {
set.redirect = '/invalid'
return
}
set.redirect = shortener[0].link
} }
) )

Loading…
Cancel
Save