Skip to content

Preserve exception stack traces during reflection-based invocation #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2025

Conversation

letnaturebe2
Copy link
Contributor

Summary

This PR enhances the robustness of the EventSubscriber.Invoke<T> method by preserving the original exception context when invoking methods via reflection.

Problem

When using MethodInfo.Invoke(...), if an exception occurs, it is wrapped inside a TargetInvocationException, which causes the original stack trace to be lost. This makes debugging much harder, especially when exceptions are thrown deep inside event handlers or predicates.

Changes

  • Wrapped _predicateMethod.Invoke(...) and _callbackMethod.Invoke(...) inside try-catch blocks.
  • Used ExceptionDispatchInfo.Capture(...).Throw() to re-throw the inner exception while preserving its original stack trace.

Impact

  • Maintains full stack trace visibility during runtime errors.

@letnaturebe2
Copy link
Contributor Author

@DevsDaddy

can you review this pr?

@DevsDaddy DevsDaddy merged commit 1ee973b into DevsDaddy:main Aug 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants