exceptions exception chain
This package contains
A simple package with extensible shield clauses
A package for handling exceptions based on Attributes, Inline and Exception Chaining
If you like or are using this project please give it a star. Thanks!
A simple package with extensible shield clauses.
Usage
public void ValidateContent(User user, string input)
{
Shield.Against.Null(User, nameof(user));
Shield.Throws.ArugmentNullException<string>((input) => { return input == null ? true : false; }, null, "param1"));
Shield.Throws.ArugmentNullException(Validate(""), null, "param1"));
Shield.Throws.ArgumentException((true || true), "param1"));
}
public bool Validate(string input)
{
if(string.IsNullOrEmpty(input))
{
return true;
}
}
Supported classes
- Shield.Against.NullOrEmpty
- Shield.Against.NullOrWhiteSpace
- Shield.Against.OutOfRange
- Shield.Against.LesserThan<T>
- Shield.Against.GreaterThan<T>
- Shield.Against.IsTypeOf<T>
- Shield.Against.NullOrEmpty<T>
- Shield.Throws.ArgumentException
- Shield.Throws.ArgumentNullException
- Shield.Throws.TimeoutException
- Shield.Throws.NotSupportedException
Extending with your own Guard Clauses
// use extension methods on IShieldClause for adding more functionality in "Against"
namespace Vishnu.ShieldClause
{
public static class ShieldClauseExtension
{
public static void Tst(this IShieldClause shieldClause, int input, string parameterName)
{
}
}
}
// use extension methods on ExceptionContainer for adding more functionality in "Throw"
namespace Vishnu.ShieldClause
{
public static class ShieldClauseExtension
{
public static void FileNotFoundException(this ExceptionContainer container, bool when, int input, string parameterName)
{
}
}
}
A package for handling exception and invokes specific action based on the exception
Description
Attribute Handle, supresses or handles the exceptions raised by any method. Optionally allows to invoke other action if any exception is handled or supressed. Handle attribute allows inheritance.
Usage
using Vishnu.HandleClause;
public class Foo
{
// Handles supress only one exception
[Handle(typeof(ArgumentNullException))]
public void Update(string value)
{
var result = Check(value);
}
// Handle multiple exceptions. Any custom exceptions can also be specifieed
[Handle(new Type[] { typeof(ArgumentNullException), typeof(ArgumentException), typeof(CustomException) })]
public int Sycnchorize()
{
return 0;
}
}
public void Test()
{
Foo foo = new Foo();
// call the specific method to be invoked.
Handle.Method<string>(foo.Update, "value");
Handle.Method(foo.Sycnchorize);
// pass the action to be excecuted if the exception is handled.
Handle.Method(foo.Sycnchorize, ExceptionHandledCallback);
}
// Action invoked if the exception is handled.
private void ExceptionHandledCallback(Exception ex)
{
//
}
Supported Actions
- Handle.Method(Action action, Action<Exception> exceptionHandledAction = null)
- Handle.Method<TInput>(Action<TInput> action, TInput input, Action<Exception> exceptionHandledAction = null)
- TResult result = Handle.Method<TResult>(Func<TResult> action, Action<Exception> exceptionHandledAction = null)
- TResult result = Handle.Method<TInput, TResult>(Func<TInput, TResult> action, TInput input, Action<Exception> exceptionHandledAction = null)
- TResult result = Handle.Method<TInput1, TInput2, TResult>(Func<TInput1, TInput2, TResult> action, TInput1 input1, TInput2 input2, Action<Exception> exceptionHandledAction = null)
- TResult result = Handle.Method<TInput1, TInput2, TInput3, TResult>(Func<TInput1, TInput2, TInput3, TResult> action, TInput1 input1, TInput2 input2, TInput3 input3, Action<Exception> exceptionHandledAction = null)
Description
Inline exception handling allows to handle or supress only one exception. Optionally, it allows to invoke an action if the exception is handled.
Usage
- Declare namespace
using Vishnu.HandleClause;
public class Boo
{
public void Update()
{
.....
}
public int Update(string value)
{
....
return 0;
}
}
public void Test()
{
Boo boo = new Boo();
// handles ArgumentException
Handle.Inline<ArgumentException>(boo.Update);
Handle.Inline<ArgumentException, string, int>(boo.Update, "hello");
// Handles ArgumentException and also invokes ExceptionHandledCallback if exception is handled.
Handle.Inline<ArgumentException, string, int>(boo.Update, "hello", this.ExceptionHandledCallback);
}
Supported Actions
- Handle.Inline<TException>(Action action, Action<Exception> exceptionHanldedAction = null) where TException : Exception
- Handle.Inline<TException, TInput>(Action<TInput> action, TInput input, Action<Exception> exceptionHanldedAction = null) where TException : Exception
- TResult result = Handle.Inline<TException, TResult>(Func<TResult> action, Action<Exception> exceptionHanldedAction = null) where TException : Exception
- TResult result = Handle.Inline<TException, TInput, TResult>(Func<TInput, TResult> action, TInput input, Action<Exception> exceptionHanldedAction = null) where TException : Exception
- TResult result = Handle.Inline<TException, TInput1, TInput2, TResult>(Func<TInput1, TInput2, TResult> action, TInput1 input1, TInput2 input2, Action<Exception> exceptionHanldedAction = null) where TException : Exception
- TResult result = Handle.Inline<TException, TInput1, TInput2, TInput3, TResult>(Func<TInput1, TInput2, TInput3, TResult> action, TInput1 input1, TInput2 input2, TInput3 input3, Action<Exception> exceptionHanldedAction = null) where TException : Exception
Description
Exception chaining allows to handle multiple exception. Optionally, it allows to invoke an action if the exception is handled.
Usage
- Declare namespace
using Vishnu.HandleClause;
public class Boo
{
public void Update()
{
.....
}
public int Update(string value)
{
....
return 0;
}
}
public void Test()
{
int result = Handle
.Case<ArgumentNullException>() // exception
.Or<CustomException>() // chain exception using Or
.Or<ArgumentException>()
.UseSync() // only synch methods allowed
.Execute<string int>(new Boo().Update, "test"); // call the method
// Handles ArgumentException and also invokes ExceptionHandledCallback if exception is handled.
int result = Handle
.Case<ArgumentNullException>() // exception
.UseSync() // only synch methods allowed
.Execute(new Boo().Update, ExceptionHandledCallback); // call the method
}
Supported Actions
Syntax :
[] - Optional
.Case<Exception>
[.Or<Exception>
.Or<Exception>
....]
.UseSync()
.Execute[<Parameters, ReturnValue>](action, [callback])
Overloaded execute methods
- Execute<TInput>(Action<TInput> action, TInput input, Action<Exception> exceptionHanldedAction = null)
- Execute(Action action, Action<Exception> exceptionHanldedAction = null)
- var result = Execute<TResult>(Func<TResult> action, Action<Exception> exceptionHanldedAction = null)
- var result = Execute<TInput, TResult>(Func<TInput, TResult> action, TInput input, Action<Exception> exceptionHanldedAction = null)
- var result = Execute<TInput1, TInput2, TResult>(Func<TInput1, TInput2, TResult> action, TInput1 input1, TInput2 input2, Action<Exception> exceptionHanldedAction = null)
- var result = Execute<TInput1, TInput2, TInput3, TResult>(Func<TInput1, TInput2, TInput3, TResult> action, TInput1 input1, TInput2 input2, TInput3 input3, Action<Exception> exceptionHanldedAction = null)