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
[Part 1] Improved error handling with beautiful trace and exit code in cli, input_engine and cvedb #798
Conversation
Codecov Report
@@ Coverage Diff @@
## master #798 +/- ##
==========================================
+ Coverage 82.40% 87.47% +5.06%
==========================================
Files 147 148 +1
Lines 2523 2594 +71
Branches 297 305 +8
==========================================
+ Hits 2079 2269 +190
+ Misses 371 254 -117
+ Partials 73 71 -2
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
fix extractor problems.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have changed modernize exception handling of almost every module except OutputEngine since @SinghHrmn is working on it. I have talked with him and he will incorporate changes in his future PR.
cve_bin_tool/error_handler.py
Outdated
| import sys | ||
| from enum import Enum | ||
|
|
||
| from rich.console import Console | ||
| from rich.traceback import Traceback |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created new module for exception handling.
| class InsufficientArgs(Exception): | ||
| """ Insufficient command line arguments""" | ||
|
|
||
|
|
||
| class InvalidCsvError(Exception): | ||
| """ Given File is an Invalid CSV """ | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved all exception to one place in error_handler so that new contributor can easily see which exit code is used and always give unique exit code.
| def excepthook(exc_type, exc_val, exc_tb): | ||
| trace = Traceback.from_exception(exc_type, exc_val, exc_tb) | ||
| CONSOLE.print(trace) | ||
| if ERROR_CODES.get(exc_type): | ||
| sys.exit(ERROR_CODES[exc_type]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
custom excepthook with colorized traceback.
| class ErrorHandler: | ||
| """ | ||
| @summary: Error handler context manager. | ||
| usecases: | ||
| Different modes that can ignore error, print full trace, truncated trace and no trace. | ||
| Log messages if logger specified. | ||
| @param: mode (ErrorMode): Can take any valid ErrorMode as an arg and change output according to that. | ||
| @param: logger: logs error message specified while raising Exception if logger is passed | ||
| while class initialization. | ||
| Ex: raise ValueError("file required") will log 'file required'. | ||
| """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New ErrorHandler context manager. It supports 4 types of mode Ignore (ignore error), NoTrace (trace won't print), TruncTrace(truncated trace for the context only (default)), FullTrace (full trace default when debugging.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While using ErrorHandler context manager if error get throws it sets exit_code to error_code of that error. This will be helpful in Ignore mode to inspect if exception get throw.
cve_bin_tool/error_handler.py
Outdated
| ERROR_CODES = { | ||
| SystemExit: -2, | ||
| FileNotFoundError: -3, | ||
| InvalidCsvError: -4, | ||
| InvalidJsonError: -4, | ||
| MissingFieldsError: -5, | ||
| InsufficientArgs: -6, | ||
| EmptyCache: -7, | ||
| CVEDataForYearNotInCache: -8, | ||
| CVEDataForCurlVersionNotInCache: -9, | ||
| AttemptedToWriteOutsideCachedir: -10, | ||
| SHAMismatch: -11, | ||
| ExtractionFailed: -12, | ||
| UnknownArchiveType: 13, | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dictionary mapping of exception with the error code.
| if 0 < LOGGER.level <= 10: | ||
| error_mode = ErrorMode.FullTrace | ||
| elif LOGGER.level >= 50: | ||
| error_mode = ErrorMode.NoTrace | ||
| else: | ||
| error_mode = ErrorMode.TruncTrace | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Set error mode according to logging level. It will not print exception trace in quite mode (Currently we are printing that)
| raise AttemptedToWriteOutsideCachedir(filepath) | ||
| with ErrorHandler(mode=self.error_mode, logger=self.LOGGER): | ||
| raise AttemptedToWriteOutsideCachedir(filepath) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always use ErrorHandler context manager while raising exception.
| with self.assertRaises(SystemExit) as e: | ||
| main(["cve-bin-tool"]) | ||
| self.assertEqual(e.exception.args[0], -6) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modernize tests
Separate PR for extractor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See Terri's comment
Co-authored-by: Terri Oda <terri@toybox.ca>
9e931d1
to
b825724
Compare
Features added:
It will ignore exception and is for internal use only.
Fixes #787 and #788.