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

Please re-raise Exceptions or raise a own exception during parsing #59

Closed
jloehel opened this issue Nov 27, 2024 · 2 comments
Closed

Please re-raise Exceptions or raise a own exception during parsing #59

jloehel opened this issue Nov 27, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@jloehel
Copy link

jloehel commented Nov 27, 2024

It's quiet confusing if parse_file do not complain if there is a ValueError or a KeyError. I just a get an empty doc. It would be nice to catch the full trace with an exception to actually see what is failing. If you still want the debugging message you can catch them but please re-raise the exception or raise a own exception like ParsingError and describe there in detail what failed.
... or at least returning the errors.

lib4sbom/lib4sbom/parser.py

Lines 120 to 125 in 7901844

except KeyError:
if self.debug:
print("Key Error")
except TypeError:
if self.debug:
print("Type Error")

Like that, I had to change the code (just adding ... as err:) and set there a breakpoint to see what was going on. I would also suggest to use a logger or print on stderr instead of stdout.

@anthonyharrison anthonyharrison added the enhancement New feature or request label Dec 1, 2024
@anthonyharrison
Copy link
Owner

Thanks for the suggestion @jloehel I have tried to be as flexible and tolerant as possible with the parsing but you are right, a lib4sbom defined exception would be a better solution.

@jloehel
Copy link
Author

jloehel commented Dec 9, 2024

@anthonyharrison Maybe one suggestion more. I would raise it like raise SBOMParserException(f"Unable to process file {sbom_file}") from e , to understand the root cause of the failure.

How I would use your lib:

from lib4sbom.cyclonedx.cyclonedx_parser import CycloneDXParser
from lib4sbom.exception import SBOMParserException

from loguru import logger

parser = CycloneDXParser()
try:
    parser.parse("empty.json")
except SBOMParserException as err:
    logger.opt(exception=err).debug("Traceback for SBOM parsing error")
    logger.error(f"{err}: {err.__cause__}")

current output:

2024-12-09 14:49:33.261 | ERROR    | __main__:<module>:10 - : None

maybe better:

2024-12-09 14:50:16.092 | ERROR    | __main__:<module>:10 - Unable to process file empty.json: Expecting value: line 1 column 1 (char 0)

Of course with implicit chaining the message would be also available in context. But it's more clear in my opinion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants