1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 10:08: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 #region Events
public event EventHandler<Exception>? Exception; public event EventHandler<ExceptionEventArgs>? Exception;
#endregion #endregion
@ -54,10 +54,15 @@ namespace RGB.NET.Core
IsInitialized = true; IsInitialized = true;
} }
catch (DeviceProviderException)
{
Reset();
throw;
}
catch (Exception ex) catch (Exception ex)
{ {
Reset(); Reset();
Throw(ex); Throw(ex, true);
return false; return false;
} }
@ -98,15 +103,16 @@ namespace RGB.NET.Core
IsInitialized = false; 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) if (args.Throw)
throw ex; 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() public virtual void Dispose()
{ {

View File

@ -27,7 +27,7 @@ namespace RGB.NET.Core
/// <summary> /// <summary>
/// Occurs when an exception is thrown in the device provider /// Occurs when an exception is thrown in the device provider
/// </summary> /// </summary>
event EventHandler<Exception>? Exception; event EventHandler<ExceptionEventArgs>? Exception;
#endregion #endregion

View File

@ -18,6 +18,10 @@ namespace RGB.NET.Core
/// </summary> /// </summary>
public Exception Exception { get; } public Exception Exception { get; }
public bool IsCritical { get; }
public bool Throw { get; set; }
#endregion #endregion
#region Constructors #region Constructors
@ -27,9 +31,11 @@ namespace RGB.NET.Core
/// Initializes a new instance of the <see cref="T:RGB.NET.Core.ExceptionEventArgs" /> class. /// Initializes a new instance of the <see cref="T:RGB.NET.Core.ExceptionEventArgs" /> class.
/// </summary> /// </summary>
/// <param name="exception">The <see cref="T:System.Exception" /> which is responsible for the event-call.</param> /// <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.Exception = exception;
this.IsCritical = isCritical;
this.Throw = @throw;
} }
#endregion #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
}
}