Package com.tnsai.exceptions
Class ContextualWrapper
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
com.tnsai.exceptions.ContextualWrapper
- All Implemented Interfaces:
Serializable
Thin
RuntimeException that wraps a non-TnsAIException
throwable so its EventContext is preserved at the catch site.
Third-party libraries (OkHttp, Jackson, Jedis, Playwright, …) throw
their own exception types that don't extend TnsAIException and
therefore can't auto-capture AgentContext at construction. When
the framework catches one at a boundary (action executor, LLM client,
MCP client) and wants to surface it to operators with the same
correlation fields a TnsAIException would carry, it goes
through ThrowableContextDecorator.enrich(Throwable) which
produces a ContextualWrapper.
The wrapper:
- Captures the current thread's
AgentContextat the wrap-time boundary. - Delegates
Throwable.getCause()to the original throwable so the full root-cause stack is preserved. - Includes the captured context in
getMessage()the same wayTnsAIExceptiondoes so log scrapers see it inline. - Does not extend
TnsAIExceptionon purpose — consumers catchingTnsAIExceptionshould not start matching wrapped third-party throwables they previously did not match. Pattern- matching for wrapped errors is explicit:if (t instanceof ContextualWrapper w) { ... }.
- Since:
- 0.5.0 (issue #90)
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionContextualWrapper(Throwable original) Wraporiginalwith the current thread'sAgentContext(which may be empty).ContextualWrapper(Throwable original, EventContext explicitContext) Wraporiginalwith an explicit context (test / cross-thread scenarios where the natural thread-local lookup is wrong). -
Method Summary
Modifier and TypeMethodDescriptionMethods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
-
Constructor Details
-
ContextualWrapper
Wraporiginalwith the current thread'sAgentContext(which may be empty). -
ContextualWrapper
Wraporiginalwith an explicit context (test / cross-thread scenarios where the natural thread-local lookup is wrong).
-
-
Method Details
-
getContext
-
getCapturedAt
-
getMessage
- Overrides:
getMessagein classThrowable
-