Skip to content

Commit 766e683

Browse files
authored
Added exception handling for UnicodeDecodeError. fixes issue aboutcode-org#1526
Signed-off-by: Aayush Kumar <aayush214.kumar@gmail.com>
1 parent ce4e46a commit 766e683

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

scanpipe/pipes/js.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import json
2525
from contextlib import suppress
2626
from pathlib import Path
27+
import logging
2728

2829
from django.core.exceptions import MultipleObjectsReturned
2930
from django.core.exceptions import ObjectDoesNotExist
@@ -62,6 +63,7 @@
6263
},
6364
}
6465

66+
logger = logging.getLogger(__name__)
6567

6668
def is_source_mapping_in_minified(resource, map_file_name):
6769
"""Return True if a string contains a source mapping in its last 5 lines."""
@@ -87,13 +89,19 @@ def source_content_sha1_list(map_file):
8789
return [sha1(content) for content in contents if content]
8890

8991

90-
def load_json_from_file(location):
91-
"""Return the deserialized json content from ``location``."""
92-
with open(location) as f:
93-
try:
92+
def load_json_from_file(file):
93+
try:
94+
with open(file, 'r') as f:
9495
return json.load(f)
95-
except json.JSONDecodeError:
96-
return
96+
except UnicodeDecodeError as e:
97+
logger.error(f"Failed to decode {file} as JSON: {str(e)}")
98+
return
99+
except json.JSONDecodeError as e:
100+
logger.error(f"Invalid JSON format in {file}: {str(e)}")
101+
return
102+
except Exception as e:
103+
logger.error(f"Unexpected error while reading {file}: {str(e)}")
104+
return
97105

98106

99107
def get_map_sources(map_file):

0 commit comments

Comments
 (0)