public void Extensions_Safe_Catch2() { var ex0 = new Ex("ex"); var ex1 = new Ex1("ex"); var ex2 = new Ex2("ex"); var ex3 = new Ex3("ex"); var baseEx = new BaseEx("ex"); var subEx = new SubEx("ex"); var err1 = Safe.Try <bool, Ex1, Ex2>(() => { throw ex1; }); var err2 = Safe.Try <bool, Ex1, Ex2>(() => { throw ex2; }); Assert.IsFalse(err1.HasValue); Assert.IsFalse(err2.HasValue); Assert.IsInstanceOfType(err1.Match(x => null, ex => ex), typeof(Ex1)); Assert.IsInstanceOfType(err2.Match(x => null, ex => ex), typeof(Ex2)); Assert.AreEqual(err1.Match(x => null, ex => ex), ex1); Assert.AreEqual(err2.Match(x => null, ex => ex), ex2); CustomAssert.Throws <Ex>(() => Safe.Try <bool, Ex2, Ex3>(() => { throw ex0; })); CustomAssert.Throws <Ex1>(() => Safe.Try <bool, Ex2, Ex3>(() => { throw ex1; })); Safe.Try <bool, BaseEx, Ex2>(() => { throw subEx; }); Safe.Try <bool, Ex, Ex2>(() => { throw ex0; }); Safe.Try <bool, Ex, Ex2>(() => { throw ex1; }); var success = Safe.Try <bool, Ex1, Ex2>(() => true); Assert.IsTrue(success.ValueOr(false)); }
public void PassingNullSecondStringComputeTest() { System.ArgumentNullException ex = Assert.Throws <System.ArgumentNullException>(() => Levenshtein.Compute("sddss", null)); Assert.Contains("Value cannot be null.", ex.Message); }
/// <summary> /// Logs and rethrow the exception. /// </summary> /// <param name="message">Error message.</param> /// <param name="inputName">Name of input type.</param> /// <param name="traceProps">custom properties, add more dimensions to this, so it will be easy to trace and query.</param> protected void LogAndThrowArgumentNullException(string message, string inputName, Dictionary <string, string> traceProps) { var argumentException = new System.ArgumentNullException(inputName, message); this.logger.TraceInformation(argumentException.Message, traceProps); throw argumentException; }
public void Extensions_Safe_Catch5() { var ex0 = new Ex("ex"); var ex1 = new Ex1("ex"); var ex2 = new Ex2("ex"); var ex3 = new Ex3("ex"); var ex4 = new Ex4("ex"); var ex5 = new Ex5("ex"); var ex6 = new Ex6("ex"); var baseEx = new BaseEx("ex"); var subEx = new SubEx("ex"); var err1 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex1; }); var err2 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex2; }); var err3 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex3; }); var err4 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex4; }); var err5 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex5; }); Assert.IsFalse(err1.HasValue); Assert.IsFalse(err2.HasValue); Assert.IsFalse(err3.HasValue); Assert.IsFalse(err4.HasValue); Assert.IsFalse(err5.HasValue); Assert.IsInstanceOf(typeof(Ex1), err1.Match(x => null, ex => ex)); Assert.IsInstanceOf(typeof(Ex2), err2.Match(x => null, ex => ex)); Assert.IsInstanceOf(typeof(Ex3), err3.Match(x => null, ex => ex)); Assert.IsInstanceOf(typeof(Ex4), err4.Match(x => null, ex => ex)); Assert.IsInstanceOf(typeof(Ex5), err5.Match(x => null, ex => ex)); Assert.AreEqual(err1.Match(x => null, ex => ex), ex1); Assert.AreEqual(err2.Match(x => null, ex => ex), ex2); Assert.AreEqual(err3.Match(x => null, ex => ex), ex3); Assert.AreEqual(err4.Match(x => null, ex => ex), ex4); Assert.AreEqual(err5.Match(x => null, ex => ex), ex5); CustomAssert.Throws <Ex>(() => Safe.Try <bool, Ex2, Ex3, Ex4, Ex5, Ex6>(() => { throw ex0; })); CustomAssert.Throws <Ex1>(() => Safe.Try <bool, Ex2, Ex3, Ex4, Ex5, Ex6>(() => { throw ex1; })); Safe.Try <bool, BaseEx, Ex2, Ex3, Ex4, Ex5>(() => { throw subEx; }); Safe.Try <bool, Ex, Ex2, Ex3, Ex4, Ex5>(() => { throw ex0; }); Safe.Try <bool, Ex, Ex2, Ex3, Ex4, Ex5>(() => { throw ex1; }); var success = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => true); Assert.IsTrue(success.ValueOr(false)); }
public void Extensions_Safe_CatchAll() { var ex1 = new Ex("ex"); var ex2 = new Ex1("ex"); var error1 = Safe.Try <bool>(() => { throw ex1; }); var error2 = Safe.Try <bool>(() => { throw ex2; }); Assert.IsFalse(error1.HasValue); Assert.IsFalse(error2.HasValue); Assert.IsInstanceOfType(error1.Match(x => null, ex => ex), typeof(Ex)); Assert.IsInstanceOfType(error2.Match(x => null, ex => ex), typeof(Ex1)); Assert.AreEqual(error1.Match(x => null, ex => ex), ex1); Assert.AreEqual(error2.Match(x => null, ex => ex), ex2); var success = Safe.Try(() => true); Assert.IsTrue(success.ValueOr(false)); }
public void Init() { this.TestContext.WriteLine($"{{ {System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName}.{System.Reflection.MethodBase.GetCurrentMethod().Name} }}"); // ReSharper disable once LocalNameCapturedOnly System.String hr; var exception1 = new System.ArgumentNullException(nameof(hr), "LogMessageTestExceptionNoEvent Broke"); var correlationId = System.Guid.NewGuid(); this._logMessage1 = new Generic.LogMessage <System.ArgumentNullException>() { CorrelationId = correlationId, //EventId = new Microsoft.Extensions.Logging.EventId(101010); Exception = exception1, //Formatted = new System.Func<System.String, System.Exception, System.String>((state, ex) => this.GetType().ToString()), LogLevel = Microsoft.Extensions.Logging.LogLevel.Trace, LogType = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName.RemoveAllSpecialCharacters(), State = exception1, Timestamp = System.DateTime.Now }; var exception2 = new System.Exception("This stupid thing broke!", exception1); this._logMessage2 = new Generic.LogMessage <System.Exception>() { CorrelationId = correlationId, //EventId = new Microsoft.Extensions.Logging.EventId(101010); Exception = exception2, // Formatted = new System.Func<System.String, System.Exception, // System.String>((state, ex) => this.GetType().ToString()), LogLevel = Microsoft.Extensions.Logging.LogLevel.Trace, LogType = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName.RemoveAllSpecialCharacters(), State = exception2, Timestamp = System.DateTime.Now }; this._logMessage3 = new Generic.LogMessage <System.String>() { CorrelationId = correlationId, EventId = new Microsoft.Extensions.Logging.EventId(101010), //Exception = exception, // Formatted = new System.Func<System.String, System.Exception, System.String>((state, ex) => this.GetType().ToString()), LogLevel = Microsoft.Extensions.Logging.LogLevel.Debug, LogType = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName.RemoveAllSpecialCharacters(), State = "LogMessageTestInnerExceptionNoEvent System.String State", Timestamp = System.DateTime.Now }; this._logMessage4 = new Generic.LogMessage <System.String>() { CorrelationId = correlationId, //EventId = new Microsoft.Extensions.Logging.EventId(101010), //Exception = exception, //Formatted = "", LogLevel = Microsoft.Extensions.Logging.LogLevel.Debug, LogType = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName.RemoveAllSpecialCharacters(), State = "LogMessageTestNoExceptionNoEvent State", Timestamp = System.DateTime.Now }; }
public static System.ArgumentNullException ReadArgumentNullException(this MemoryBuffer buffer) { System.String _message = buffer.ReadAString(); var result = new System.ArgumentNullException(_message); return result; }