try:
some_function()
except RuntimeError as e:
logger.error(e)
it can be difficult to troubleshoot the error when the error message e isn't helpful on its own.
(It's best to avoid catching the generic RuntimeError (or Exception) whenever possible, and catch a more specific type of error that you know the code might throw.)
If you don't want the code to re-raise the exception after printing out the error message, you can make use of the function traceback.print_exec() to print the full error trace when that would be helpful. The new utils.capture_error_trace() function is helpful in that it capture the output of traceback.print_exec() in a variable, which we can then hand off to logger.error().
When an exception is handled like this:
it can be difficult to troubleshoot the error when the error message
e
isn't helpful on its own. (It's best to avoid catching the genericRuntimeError
(orException
) whenever possible, and catch a more specific type of error that you know the code might throw.)If you don't want the code to re-raise the exception after printing out the error message, you can make use of the function
traceback.print_exec()
to print the full error trace when that would be helpful. The newutils.capture_error_trace()
function is helpful in that it capture the output oftraceback.print_exec()
in a variable, which we can then hand off tologger.error()
.