Skip to content

Commit cae2d0b

Browse files
committed
Improved ISQLExceptionConverter for future extensions
SVN: trunk@4372
1 parent 9983880 commit cae2d0b

18 files changed

+72
-58
lines changed

releasenotes.txt

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Build 2.1.0
2929
* CriteriaUtil is gone. NHibernate.Transform.Transformers now returns predefined IResultTransformer.
3030
* ISessionFactory.Settings is gone (moved to ISessionFactoryImplementor.Settings)
3131
* Obsolete ORACLE dialects was removed (new implementations are available)
32+
* ISQLExceptionConverter was changed in order to have more flexibility about information available for the conversion and followed management.
33+
* ADOException now use string instead SqlString
3234

3335
Build 2.1.0.Alpha2 (rev4167)
3436
========================

src/NHibernate.Test/ExceptionsTest/MSSQLExceptionConverterExample.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Data.SqlClient;
33
using NHibernate.Exceptions;
4-
using NHibernate.SqlCommand;
54

65
namespace NHibernate.Test.ExceptionsTest
76
{
@@ -11,17 +10,17 @@ public class MSSQLExceptionConverterExample : ISQLExceptionConverter
1110
{
1211
#region ISQLExceptionConverter Members
1312

14-
public ADOException Convert(Exception sqlException, string message, SqlString sql)
13+
public Exception Convert(AdoExceptionContextInfo exInfo)
1514
{
16-
SqlException sqle = ADOExceptionHelper.ExtractDbException(sqlException) as SqlException;
15+
SqlException sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as SqlException;
1716
if(sqle != null)
1817
{
1918
if (sqle.Number == 547)
20-
return new ConstraintViolationException(message, sqle.InnerException, sql, null);
19+
return new ConstraintViolationException(exInfo.Message, sqle.InnerException, exInfo.Sql, null);
2120
if (sqle.Number == 208)
22-
return new SQLGrammarException(message, sqle.InnerException, sql);
21+
return new SQLGrammarException(exInfo.Message, sqle.InnerException, exInfo.Sql);
2322
}
24-
return SQLStateConverter.HandledNonSpecificException(sqlException, message, sql);
23+
return SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql);
2524
}
2625

2726
#endregion

src/NHibernate.Test/ExceptionsTest/OracleClientExceptionConverterExample.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
using System;
22
using System.Data.OracleClient;
33
using NHibernate.Exceptions;
4-
using NHibernate.SqlCommand;
54

65
namespace NHibernate.Test.ExceptionsTest
76
{
87
public class OracleClientExceptionConverterExample : ISQLExceptionConverter
98
{
109
#region ISQLExceptionConverter Members
1110

12-
public ADOException Convert(Exception sqlException, string message, SqlString sql)
11+
public Exception Convert(AdoExceptionContextInfo exInfo)
1312
{
14-
var sqle = ADOExceptionHelper.ExtractDbException(sqlException) as OracleException;
13+
var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as OracleException;
1514
if (sqle != null)
1615
{
1716
if (sqle.Code == 1036)
1817
{
19-
return new ConstraintViolationException(message, sqle.InnerException, sql, null);
18+
return new ConstraintViolationException(exInfo.Message, sqle.InnerException, exInfo.Sql, null);
2019
}
2120
if (sqle.Code == 942)
2221
{
23-
return new SQLGrammarException(message, sqle.InnerException, sql);
22+
return new SQLGrammarException(exInfo.Message, sqle.InnerException, exInfo.Sql);
2423
}
2524
}
26-
return SQLStateConverter.HandledNonSpecificException(sqlException, message, sql);
25+
return SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql);
2726
}
2827

2928
#endregion

src/NHibernate.Test/ExceptionsTest/PostgresExceptionConverterExample.cs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
using System;
22
using System.Data.Common;
3-
using NHibernate;
43
using NHibernate.Exceptions;
5-
using NHibernate.SqlCommand;
64

75
public class PostgresExceptionConverterExample : ISQLExceptionConverter
86
{
97
#region ISQLExceptionConverter Members
108

11-
public ADOException Convert(Exception sqlException, string message, SqlString sql)
9+
public Exception Convert(AdoExceptionContextInfo exInfo)
1210
{
13-
var sqle = ADOExceptionHelper.ExtractDbException(sqlException) as DbException;
11+
var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as DbException;
1412
if (sqle != null)
1513
{
1614
string code = (string) sqle.GetType().GetProperty("Code").GetValue(sqle, null);
1715

1816
if (code == "23503")
1917
{
20-
return new ConstraintViolationException(message, sqle.InnerException, sql, null);
18+
return new ConstraintViolationException(exInfo.Message, sqle.InnerException, exInfo.Sql, null);
2119
}
2220
if (code == "42P01")
2321
{
24-
return new SQLGrammarException(message, sqle.InnerException, sql);
22+
return new SQLGrammarException(exInfo.Message, sqle.InnerException, exInfo.Sql);
2523
}
2624
}
27-
return SQLStateConverter.HandledNonSpecificException(sqlException, message, sql);
25+
return SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql);
2826
}
2927

3028
#endregion

src/NHibernate.Test/ExceptionsTest/SQLExceptionConversionTest.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void IntegrityViolation()
7979
catch (Exception sqle)
8080
{
8181
ADOExceptionReporter.LogExceptions(sqle, "Just output!!!!");
82-
ADOException adoException = converter.Convert(sqle, null, null);
82+
Exception adoException = converter.Convert(new AdoExceptionContextInfo{SqlException = sqle});
8383
Assert.AreEqual(typeof(ConstraintViolationException), adoException.GetType(),
8484
"Bad conversion [" + sqle.Message + "]");
8585
ConstraintViolationException ex = (ConstraintViolationException)adoException;
@@ -126,7 +126,9 @@ public void BadGrammar()
126126
}
127127
catch (Exception sqle)
128128
{
129-
Assert.AreEqual(typeof(SQLGrammarException), converter.Convert(sqle, null, null).GetType(), "Bad conversion [" + sqle.Message + "]");
129+
Assert.AreEqual(typeof (SQLGrammarException),
130+
converter.Convert(new AdoExceptionContextInfo {SqlException = sqle}).GetType(),
131+
"Bad conversion [" + sqle.Message + "]");
130132
}
131133
finally
132134
{

src/NHibernate/ADOException.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Runtime.Serialization;
33
using System.Security.Permissions;
4-
using NHibernate.SqlCommand;
54

65
namespace NHibernate
76
{
@@ -16,7 +15,7 @@ namespace NHibernate
1615
[Serializable]
1716
public class ADOException : HibernateException
1817
{
19-
private readonly SqlString sql;
18+
private readonly string sql;
2019

2120
/// <summary>
2221
/// Initializes a new instance of the <see cref="ADOException"/> class.
@@ -31,7 +30,7 @@ public ADOException(string message, Exception innerException) : base(message, in
3130
{
3231
}
3332

34-
public ADOException(string message, Exception innerException, SqlString sql)
33+
public ADOException(string message, Exception innerException, string sql)
3534
: base(message + "[SQL: " + sql + "]", innerException)
3635
{
3736
this.sql = sql;
@@ -49,7 +48,7 @@ public ADOException(string message, Exception innerException, SqlString sql)
4948
/// </param>
5049
protected ADOException(SerializationInfo info, StreamingContext context) : base(info, context)
5150
{
52-
this.sql = (SqlString) info.GetValue("sql", typeof(SqlString));
51+
this.sql = (string) info.GetValue("sql", typeof(string));
5352
}
5453

5554
[SecurityPermission(SecurityAction.LinkDemand,
@@ -60,7 +59,7 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont
6059
info.AddValue("sql", sql);
6160
}
6261

63-
public SqlString SqlString
62+
public string SqlString
6463
{
6564
get { return sql; }
6665
}

src/NHibernate/AdoNet/AbstractBatcher.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ public bool HasOpenResources
501501
get { return commandsToClose.Count > 0 || readersToClose.Count > 0; }
502502
}
503503

504-
protected ADOException Convert(Exception sqlException, string message)
504+
protected Exception Convert(Exception sqlException, string message)
505505
{
506506
return ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, sqlException, message);
507507
}

src/NHibernate/Exceptions/ADOConnectionException.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Runtime.Serialization;
3-
using NHibernate.SqlCommand;
43

54
namespace NHibernate.Exceptions
65
{
@@ -12,7 +11,7 @@ namespace NHibernate.Exceptions
1211
public class ADOConnectionException : ADOException
1312
{
1413
public ADOConnectionException(SerializationInfo info, StreamingContext context) : base(info, context) {}
15-
public ADOConnectionException(string message, Exception innerException, SqlString sql) : base(message, innerException, sql) {}
14+
public ADOConnectionException(string message, Exception innerException, string sql) : base(message, innerException, sql) {}
1615
public ADOConnectionException(string message, Exception innerException) : base(message, innerException) {}
1716
}
1817
}

src/NHibernate/Exceptions/ADOExceptionHelper.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ public static class ADOExceptionHelper
2121
/// <param name="message">An optional error message.</param>
2222
/// <param name="sql">The SQL executed.</param>
2323
/// <returns> The converted <see cref="ADOException"/>.</returns>
24-
public static ADOException Convert(ISQLExceptionConverter converter, Exception sqlException, string message,
24+
public static Exception Convert(ISQLExceptionConverter converter, Exception sqlException, string message,
2525
SqlString sql)
2626
{
2727
sql = TryGetActualSqlQuery(sqlException, sql);
2828
ADOExceptionReporter.LogExceptions(sqlException, ExtendMessage(message, sql, null, null));
29-
return converter.Convert(sqlException, message, sql);
29+
return
30+
converter.Convert(new AdoExceptionContextInfo {SqlException = sqlException, Message = message, Sql = sql.ToString()});
3031
}
3132

3233
/// <summary>
@@ -37,7 +38,7 @@ public static ADOException Convert(ISQLExceptionConverter converter, Exception s
3738
/// <param name="sqlException">The exception to convert.</param>
3839
/// <param name="message">An optional error message.</param>
3940
/// <returns> The converted <see cref="ADOException"/>.</returns>
40-
public static ADOException Convert(ISQLExceptionConverter converter, Exception sqlException, string message)
41+
public static Exception Convert(ISQLExceptionConverter converter, Exception sqlException, string message)
4142
{
4243
var sql = new SqlString(SQLNotAvailable);
4344
sql = TryGetActualSqlQuery(sqlException, sql);
@@ -50,7 +51,7 @@ public static ADOException Convert(ISQLExceptionConverter converter, Exception s
5051
sql = TryGetActualSqlQuery(sqle, sql);
5152
string extendMessage = ExtendMessage(message, sql, parameterValues, namedParameters);
5253
ADOExceptionReporter.LogExceptions(sqle, extendMessage);
53-
return new ADOException(extendMessage, sqle, sql);
54+
return new ADOException(extendMessage, sqle, sql.ToString());
5455
}
5556

5657
/// <summary> For the given <see cref="Exception"/>, locates the <see cref="System.Data.Common.DbException"/>. </summary>

src/NHibernate/Exceptions/ConstraintViolationException.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Runtime.Serialization;
3-
using NHibernate.SqlCommand;
43

54
namespace NHibernate.Exceptions
65
{
@@ -15,7 +14,7 @@ public class ConstraintViolationException : ADOException
1514
public ConstraintViolationException(SerializationInfo info, StreamingContext context)
1615
: base(info, context) {}
1716

18-
public ConstraintViolationException(string message, Exception innerException, SqlString sql, string constraintName)
17+
public ConstraintViolationException(string message, Exception innerException, string sql, string constraintName)
1918
: base(message, innerException, sql)
2019
{
2120
this.constraintName = constraintName;

src/NHibernate/Exceptions/DataException.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Runtime.Serialization;
3-
using NHibernate.SqlCommand;
43

54
namespace NHibernate.Exceptions
65
{
@@ -13,7 +12,7 @@ namespace NHibernate.Exceptions
1312
public class DataException : ADOException
1413
{
1514
public DataException(SerializationInfo info, StreamingContext context) : base(info, context) {}
16-
public DataException(string message, Exception innerException, SqlString sql) : base(message, innerException, sql) {}
15+
public DataException(string message, Exception innerException, string sql) : base(message, innerException, sql) {}
1716
public DataException(string message, Exception innerException) : base(message, innerException) {}
1817
}
1918
}

src/NHibernate/Exceptions/GenericADOException.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace NHibernate.Exceptions
88
public class GenericADOException : ADOException
99
{
1010
public GenericADOException(SerializationInfo info, StreamingContext context) : base(info, context) { }
11-
public GenericADOException(string message, Exception innerException, SqlString sql) : base(message, innerException, sql) { }
11+
public GenericADOException(string message, Exception innerException, string sql) : base(message, innerException, sql) { }
1212
public GenericADOException(string message, Exception innerException) : base(message, innerException) { }
1313
}
1414
}

src/NHibernate/Exceptions/ISQLExceptionConverter.cs

+29-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
11
using System;
2-
using NHibernate.SqlCommand;
32

43
namespace NHibernate.Exceptions
54
{
5+
/// <summary>
6+
/// Collect data of an <see cref="ADOException"/> to be converted.
7+
/// </summary>
8+
public class AdoExceptionContextInfo
9+
{
10+
// This class was introduced, in NH, to allow less intrusive possible extensions
11+
// of information given to the ISQLExceptionConverter
12+
// (extensions of a class instead succesive changes of a method)
13+
14+
/// <summary>
15+
/// The <see cref="System.Data.Common.DbException"/> to be converted.
16+
/// </summary>
17+
public Exception SqlException { get; set; }
18+
19+
/// <summary>
20+
/// An optional error message.
21+
/// </summary>
22+
public string Message { get; set; }
23+
24+
/// <summary>
25+
/// The SQL that generate the exception
26+
/// </summary>
27+
public string Sql { get; set; }
28+
}
29+
630
/// <summary>
731
/// Defines a contract for implementations that know how to convert a <see cref="System.Data.Common.DbException"/>
832
/// into NHibernate's <see cref="ADOException"/> hierarchy.
@@ -20,12 +44,10 @@ namespace NHibernate.Exceptions
2044
public interface ISQLExceptionConverter
2145
{
2246
/// <summary>
23-
/// Convert the given <see cref="System.Data.Common.DbException"/> into NHibernate's ADOException hierarchy.
47+
/// Convert the given <see cref="System.Data.Common.DbException"/> into custom Exception.
2448
/// </summary>
25-
/// <param name="sqlException">The <see cref="System.Data.Common.DbException"/> to be converted. </param>
26-
/// <param name="message"> An optional error message. </param>
27-
/// <param name="sql">The SQL that generate the exception</param>
28-
/// <returns> The resulting ADOException. </returns>
29-
ADOException Convert(Exception sqlException, string message, SqlString sql);
49+
/// <param name="adoExceptionContextInfo">Available information during exception throw.</param>
50+
/// <returns> The resulting Exception to throw. </returns>
51+
Exception Convert(AdoExceptionContextInfo adoExceptionContextInfo);
3052
}
3153
}

src/NHibernate/Exceptions/LockAcquisitionException.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Runtime.Serialization;
3-
using NHibernate.SqlCommand;
43

54
namespace NHibernate.Exceptions
65
{
@@ -12,7 +11,7 @@ namespace NHibernate.Exceptions
1211
public class LockAcquisitionException : ADOException
1312
{
1413
public LockAcquisitionException(SerializationInfo info, StreamingContext context) : base(info, context) {}
15-
public LockAcquisitionException(string message, Exception innerException, SqlString sql) : base(message, innerException, sql) {}
14+
public LockAcquisitionException(string message, Exception innerException, string sql) : base(message, innerException, sql) {}
1615
public LockAcquisitionException(string message, Exception innerException) : base(message, innerException) {}
1716
}
1817
}

src/NHibernate/Exceptions/SQLExceptionConverterFactory.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.Reflection;
44
using log4net;
5-
using NHibernate.SqlCommand;
65
using NHibernate.Util;
76

87
namespace NHibernate.Exceptions
@@ -16,9 +15,9 @@ private class MinimalSQLExceptionConverter : ISQLExceptionConverter
1615
{
1716
#region ISQLExceptionConverter Members
1817

19-
public ADOException Convert(Exception sqlException, string message, SqlString sql)
18+
public Exception Convert(AdoExceptionContextInfo exceptionContextInfo)
2019
{
21-
throw new GenericADOException(message, sqlException, sql);
20+
throw new GenericADOException(exceptionContextInfo.Message, exceptionContextInfo.SqlException, exceptionContextInfo.Sql);
2221
}
2322

2423
#endregion

src/NHibernate/Exceptions/SQLGrammarException.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Runtime.Serialization;
3-
using NHibernate.SqlCommand;
43

54
namespace NHibernate.Exceptions
65
{
@@ -12,7 +11,7 @@ namespace NHibernate.Exceptions
1211
public class SQLGrammarException : ADOException
1312
{
1413
public SQLGrammarException(SerializationInfo info, StreamingContext context) : base(info, context) {}
15-
public SQLGrammarException(string message, Exception innerException, SqlString sql) : base(message, innerException, sql) {}
14+
public SQLGrammarException(string message, Exception innerException, string sql) : base(message, innerException, sql) {}
1615
public SQLGrammarException(string message, Exception innerException) : base(message, innerException) {}
1716
}
1817
}

0 commit comments

Comments
 (0)