Handling exception thrown by handlers
See original GitHub issueI am evaluating the library for an internal use at my company.
One issue I have currently is that if any handler throws an exception, it will prevent other handlers from receiving the same event. While that is currently the same behavior of normal .NET events, it can cause issues.
Would you consider an overloaded method for WeakEventSource<TEventArgs>.Raise
that takes a delegate that can handle any exception and decide whether event notification should continue or not.
Something like:
public void Raise(object? sender, TEventArgs args, Func<Exception, bool> exceptionHandler)
{
var validHandlers = GetValidHandlers(_handlers);
foreach (var handler in validHandlers)
{
try
{
handler.Invoke(sender, args);
}
catch (Exception ex)
{
if (exceptionHandler(ex))
continue;
throw;
}
}
}
I was also thinking of a more advanced handler that returns a state so that bad subscriber can be automatically unsubscribed:
[Flags]
public enum ErrorHandlingFlags
{
None = 0,
NotThrow = 1,
Unsubscribe = 2,
}
public void Raise(object? sender, TEventArgs args, Func<Exception, ErrorHandlingFlags> exceptionHandler)
{
var validHandlers = GetValidHandlers(_handlers);
foreach (var handler in validHandlers)
{
try
{
handler.Invoke(sender, args);
}
catch (Exception ex)
{
var flag = exceptionHandler(ex);
if (flag.HasFlag(ErrorHandlingFlags.Unsubscribe))
{
// find a way to unsubscribe
}
if (flag.HasFlag(ErrorHandlingFlags.NotThrow))
continue;
throw;
}
}
}
But I’m not sure how the unsubscription can work without the original EventHandler<TEventArgs>
.
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (8 by maintainers)
Top Results From Across the Web
Complete Guide to Exception Handling in Spring Boot
This article showcases various ways to handle exceptions in a Spring Boot Application.
Read more >Exception Handling in Spring MVC
ExceptionHandlerExceptionResolver matches uncaught exceptions against suitable @ExceptionHandler methods on both the handler (controller) and on ...
Read more >What is Exception Handling? - SearchSoftwareQuality
A catch statement is a group of statements that handle the specific thrown exception. Catch parameters determine the specific type of exception that...
Read more >Error Handling for REST with Spring
The app can throw exceptions normally to indicate a failure of some kind, which will then be handled separately.
Read more >How to Specify and Handle Exceptions in Java
A method that throws a checked exception or that calls a method that specifies a checked exception needs to either specify or handle...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @Kryptos-FR,
I have most of the code sitting on my laptop, but I haven’t had time to come back to it since last week. I’ll try to finish it in the next few days.
Anyway, the good news is that I found a way to unsubscribe automatically (still need to test, though). It involves significant internal changes, but I think they’re good ones.