Skip to content

Commit aac6121

Browse files
authored
Merge pull request #271 from Alearner12/fix/carbon-json-parsing-error
2 parents 62b3c88 + a163c35 commit aac6121

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

‎api/carbon.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,17 @@ const carbonHandler = async (url) => {
2929
data += chunk;
3030
});
3131
res.on('end', () => {
32-
resolve(JSON.parse(data));
32+
// Check if response looks like HTML (e.g., Cloudflare challenge page)
33+
const trimmedData = data.trim();
34+
if (trimmedData.startsWith('<!DOCTYPE') || trimmedData.startsWith('<html') || trimmedData.startsWith('<')) {
35+
reject(new Error('WebsiteCarbon API returned HTML instead of JSON. This may be due to Cloudflare protection when running from a datacenter IP.'));
36+
return;
37+
}
38+
try {
39+
resolve(JSON.parse(data));
40+
} catch (parseError) {
41+
reject(new Error(`Failed to parse WebsiteCarbon API response as JSON: ${parseError.message}`));
42+
}
3343
});
3444
}).on('error', reject);
3545
});

0 commit comments

Comments
 (0)