From a163c356b91129a2b9c6233be2f9364398220ee5 Mon Sep 17 00:00:00 2001 From: Sid Date: Thu, 8 Jan 2026 21:54:20 +0530 Subject: [PATCH] fix: handle non-JSON responses in carbon.js to prevent crash When the WebsiteCarbon API is protected by Cloudflare and requests originate from datacenter IPs, Cloudflare may return an HTML challenge page instead of JSON. This caused JSON.parse() to throw an uncaught SyntaxError exception, crashing the entire application. This fix: - Checks if the response starts with HTML markers before parsing - Wraps JSON.parse in try-catch for additional safety - Returns a descriptive error message instead of crashing Fixes #268 --- api/carbon.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/api/carbon.js b/api/carbon.js index 7db750c..22d6c51 100644 --- a/api/carbon.js +++ b/api/carbon.js @@ -29,7 +29,17 @@ const carbonHandler = async (url) => { data += chunk; }); res.on('end', () => { - resolve(JSON.parse(data)); + // Check if response looks like HTML (e.g., Cloudflare challenge page) + const trimmedData = data.trim(); + if (trimmedData.startsWith('