Vamsi-klu commented on code in PR #62656:
URL: https://github.com/apache/airflow/pull/62656#discussion_r2934711344
##########
shared/logging/src/airflow_shared/logging/structlog.py:
##########
@@ -316,7 +316,17 @@ def json_dumps(msg, default):
"event": msg.pop("event"),
**msg,
}
- return msgspec.json.encode(msg, enc_hook=default)
+
+ def safe_default(obj):
+ """Fall back to str() if the default enc_hook fails or is
None."""
+ if default is not None:
Review Comment:
Addressed in 1bd14c45f2. Added three tests:
1. **`test_json_non_serializable_object`** — an object whose
`__structlog__()` raises `TypeError` now falls back to `str()` instead of
crashing. This is the actual crash path: `_json_fallback_handler` only catches
`AttributeError`, so `TypeError` from `__structlog__()` propagates through
msgspec and crashes the logger.
2. **`test_json_custom_object_uses_repr`** — regression test confirming
normal custom objects (without `__structlog__`) still serialize via `repr()`
through the standard `_json_fallback_handler` path.
3. **`test_safe_enc_hook_with_none_default`** — unit test for the
`default=None` edge case, testing `_make_safe_enc_hook` directly.
##########
shared/logging/src/airflow_shared/logging/structlog.py:
##########
@@ -316,7 +316,17 @@ def json_dumps(msg, default):
"event": msg.pop("event"),
**msg,
}
- return msgspec.json.encode(msg, enc_hook=default)
+
+ def safe_default(obj):
+ """Fall back to str() if the default enc_hook fails or is
None."""
+ if default is not None:
Review Comment:
Addressed in 1bd14c45f2. Hoisted the closure into a module-level factory
function `_make_safe_enc_hook(default)` — follows the same pattern as the
existing `_make_airflow_structlogger` in the same file.
The `json_dumps` body is now a clean one-liner:
```python
return msgspec.json.encode(msg, enc_hook=_make_safe_enc_hook(default))
```
The factory is at module level (not inside the `@cache`d
`structlog_processors`) so it's also directly importable for unit testing.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]