Skip to content

Commit

Permalink
Add custom Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
martinsmith1968 committed Aug 24, 2024
1 parent ee78423 commit 9ca217e
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/DNX.Extensions/Exceptions/ConversionException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;

namespace DNX.Extensions.Exceptions
{
/// <summary>
/// Conversion Exception.
/// </summary>
/// <seealso cref="System.Exception" />
/// <remarks>Thrown when a conversion to a specified type fails</remarks>
public class ConversionException : Exception
{
/// <summary>
/// Gets the value.
/// </summary>
/// <value>The value.</value>
public string Value { get; private set; }

/// <summary>
/// Gets the type of the convert.
/// </summary>
/// <value>The type of the convert.</value>
public Type ConvertType { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="ConversionException" /> class.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="message">The message.</param>
public ConversionException(string value, string message)
: this(value, message, null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ConversionException" /> class.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="message">The message.</param>
/// <param name="type">The type.</param>
public ConversionException(string value, string message, Type type)
: base(message)
{
Value = value;
ConvertType = type;
}
}
}
43 changes: 43 additions & 0 deletions src/DNX.Extensions/Exceptions/EnumTypeException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;

namespace DNX.Extensions.Exceptions
{
/// <summary>
/// EnumTypeException.
/// </summary>
/// <seealso cref="System.Exception" />
public class EnumTypeException : Exception
{
/// <summary>
/// The message template
/// </summary>
private const string MessageTemplate = "{0} is not an enumeration type";

/// <summary>
/// The type the exception was thrown for
/// </summary>
/// <value>The type.</value>
public Type Type { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="EnumTypeException" /> class.
/// </summary>
/// <param name="type">The type.</param>
/// <remarks>Uses the default message template</remarks>
public EnumTypeException(Type type)
: this(type, MessageTemplate)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="EnumTypeException" /> class.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="messageTemplate">The message template.</param>
public EnumTypeException(Type type, string messageTemplate)
: base(string.Format(messageTemplate, type.Name))
{
Type = type;
}
}
}
49 changes: 49 additions & 0 deletions src/DNX.Extensions/Exceptions/EnumValueException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;

namespace DNX.Extensions.Exceptions
{
/// <summary>
/// Class EnumValueException.
/// </summary>
/// <seealso cref="System.Exception" />
public class EnumValueException<T> : Exception
{
/// <summary>
/// The message template
/// </summary>
private const string MessageTemplate = "{0} is not a valid {1} value";

/// <summary>
/// Gets the type.
/// </summary>
/// <value>The type.</value>
public Type Type { get; private set; }

/// <summary>
/// Gets the value.
/// </summary>
/// <value>The value.</value>
public T Value { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="EnumValueException{T}"/> class.
/// </summary>
/// <param name="value">The value.</param>
public EnumValueException(T value)
: this(value, MessageTemplate)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="EnumValueException{T}"/> class.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="messageTemplate">The message template.</param>
public EnumValueException(T value, string messageTemplate)
: base(string.Format(messageTemplate, value, typeof(T).Name))
{
Type = typeof(T);
Value = value;
}
}
}
51 changes: 51 additions & 0 deletions src/DNX.Extensions/Exceptions/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;

namespace DNX.Extensions.Exceptions
{
/// <summary>
/// Exception Extensions.
/// </summary>
public static class ExceptionExtensions
{
/// <summary>
/// Gets the list of messages from an Exception and inner exceptions
/// </summary>
/// <param name="ex">The ex.</param>
/// <returns>IList&lt;System.String&gt;.</returns>
/// <remarks>Also available as an extension method</remarks>
public static IList<string> GetMessageList(this Exception ex)
{
var list = new List<string>();

while (ex != null)
{
list.Add(ex.Message);

ex = ex.InnerException;
}

return list;
}

/// <summary>
/// Gets the list of messages from an Exception and inner exceptions
/// </summary>
/// <param name="ex">The ex.</param>
/// <returns>IList&lt;System.String&gt;.</returns>
/// <remarks>Also available as an extension method</remarks>
public static IList<Exception> GetExceptionList(this Exception ex)
{
var list = new List<Exception>();

while (ex != null)
{
list.Add(ex);

ex = ex.InnerException;
}

return list;
}
}
}
73 changes: 73 additions & 0 deletions src/DNX.Extensions/Exceptions/ParameterException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;

namespace DNX.Extensions.Exceptions
{
/// <summary>
/// An exception for idenifying issues with expected parameters
/// </summary>
/// <seealso cref="System.Exception" />
public class ParameterException : Exception
{
/// <summary>
/// The Parameter Name
/// </summary>
/// <value>The name of the parameter.</value>
public string ParamName { get; private set; }

/// <summary>
/// The value specified for the Parameter
/// </summary>
/// <value>The parameter value.</value>
public object ParamValue { get; private set; }

/// <summary>
/// Create a ParameterException with a parameter name and a message
/// </summary>
/// <param name="paramName">Name of the parameter.</param>
/// <param name="message">The message.</param>
public ParameterException(string paramName, string message)
: base(message)
{
ParamName = paramName;
}

/// <summary>
/// Create a ParameterException with a parameter name and value, and a message
/// </summary>
/// <param name="paramName">Name of the parameter.</param>
/// <param name="paramValue">The parameter value.</param>
/// <param name="message">The message.</param>
public ParameterException(string paramName, object paramValue, string message)
: base(message)
{
ParamName = paramName;
ParamValue = paramValue;
}

/// <summary>
/// Create a ParameterException with a parameter name, message and inner Exception
/// </summary>
/// <param name="paramName">Name of the parameter.</param>
/// <param name="message">The message.</param>
/// <param name="innerException">The inner exception.</param>
public ParameterException(string paramName, string message, Exception innerException)
: base(message, innerException)
{
ParamName = paramName;
}

/// <summary>
/// Create a ParameterException with a parameter name and value, message and inner Exception
/// </summary>
/// <param name="paramName">Name of the parameter.</param>
/// <param name="paramValue">The parameter value.</param>
/// <param name="message">The message.</param>
/// <param name="innerException">The inner exception.</param>
public ParameterException(string paramName, object paramValue, string message, Exception innerException)
: base(message, innerException)
{
ParamName = paramName;
ParamValue = paramValue;
}
}
}
55 changes: 55 additions & 0 deletions src/DNX.Extensions/Exceptions/ParameterInvalidException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;

namespace DNX.Extensions.Exceptions
{
/// <summary>
/// An exception for idenifying an invalid value issue with expected parameters
/// </summary>
/// <seealso cref="ParameterException" />
public class ParameterInvalidException : ParameterException
{
/// <summary>
/// Create a ParameterInvalidException with a parameter name and a message
/// </summary>
/// <param name="paramName">Name of the parameter.</param>
/// <param name="message">The message.</param>
public ParameterInvalidException(string paramName, string message)
: base(paramName, message)
{
}

/// <summary>
/// Create a ParameterInvalidException with a parameter name and value, and a message
/// </summary>
/// <param name="paramName">Name of the parameter.</param>
/// <param name="paramValue">The parameter value.</param>
/// <param name="message">The message.</param>
public ParameterInvalidException(string paramName, object paramValue, string message)
: base(paramName, paramValue, message)
{
}

/// <summary>
/// Create a ParameterInvalidException with a parameter name, message and inner Exception
/// </summary>
/// <param name="paramName">Name of the parameter.</param>
/// <param name="message">The message.</param>
/// <param name="innerException">The inner exception.</param>
public ParameterInvalidException(string paramName, string message, Exception innerException)
: base(paramName, message, innerException)
{
}

/// <summary>
/// Create a ParameterInvalidException with a parameter name and value, message and inner Exception
/// </summary>
/// <param name="paramName">Name of the parameter.</param>
/// <param name="paramValue">The parameter value.</param>
/// <param name="message">The message.</param>
/// <param name="innerException">The inner exception.</param>
public ParameterInvalidException(string paramName, object paramValue, string message, Exception innerException)
: base(paramName, paramValue, message, innerException)
{
}
}
}
28 changes: 28 additions & 0 deletions src/DNX.Extensions/Exceptions/ReadOnlyListException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;

namespace DNX.Extensions.Exceptions
{
/// <summary>
/// Class ReadOnlyListException.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <seealso cref="System.Exception" />
public class ReadOnlyListException<T> : Exception
{
/// <summary>
/// Gets the list.
/// </summary>
/// <value>The list.</value>
public IList<T> List { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="ReadOnlyListException{T}" /> class.
/// </summary>
/// <param name="list">The list.</param>
public ReadOnlyListException(IList<T> list)
{
List = list;
}
}
}

0 comments on commit 9ca217e

Please sign in to comment.