From 4cbc593ee47b8d936ce54a6d3018ae443a77aa7f Mon Sep 17 00:00:00 2001 From: LeeGordon83 Date: Mon, 19 Jan 2026 12:11:10 +0000 Subject: [PATCH] Improve error logging for proxy debugging - Remove timeout from health check to see real error - Add detailed error logging with cause and code - Log full error details in structured format - Add response text to non-200 responses --- src/lib/flood-service.js | 31 ++++++++++++++++++++++--------- src/routes/health-check.js | 7 ++----- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/lib/flood-service.js b/src/lib/flood-service.js index 98f1001..96c535c 100644 --- a/src/lib/flood-service.js +++ b/src/lib/flood-service.js @@ -11,17 +11,24 @@ export function proxyFetch (url, options = {}) { const proxyUrlConfig = config.get('httpProxy') // bound to HTTP_PROXY if (!proxyUrlConfig) { + console.log(`[PROXY] No HTTP_PROXY set - using direct fetch for: ${url}`) return fetch(url, options) } - return fetch(url, { - ...options, - dispatcher: new ProxyAgent({ - uri: proxyUrlConfig, - keepAliveTimeout: 10, - keepAliveMaxTimeout: 10 + console.log(`[PROXY] Using proxy ${proxyUrlConfig} for: ${url}`) + try { + return fetch(url, { + ...options, + dispatcher: new ProxyAgent({ + uri: proxyUrlConfig, + keepAliveTimeout: 10, + keepAliveMaxTimeout: 10 + }) }) - }) + } catch (error) { + console.error(`[PROXY] Error setting up proxy for ${url}:`, error) + throw error + } } /** @@ -35,7 +42,8 @@ export async function getStation (stationId) { console.log(`Station API response status: ${response.status} ${response.statusText}`) if (!response.ok) { - throw new Error(`Failed to fetch station: ${response.status} ${response.statusText}`) + const errorText = await response.text().catch(() => 'Unable to read error response') + throw new Error(`Failed to fetch station: ${response.status} ${response.statusText} - ${errorText}`) } const data = await response.json() // The API returns an array of stations in items @@ -46,7 +54,12 @@ export async function getStation (stationId) { console.log(`No station items found for ${stationId}`) return null } catch (error) { - console.error(`Error fetching station from ${url}:`, error.message, error.cause) + console.error(`Error fetching station from ${url}:`, { + message: error.message, + cause: error.cause?.message || error.cause, + code: error.cause?.code, + stack: error.stack?.split('\n').slice(0, 5).join('\n') + }) return null } } diff --git a/src/routes/health-check.js b/src/routes/health-check.js index b68a573..efa3216 100644 --- a/src/routes/health-check.js +++ b/src/routes/health-check.js @@ -18,11 +18,8 @@ export const healthCheck = { const testUrl = 'https://environment.data.gov.uk/flood-monitoring/id/stations?RLOIid=8085' request.logger.info(`Testing connectivity to: ${testUrl}`) - const controller = new AbortController() - const timeoutId = setTimeout(() => controller.abort(), 5000) - - const response = await proxyFetch(testUrl, { signal: controller.signal }) - clearTimeout(timeoutId) + // No timeout - let it fail naturally to see the real error + const response = await proxyFetch(testUrl) results.externalApis.environmentAgency = { url: testUrl,