@@ -91,21 +91,19 @@ def scrub_sensitive_data(record):
9191 scrubbed_value_str = _SCRUBBER .scrub (value_str )
9292
9393 if scrubbed_value_str != value_str :
94- # Re-instantiate the exception with the redacted message to preserve loguru formatting
94+ # Modify args in-place to preserve exception type and metadata
95+ # Most exceptions store their message in args[0]
9596 try :
96- # Most standard exceptions accept a single string argument
97- new_value = type_ (scrubbed_value_str )
98- except TypeError :
99- # Fallback to a generic Exception if type instantiation fails
100- # (e.g., some exceptions require specific constructor arguments)
101- new_value = Exception (f"[{ type_ .__name__ } ] { scrubbed_value_str } " )
102-
103- # Preserve traceback and context metadata
104- new_value .__traceback__ = tb
105- new_value .__cause__ = getattr (value , "__cause__" , None )
106- new_value .__context__ = getattr (value , "__context__" , None )
107-
108- record ["exception" ] = (type_ , new_value , tb )
97+ if value .args :
98+ scrubbed_args = tuple (
99+ _SCRUBBER .scrub (str (arg )) if isinstance (arg , str ) else arg
100+ for arg in value .args
101+ )
102+ value .args = scrubbed_args
103+ except (AttributeError , TypeError ):
104+ # If args modification fails, leave original exception unchanged
105+ # This is safer than creating a mismatched exception type
106+ pass
109107
110108 # Scrub extra context if present
111109 extra = record .get ("extra" )
0 commit comments