Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added exception handling for UnicodeDecodeError #1533

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions scanpipe/pipes/js.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand All @@ -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):
Expand Down