Catch `LinkageError` in `ThrowableExtendedStackTraceRenderer#loadClass` (#4028)
Summary
The loadClass method in ThrowableExtendedStackTraceRenderer only catches Exception,
but ClassLoader can throw NoClassDefFoundError, which extends LinkageError (an Error
subclass). This causes the entire log event to fail with AppenderLoggingException instead
of gracefully degrading when a class in the stack trace cannot be resolved.
Changes
ThrowableExtendedStackTraceRenderer.java: Changedcatch (Exception)tocatch (Exception | LinkageError)in theloadClassmethodThrowableExtendedStackTraceRendererTest.java: Added two test cases:- Verifies rendering succeeds when a stack trace references a non-existent class
- Verifies rendering succeeds when a custom
ClassLoaderthrowsNoClassDefFoundError
- Changelog entry: Added
4028_fix_catch_LinkageError_in_ThrowableExtendedStackTraceRenderer.xml
Root Cause
// Before: Only catches Exception — NoClassDefFoundError (Error) escapes
catch (final Exception ignored)
// After: Also catches LinkageError, which is the parent of NoClassDefFoundError
catch (final Exception | LinkageError ignored)
Java's Throwable hierarchy:
Throwable
├── Exception ← previously caught
└── Error
└── LinkageError ← now also caught
└── NoClassDefFoundError
Fixes #4028
Hi @vy, thanks for the encouragement!
Here's what this PR includes:
- Fix: Changed
catch (Exception)tocatch (Exception | LinkageError)inThrowableExtendedStackTraceRenderer#loadClass - Tests: Two test cases covering
NoClassDefFoundErrorscenarios - Changelog: Added entry for this fix
Let me know if any changes are needed!
Hi @vy, thank you for the review! I've addressed all the feedback.
catch (Throwable)— Applied the suggested change to catch allThrowables instead ofException | LinkageError- Changelog description — Updated to "Ensure all
Throwables are handled while rendering stack traces" - Changelog issue link — Changed from
#4028to#4049 - Tests moved — Removed
ThrowableExtendedStackTraceRendererTestand added the test cases intoThrowablePatternConverterTest.StackTraceTestas requested
Please let me know if any further changes are needed!
@chanani, TestFriendlyException is already containing a stack trace element of a non-existent class: ORG_APACHE_REPLACEMENT_STACK_TRACE_ELEMENT. Any ideas on why does this NoClassDefFoundError not surface with the existing test suite?
Hi @vy,
The reason NoClassDefFoundError doesn't surface with the existing test suite is the difference between two exception paths:
-
ClassNotFoundException(extendsException) — Thrown byClassLoader.loadClass()when a class simply doesn't exist on the classpath. That's the case forbar.OrgApacheReplacement— it was never on the classpath, so the class loader search fails cleanly withClassNotFoundException, which was already caught by the previouscatch (Exception) -
NoClassDefFoundError(extendsLinkageError→Error) — Thrown when the class is found but fails during linking/initialization (e.g., a missing dependency at link time). This is the scenario from issue #4028, and it wasn't caught bycatch (Exception)
So the existing test with ORG_APACHE_REPLACEMENT_STACK_TRACE_ELEMENT exercises the ClassNotFoundException path, not the NoClassDefFoundError path
Please let me know if any further changes are needed !