Skip to content

Conversation

@Harshit28j
Copy link

Summary

Fixes #3518

When parse_vulnerabilities_from_data() fails to parse a vulnerability record, the error message now includes the filename that caused the issue.

Changes

Added an optional source_name parameter to parse_vulnerabilities_from_data() in osv/sources.py. When parsing fails and source_name is provided, the exception is wrapped with a RuntimeError that includes the filename.

Files Modified

File Change
osv/sources.py Added source_name parameter to wrap parsing errors with file context
gcp/workers/importer/importer.py Pass source_name=blob.name at 3 call sites
gcp/workers/worker/worker.py Pass source_name=path at 1 call site

Before vs After

Before:

Expecting value: line 1 column 1 (char 0)
Traceback (most recent call last):
  File "/usr/local/bin/importer.py", line 291, in run
    self.process_updates(source_repo)
  File "/usr/local/bin/importer.py", line 986, in process_updates
    self._process_updates_rest(source_repo)
  File "/usr/local/bin/importer.py", line 907, in _process_updates_rest
    vulns = osv.parse_vulnerabilities_from_data(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/env/osv/sources.py", line 146, in parse_vulnerabilities_from_data
    data = json.loads(data_text)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/json/decoder.py", line 338, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/json/decoder.py", line 356, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

After:
image

@another-rex
Copy link
Contributor

/gcbrun

Copy link
Contributor

@another-rex another-rex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code that could fail should wrap exception with the appropriate context

2 participants