1
0
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:
Darth Affe 2021-04-26 21:56:30 +02:00
parent b39188474f
commit 68c5990ccd
4 changed files with 44 additions and 9 deletions

View File

@ -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()
{

View File

@ -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

View File

@ -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

View 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
}
}