mirror of
https://github.com/DarthAffe/RGB.NET.git
synced 2025-12-12 17:48:31 +00:00
Imporoved exception-handling in device providers
This commit is contained in:
parent
b39188474f
commit
68c5990ccd
@ -22,7 +22,7 @@ namespace RGB.NET.Core
|
||||
|
||||
#region Events
|
||||
|
||||
public event EventHandler<Exception>? Exception;
|
||||
public event EventHandler<ExceptionEventArgs>? Exception;
|
||||
|
||||
#endregion
|
||||
|
||||
@ -54,10 +54,15 @@ namespace RGB.NET.Core
|
||||
|
||||
IsInitialized = true;
|
||||
}
|
||||
catch (DeviceProviderException)
|
||||
{
|
||||
Reset();
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Reset();
|
||||
Throw(ex);
|
||||
Throw(ex, true);
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -98,15 +103,16 @@ namespace RGB.NET.Core
|
||||
IsInitialized = false;
|
||||
}
|
||||
|
||||
protected virtual void Throw(Exception ex)
|
||||
protected virtual void Throw(Exception ex, bool isCritical = false)
|
||||
{
|
||||
try { OnException(ex); } catch { /* we don't want to throw due to bad event handlers */ }
|
||||
ExceptionEventArgs args = new(ex, isCritical, ThrowsExceptions);
|
||||
try { OnException(args); } catch { /* we don't want to throw due to bad event handlers */ }
|
||||
|
||||
if (ThrowsExceptions)
|
||||
throw ex;
|
||||
if (args.Throw)
|
||||
throw new DeviceProviderException(ex, isCritical);
|
||||
}
|
||||
|
||||
protected virtual void OnException(Exception ex) => Exception?.Invoke(this, ex);
|
||||
protected virtual void OnException(ExceptionEventArgs args) => Exception?.Invoke(this, args);
|
||||
|
||||
public virtual void Dispose()
|
||||
{
|
||||
|
||||
@ -27,7 +27,7 @@ namespace RGB.NET.Core
|
||||
/// <summary>
|
||||
/// Occurs when an exception is thrown in the device provider
|
||||
/// </summary>
|
||||
event EventHandler<Exception>? Exception;
|
||||
event EventHandler<ExceptionEventArgs>? Exception;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
@ -18,6 +18,10 @@ namespace RGB.NET.Core
|
||||
/// </summary>
|
||||
public Exception Exception { get; }
|
||||
|
||||
public bool IsCritical { get; }
|
||||
|
||||
public bool Throw { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
@ -27,9 +31,11 @@ namespace RGB.NET.Core
|
||||
/// Initializes a new instance of the <see cref="T:RGB.NET.Core.ExceptionEventArgs" /> class.
|
||||
/// </summary>
|
||||
/// <param name="exception">The <see cref="T:System.Exception" /> which is responsible for the event-call.</param>
|
||||
public ExceptionEventArgs(Exception exception)
|
||||
public ExceptionEventArgs(Exception exception, bool isCritical = false, bool @throw = false)
|
||||
{
|
||||
this.Exception = exception;
|
||||
this.IsCritical = isCritical;
|
||||
this.Throw = @throw;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
23
RGB.NET.Core/Exceptions/DeviceProviderException.cs
Normal file
23
RGB.NET.Core/Exceptions/DeviceProviderException.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
|
||||
namespace RGB.NET.Core
|
||||
{
|
||||
public class DeviceProviderException : ApplicationException
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private bool IsCritical { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public DeviceProviderException(Exception? innerException, bool isCritical)
|
||||
: base(innerException?.Message, innerException)
|
||||
{
|
||||
this.IsCritical = isCritical;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user