Created
September 18, 2013 11:25
-
-
Save nunofilipecosta/6607820 to your computer and use it in GitHub Desktop.
Database Raise Error exception converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data.SqlClient; | |
using System.Reflection; | |
using System.Text.RegularExpressions; | |
namespace Utilities.Database | |
{ | |
/// <summary> | |
/// Converts exceptions thrown by the DB into the correct exception | |
/// | |
/// Original code by Nuno Lourenço @ F6 Portugal | |
/// </summary> | |
public static class DatabaseExceptionConverter | |
{ | |
/// <summary> | |
/// Defines the format of the database exception messages | |
/// </summary> | |
private static readonly Regex dbExceptionExpression = new Regex(@"^(?<exception>\w+Exception\s*)\s*:?\s*(?<message>.*)$", RegexOptions.Singleline | RegexOptions.Compiled); | |
private static readonly IDictionary<string, Type> availableExceptions = InitAvailableExceptions(); | |
private static IDictionary<string, Type> InitAvailableExceptions() | |
{ | |
IDictionary<string, Type> ret = new Dictionary<string, Type>(); | |
foreach (Type exception in Assembly.GetExecutingAssembly().GetTypes()) | |
{ | |
if (typeof(Exception).IsAssignableFrom(exception)) | |
{ | |
ret.Add(exception.Name, exception); | |
} | |
} | |
return ret; | |
} | |
private static IDictionary<string, Type> AvailableExceptions | |
{ | |
get { return availableExceptions; } | |
} | |
/// <summary> | |
/// Converts the SqlException to known exception | |
/// </summary> | |
/// <remarks> | |
/// It is an error to pass an exception with a message that is matched by <see cref="dbExceptionExpression" /> | |
/// but for which there is no corresponding exception | |
/// </remarks> | |
/// <param name="exception">The exception.</param> | |
/// <returns>Returns an exception of the type defined in the exception message.</returns> | |
public static Exception Convert(SqlException exception) | |
{ | |
// Check the exception message for a match | |
Match match = dbExceptionExpression.Match(exception.Message); | |
if (!match.Success) | |
return exception; | |
string exceptionTypeName = match.Groups["exception"].Value; | |
string exceptionMessage = match.Groups["message"].Value; | |
Type exceptionType = GetExceptionType(exceptionTypeName); | |
if (exceptionType != null) | |
return (Exception)Activator.CreateInstance(exceptionType, exceptionMessage, exception); | |
return exception; | |
} | |
/// <summary> | |
/// Gets the type of the exception from the specified name. | |
/// </summary> | |
/// <param name="exceptionTypeName">Name of the exception's type.</param> | |
/// <returns></returns> | |
private static Type GetExceptionType(string exceptionTypeName) | |
{ | |
if (AvailableExceptions.ContainsKey(exceptionTypeName)) | |
return AvailableExceptions[exceptionTypeName]; | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment