From d329989230504cd221a4b70d0aac31c3e3757b7a Mon Sep 17 00:00:00 2001 From: OllyNicholass Date: Tue, 5 Nov 2024 21:44:48 +0000 Subject: [PATCH] refactor(#65): move scope of logged params Type defs added where relevent --- src/routes/products/+server.ts | 36 ++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/routes/products/+server.ts b/src/routes/products/+server.ts index a60e5f6..02fe9d3 100644 --- a/src/routes/products/+server.ts +++ b/src/routes/products/+server.ts @@ -3,25 +3,35 @@ import { getActiveProductsWithPrices } from '$lib/utils/supabase/admin'; import logger from '$lib/utils/logger/logger'; export const GET = async ({ url }) => { + const limitParam = url.searchParams.get('limit'); + const offsetParam = url.searchParams.get('offset'); try { - const limitParam = url.searchParams.get('limit'); - const offsetParam = url.searchParams.get('offset'); - const limit = limitParam ? parseInt(limitParam, 10) : 10; const offset = offsetParam ? parseInt(offsetParam, 10) : 0; const { products, count } = await getActiveProductsWithPrices(limit, offset); return json({ products, count }); - } catch (error) { - logger.error('Failed to fetch products', { - error: error.message, - stack: error.stack, - params: { - limit: limitParam, - offset: offsetParam - } - }); - return json({ error: error.message }, { status: 500 }); + } catch (error: unknown) { + if (error instanceof Error) { + logger.error('Failed to fetch products', { + error: error.message, + stack: error.stack, + params: { + limit: limitParam, + offset: offsetParam + } + }); + return json({ error: error.message }, { status: 500 }); + } else { + logger.error('Failed to fetch products', { + error, + params: { + limit: limitParam, + offset: offsetParam + } + }); + return json({ error }, { status: 500 }); + } } };