File tree Expand file tree Collapse file tree 1 file changed +11
-1
lines changed
Expand file tree Collapse file tree 1 file changed +11
-1
lines changed Original file line number Diff line number Diff 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 } ) ;
You can’t perform that action at this time.
0 commit comments