From 1f549ac0c464c43bfb71ddcca2d5e09aa06570d6 Mon Sep 17 00:00:00 2001 From: TZGyn Date: Tue, 26 Sep 2023 05:13:35 +0800 Subject: [PATCH] Backend added error handling on invalid shortener code --- elysia/src/index.ts | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/elysia/src/index.ts b/elysia/src/index.ts index acf0741..0322711 100644 --- a/elysia/src/index.ts +++ b/elysia/src/index.ts @@ -57,27 +57,32 @@ app.get( await fetch(`https://api.ipbase.com/v2/info?ip=${ip}`) ).json() - const shortener = await db - .selectFrom('shortener') - .selectAll() - .where('code', '=', shortenerCode) - .orderBy('created_at', 'desc') - .execute() - - const visitor_data = { - shortener_id: shortener[0].id, - country: geolocation.data.location.country.name as string, - country_code: geolocation.data.location.country.alpha2 as string, + try { + const shortener = await db + .selectFrom('shortener') + .selectAll() + .where('code', '=', shortenerCode) + .orderBy('created_at', 'desc') + .execute() + + const visitor_data = { + shortener_id: shortener[0].id, + 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 } )