Skip to content

Commit

Permalink
feature: add throw util
Browse files Browse the repository at this point in the history
  • Loading branch information
JusterZhu committed Jan 2, 2024
1 parent 4e2e60c commit 04ae971
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
48 changes: 48 additions & 0 deletions src/c#/GeneralUpdate.Core/Exceptions/ThrowExceptionUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using GeneralUpdate.Core.Exceptions.CustomArgs;
using System;

namespace GeneralUpdate.Core.Exceptions
{
internal sealed class ThrowExceptionUtility
{
public static void ThrowGeneralUpdateException(ExceptionArgs args)
=> Throw<Exception>(args.ToString(), args);

#region Common

/// <summary>
/// Checks if an object is empty and throws an exception if it is
/// </summary>
/// <param name="obj"></param>
/// <param name="paramName"></param>
/// <exception cref="ArgumentNullException"></exception>
public static void ThrowIfNull(object obj, string paramName)
{
if (obj == null)
Throw<ArgumentNullException>(paramName);
}

/// <summary>
/// Checks if the string is empty or blank, and throws an exception if it is.
/// </summary>
/// <param name="str"></param>
/// <param name="paramName"></param>
/// <exception cref="ArgumentException"></exception>
public static void ThrowIfNullOrWhiteSpace(string str, string paramName)
{
if (string.IsNullOrWhiteSpace(str))
Throw<ArgumentException>("Parameter cannot be null or whitespace", paramName);
}

/// <summary>
/// Basic method of exception declaration.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="message"></param>
/// <param name="args"></param>
public static void Throw<T>(string message, params object[] args) where T : Exception, new()
=> throw (T)Activator.CreateInstance(typeof(T), message, args);

#endregion
}
}
10 changes: 5 additions & 5 deletions src/c#/GeneralUpdate.Upgrad/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ private static void Main(string[] args)
.AddListenerMultiDownloadError(OnMultiDownloadError)
//整个更新过程出现的任何问题都会通过这个事件通知
.AddListenerException(OnException)
.Strategy<WindowsStrategy>().
Option(UpdateOption.Encoding, Encoding.Default).
Option(UpdateOption.DownloadTimeOut, 60).
Option(UpdateOption.Format, Format.ZIP).
LaunchTaskAsync();
.Strategy<WindowsStrategy>()
.Option(UpdateOption.Encoding, Encoding.Default)
.Option(UpdateOption.DownloadTimeOut, 60)
.Option(UpdateOption.Format, Format.ZIP)
.LaunchTaskAsync();
});
Console.Read();
}
Expand Down

0 comments on commit 04ae971

Please sign in to comment.