mirror of
https://codeberg.org/alicia/web-check.git
synced 2026-03-14 14:45:48 +01:00
Merge pull request #271 from Alearner12/fix/carbon-json-parsing-error
This commit is contained in:
commit
aac6121e81
1 changed files with 11 additions and 1 deletions
|
|
@ -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('<!DOCTYPE') || trimmedData.startsWith('<html') || trimmedData.startsWith('<')) {
|
||||
reject(new Error('WebsiteCarbon API returned HTML instead of JSON. This may be due to Cloudflare protection when running from a datacenter IP.'));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
resolve(JSON.parse(data));
|
||||
} catch (parseError) {
|
||||
reject(new Error(`Failed to parse WebsiteCarbon API response as JSON: ${parseError.message}`));
|
||||
}
|
||||
});
|
||||
}).on('error', reject);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue