From 766e6834d3cf7713dde834bb28f028a01b685c05 Mon Sep 17 00:00:00 2001 From: Aayush Kumar Date: Tue, 14 Jan 2025 12:29:05 +0530 Subject: [PATCH] Added exception handling for UnicodeDecodeError. fixes issue #1526 Signed-off-by: Aayush Kumar --- scanpipe/pipes/js.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/scanpipe/pipes/js.py b/scanpipe/pipes/js.py index 90c5f4c37..dad622731 100644 --- a/scanpipe/pipes/js.py +++ b/scanpipe/pipes/js.py @@ -24,6 +24,7 @@ import json from contextlib import suppress from pathlib import Path +import logging from django.core.exceptions import MultipleObjectsReturned from django.core.exceptions import ObjectDoesNotExist @@ -62,6 +63,7 @@ }, } +logger = logging.getLogger(__name__) def is_source_mapping_in_minified(resource, map_file_name): """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): return [sha1(content) for content in contents if content] -def load_json_from_file(location): - """Return the deserialized json content from ``location``.""" - with open(location) as f: - try: +def load_json_from_file(file): + try: + with open(file, 'r') as f: return json.load(f) - except json.JSONDecodeError: - return + except UnicodeDecodeError as e: + logger.error(f"Failed to decode {file} as JSON: {str(e)}") + return + except json.JSONDecodeError as e: + logger.error(f"Invalid JSON format in {file}: {str(e)}") + return + except Exception as e: + logger.error(f"Unexpected error while reading {file}: {str(e)}") + return def get_map_sources(map_file):