forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathADOExceptionHelper.cs
125 lines (116 loc) · 4.8 KB
/
ADOExceptionHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Text;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.Util;
namespace NHibernate.Exceptions
{
public static class ADOExceptionHelper
{
public const string SQLNotAvailable = "SQL not available";
public static Exception Convert(ISQLExceptionConverter converter, AdoExceptionContextInfo exceptionContextInfo)
{
if(exceptionContextInfo == null)
{
throw new AssertionFailure("The argument exceptionContextInfo is null.");
}
var sql = TryGetActualSqlQuery(exceptionContextInfo.SqlException, exceptionContextInfo.Sql);
ADOExceptionReporter.LogExceptions(exceptionContextInfo.SqlException,
ExtendMessage(exceptionContextInfo.Message, sql, null, null));
return converter.Convert(exceptionContextInfo);
}
/// <summary>
/// Converts the given SQLException into Exception hierarchy, as well as performing
/// appropriate logging.
/// </summary>
/// <param name="converter">The converter to use.</param>
/// <param name="sqlException">The exception to convert.</param>
/// <param name="message">An optional error message.</param>
/// <param name="sql">The SQL executed.</param>
/// <returns> The converted <see cref="ADOException"/>.</returns>
public static Exception Convert(ISQLExceptionConverter converter, Exception sqlException, string message,
SqlString sql)
{
return Convert(converter,
new AdoExceptionContextInfo
{SqlException = sqlException, Message = message, Sql = sql != null ? sql.ToString() : null});
}
/// <summary>
/// Converts the given SQLException into Exception hierarchy, as well as performing
/// appropriate logging.
/// </summary>
/// <param name="converter">The converter to use.</param>
/// <param name="sqlException">The exception to convert.</param>
/// <param name="message">An optional error message.</param>
/// <returns> The converted <see cref="ADOException"/>.</returns>
public static Exception Convert(ISQLExceptionConverter converter, Exception sqlException, string message)
{
var sql = TryGetActualSqlQuery(sqlException, SQLNotAvailable);
return Convert(converter, new AdoExceptionContextInfo {SqlException = sqlException, Message = message, Sql = sql});
}
public static Exception Convert(ISQLExceptionConverter converter, Exception sqle, string message, SqlString sql,
object[] parameterValues, IDictionary<string, TypedValue> namedParameters)
{
sql = TryGetActualSqlQuery(sqle, sql);
string extendMessage = ExtendMessage(message, sql != null ? sql.ToString() : null, parameterValues, namedParameters);
ADOExceptionReporter.LogExceptions(sqle, extendMessage);
return Convert(converter, sqle, extendMessage, sql);
}
/// <summary> For the given <see cref="Exception"/>, locates the <see cref="System.Data.Common.DbException"/>. </summary>
/// <param name="sqlException">The exception from which to extract the <see cref="System.Data.Common.DbException"/> </param>
/// <returns> The <see cref="System.Data.Common.DbException"/>, or null. </returns>
public static DbException ExtractDbException(Exception sqlException)
{
Exception baseException = sqlException;
var result = sqlException as DbException;
while (result == null && baseException != null)
{
baseException = baseException.InnerException;
result = baseException as DbException;
}
return result;
}
public static string ExtendMessage(string message, string sql, object[] parameterValues,
IDictionary<string, TypedValue> namedParameters)
{
var sb = new StringBuilder();
sb.Append(message).AppendLine().Append("[ ").Append(sql ?? SQLNotAvailable).Append(" ]");
if (parameterValues != null && parameterValues.Length > 0)
{
sb.AppendLine().Append("Positional parameters: ");
for (int index = 0; index < parameterValues.Length; index++)
{
object value = parameterValues[index] ?? "null";
sb.Append(" #").Append(index).Append(">").Append(value);
}
}
if (namedParameters != null && namedParameters.Count > 0)
{
sb.AppendLine();
foreach (var namedParameter in namedParameters)
{
object value = namedParameter.Value.Value ?? "null";
sb.Append(" ").Append("Name:").Append(namedParameter.Key).Append(" - Value:").Append(value).Append(" [Type: ").Append(namedParameter.Value.Type).Append("]");
}
}
sb.AppendLine();
return sb.ToString();
}
public static SqlString TryGetActualSqlQuery(Exception sqle, SqlString sql)
{
var query = (string) sqle.Data["actual-sql-query"];
if (query != null)
{
sql = new SqlString(query);
}
return sql;
}
public static string TryGetActualSqlQuery(Exception sqle, string sql)
{
var query = (string)sqle.Data["actual-sql-query"];
return query ?? sql;
}
}
}