I have a simple try and except statement. However I want to use logger exception to log the exception. What is the best way of only have 1 line of code for the logger.exception. In the exception base class ?
try:
do_something()
except CustomBaseExecption, exc:
logger.exception("Exception Raised:")
raise GeneralError(exc)
except Exception as exc:
logger.exception("Exception Raised:")
raise GeneralError("Unknown Error")
Łukasz Rogalski
23.3k10 gold badges63 silver badges93 bronze badges
asked Jan 15, 2016 at 13:20
felix001
16.9k38 gold badges104 silver badges138 bronze badges
1 Answer 1
Only thing that's changed between two code blocks is GeneralError argument. Let's put a conditional there.
try:
do_something()
except Exception as exc:
logger.exception("Exception Raised:")
raise GeneralError(exc if isinstance(exc, CustomBaseExecption) else "Unknown Error")
answered Jan 15, 2016 at 13:23
Łukasz Rogalski
23.3k10 gold badges63 silver badges93 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py