mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 13:31:12 -05:00
Update the detect-location Supabase function to return a 500 status code and an error message on failure, improving debugging and error monitoring. Replit-Commit-Author: Agent Replit-Commit-Session-Id: fe5b902e-beda-40fc-bf87-a3c4ab300e3a Replit-Commit-Checkpoint-Type: intermediate_checkpoint
105 lines
3.2 KiB
TypeScript
105 lines
3.2 KiB
TypeScript
import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
|
|
|
|
const corsHeaders = {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
|
};
|
|
|
|
interface IPLocationResponse {
|
|
country: string;
|
|
countryCode: string;
|
|
measurementSystem: 'metric' | 'imperial';
|
|
}
|
|
|
|
serve(async (req) => {
|
|
// Handle CORS preflight requests
|
|
if (req.method === 'OPTIONS') {
|
|
return new Response(null, { headers: corsHeaders });
|
|
}
|
|
|
|
try {
|
|
// Get the client's IP address
|
|
const forwarded = req.headers.get('x-forwarded-for');
|
|
const realIP = req.headers.get('x-real-ip');
|
|
const clientIP = forwarded?.split(',')[0] || realIP || '8.8.8.8'; // fallback to Google DNS for testing
|
|
|
|
console.log('Detecting location for IP:', clientIP);
|
|
|
|
// Use configurable geolocation service with proper error handling
|
|
// Defaults to ip-api.com if not configured
|
|
const geoApiUrl = Deno.env.get('GEOLOCATION_API_URL') || 'http://ip-api.com/json';
|
|
const geoApiFields = Deno.env.get('GEOLOCATION_API_FIELDS') || 'status,country,countryCode';
|
|
|
|
let geoResponse;
|
|
try {
|
|
geoResponse = await fetch(`${geoApiUrl}/${clientIP}?fields=${geoApiFields}`);
|
|
} catch (fetchError) {
|
|
console.error('Network error fetching location data:', fetchError);
|
|
throw new Error('Network error: Unable to reach geolocation service');
|
|
}
|
|
|
|
if (!geoResponse.ok) {
|
|
throw new Error(`Geolocation service returned ${geoResponse.status}: ${geoResponse.statusText}`);
|
|
}
|
|
|
|
let geoData;
|
|
try {
|
|
geoData = await geoResponse.json();
|
|
} catch (parseError) {
|
|
console.error('Failed to parse geolocation response:', parseError);
|
|
throw new Error('Invalid response format from geolocation service');
|
|
}
|
|
|
|
if (geoData.status !== 'success') {
|
|
throw new Error(`Geolocation failed: ${geoData.message || 'Invalid location data'}`);
|
|
}
|
|
|
|
// Countries that primarily use imperial system
|
|
const imperialCountries = ['US', 'LR', 'MM']; // USA, Liberia, Myanmar
|
|
const measurementSystem = imperialCountries.includes(geoData.countryCode) ? 'imperial' : 'metric';
|
|
|
|
const result: IPLocationResponse = {
|
|
country: geoData.country,
|
|
countryCode: geoData.countryCode,
|
|
measurementSystem
|
|
};
|
|
|
|
console.log('Location detected:', result);
|
|
|
|
return new Response(
|
|
JSON.stringify(result),
|
|
{
|
|
headers: {
|
|
...corsHeaders,
|
|
'Content-Type': 'application/json'
|
|
}
|
|
}
|
|
);
|
|
|
|
} catch (error) {
|
|
console.error('Error detecting location:', error);
|
|
|
|
// Return default (metric) with 500 status to indicate error occurred
|
|
// This allows proper error monitoring while still providing fallback data
|
|
const defaultResult: IPLocationResponse = {
|
|
country: 'Unknown',
|
|
countryCode: 'XX',
|
|
measurementSystem: 'metric'
|
|
};
|
|
|
|
return new Response(
|
|
JSON.stringify({
|
|
...defaultResult,
|
|
error: error instanceof Error ? error.message : 'Failed to detect location',
|
|
fallback: true
|
|
}),
|
|
{
|
|
headers: {
|
|
...corsHeaders,
|
|
'Content-Type': 'application/json'
|
|
},
|
|
status: 500
|
|
}
|
|
);
|
|
}
|
|
}); |