Problem: When try except catching errors and logging them I occasionally get this TypeError
...
try:
withconnection.cursor() ascursor:
cursor.execute(query)
...
exceptdatabricks.sql.Errorase:
logger.debug(f"{str(e)}: failed to execute query {query}")
...__str__ returned non-string (type NoneType)
Explanation:
String representations of a class must be of type string
the message attribute can be None therefore there needs to be a check/cast prior to returning.
Note: message_with_context will also fail when a message is None since you can't use the operand type + for NoneType and a str
classError(Exception):
"""Base class for DB-API2.0 exceptions. `message`: An optional user-friendly error message. It should be short, actionable and stable `context`: Optional extra context about the error. MUST be JSON serializable """def__init__(self, message=None, context=None, *args, **kwargs):
super().__init__(message, *args, **kwargs)
self.message=messageself.context=contextor {}
def__str__(self):
returnself.messagedefmessage_with_context(self):
returnself.message+": "+json.dumps(self.context, default=str)
Problem: When try except catching errors and logging them I occasionally get this TypeError
__str__ returned non-string (type NoneType)Explanation:
String representations of a class must be of type string
the message attribute can be None therefore there needs to be a check/cast prior to returning.
Note:
message_with_contextwill also fail when a message is None since you can't use the operand type+forNoneTypeand astr