From 53e9dd02fc25426641c004cd0f286129385ab0e7 Mon Sep 17 00:00:00 2001 From: Jonas_Jones Date: Mon, 23 Dec 2024 03:41:14 +0100 Subject: [PATCH] fixed analytics request --- src/index.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index 20b4c93..5cbc7d8 100644 --- a/src/index.js +++ b/src/index.js @@ -50,8 +50,12 @@ export default { const record = { timestamp, domain, method, path, country }; - // Store the new record in the database - await this.storeRecordInDB(record, env); + try { + // Store the new record in the database + await this.storeRecordInDB(record, env); + } catch (error) { + return new Response('Error storing record in DB', { status: 500 }); + } return new Response('Recorded', { status: 200 }); } catch (error) { @@ -80,8 +84,12 @@ export default { const record = { timestamp, domain, method, path, country }; - // Store the new record in the database - await this.storeRecordInDB(record, env); + try { + // Store the new record in the database + await this.storeRecordInDB(record, env); + } catch (error) { + return new Response('Error storing record in DB', { status: 500 }); + } const origin = request.headers.get('Origin'); const isAllowedOrigin = ALLOWED_ORIGINS.includes(origin) || ALLOWED_ORIGINS.includes('localhost'); @@ -151,13 +159,9 @@ export default { // Store the new record in the database async storeRecordInDB(record, env) { - try { await env.DB.prepare('INSERT INTO requests (timestamp, domain, method, path, country) VALUES (?, ?, ?, ?, ?)') - .bind(record.timestamp, record.domain, record.method, record.path, record.country) - .run(); - } catch (error) { - console.error('Error storing record in DB:', error); - } + .bind(record.timestamp, record.domain, record.method, record.path, record.country) + .run(); }, // Retrieve filtered records from the database