fix: log OSRM failure reason and URL instead of silent fallback

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
chris 2026-05-05 15:15:40 -04:00
parent 175305a28f
commit 5bebd51ac4

View File

@ -57,6 +57,7 @@ export async function drivingInfo(
// Self-hosted gets a generous timeout; public demo gets a short one
const timeoutMs = process.env.OSRM_URL ? 10_000 : 6_000
let lastErr = 'unknown'
for (let attempt = 0; attempt < 3; attempt++) {
try {
const controller = new AbortController()
@ -67,19 +68,20 @@ export async function drivingInfo(
signal: controller.signal,
})
clearTimeout(timer)
if (!res.ok) throw new Error(`OSRM ${res.status}`)
if (!res.ok) throw new Error(`HTTP ${res.status}`)
const data = await res.json()
if (data.code !== 'Ok' || !data.routes?.length) throw new Error('No route found')
if (data.code !== 'Ok' || !data.routes?.length) throw new Error(`bad response: code=${data.code}`)
const miles = (data.routes[0].distance as number) / 1609.344
const minutes = Math.ceil((data.routes[0].duration as number) / 60)
return { miles, minutes }
} catch {
} catch (err) {
lastErr = err instanceof Error ? err.message : String(err)
if (attempt < 2) await new Promise((r) => setTimeout(r, 500 * (attempt + 1)))
}
}
// Fallback: straight-line distance × 1.3 road overhead, ~25 mph average
console.warn('[delivery] OSRM unavailable — using haversine fallback')
console.warn(`[delivery] OSRM unavailable (${url}) — last error: ${lastErr} — using haversine fallback`)
const miles = haversineMiles(lat1, lng1, lat2, lng2) * 1.3
const minutes = Math.ceil((miles / 25) * 60)
return { miles, minutes }