Refactor: Continue implementation plan

This commit is contained in:
gpt-engineer-app[bot]
2025-10-21 12:56:53 +00:00
parent 74860c6774
commit 0d247d9fdd
10 changed files with 254 additions and 65 deletions

View File

@@ -1,8 +1,9 @@
import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
import { startRequest, endRequest } from "../_shared/logger.ts";
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type, x-request-id',
};
interface IPLocationResponse {
@@ -181,6 +182,8 @@ serve(async (req) => {
return new Response(null, { headers: corsHeaders });
}
const tracking = startRequest('detect-location');
try {
// Get the client's IP address
const forwarded = req.headers.get('x-forwarded-for');
@@ -252,15 +255,19 @@ serve(async (req) => {
console.log('[Location] Location detected:', {
country: result.country,
countryCode: result.countryCode,
measurementSystem: result.measurementSystem
measurementSystem: result.measurementSystem,
requestId: tracking.requestId
});
endRequest(tracking, 200);
return new Response(
JSON.stringify(result),
JSON.stringify({ ...result, requestId: tracking.requestId }),
{
headers: {
...corsHeaders,
'Content-Type': 'application/json'
'Content-Type': 'application/json',
'X-Request-ID': tracking.requestId
}
}
);
@@ -273,9 +280,12 @@ serve(async (req) => {
console.error('[Location Detection Error]', {
error: errorMessage,
stack: errorStack,
hasIP: true // IP removed for PII protection
hasIP: true, // IP removed for PII protection
requestId: tracking.requestId
});
endRequest(tracking, 500, errorMessage);
// Return default (metric) with 500 status to indicate error occurred
// This allows proper error monitoring while still providing fallback data
const defaultResult: IPLocationResponse = {
@@ -288,12 +298,14 @@ serve(async (req) => {
JSON.stringify({
...defaultResult,
error: errorMessage,
fallback: true
fallback: true,
requestId: tracking.requestId
}),
{
headers: {
...corsHeaders,
'Content-Type': 'application/json'
'Content-Type': 'application/json',
'X-Request-ID': tracking.requestId
},
status: 500
}