From cdf7d2a0e268c1b5e1082a1fe062c2c6757121cb Mon Sep 17 00:00:00 2001 From: ndrwrbgs Date: Fri, 28 Feb 2020 12:15:57 -0800 Subject: [PATCH] Fixes #303, add Rethrow() method --- src/CacheManager.Core/BaseCacheManager.cs | 3 ++- src/CacheManager.Core/Utility/Throw.cs | 24 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/CacheManager.Core/Utility/Throw.cs diff --git a/src/CacheManager.Core/BaseCacheManager.cs b/src/CacheManager.Core/BaseCacheManager.cs index 353f11bc..ff48a6ff 100644 --- a/src/CacheManager.Core/BaseCacheManager.cs +++ b/src/CacheManager.Core/BaseCacheManager.cs @@ -5,6 +5,7 @@ using System.Linq; using CacheManager.Core.Internal; using CacheManager.Core.Logging; +using CacheManager.Core.Utility; using static CacheManager.Core.Utility.Guard; namespace CacheManager.Core @@ -127,7 +128,7 @@ private BaseCacheManager(string name, ICacheManagerConfiguration configuration) catch (Exception ex) { Logger.LogError(ex, "Error occurred while creating the cache manager."); - throw ex.InnerException ?? ex; + Throw.Rethrow(ex.InnerException ?? ex); } } diff --git a/src/CacheManager.Core/Utility/Throw.cs b/src/CacheManager.Core/Utility/Throw.cs new file mode 100644 index 00000000..b6106ed3 --- /dev/null +++ b/src/CacheManager.Core/Utility/Throw.cs @@ -0,0 +1,24 @@ +using System; +using System.Runtime.ExceptionServices; + +namespace CacheManager.Core.Utility +{ + /// + /// related helper. + /// + internal static class Throw + { + /// + /// Rethrows the supplied exception without losing its stack trace. + /// Prefer throw; where possible, this method is useful for rethrowing + /// + /// which cannot be done with the throw; semantics. + /// + /// The exception. + public static void Rethrow( + this Exception exception) + { + ExceptionDispatchInfo.Capture(exception).Throw(); + } + } +}