Skip to content

Commit 0978f4c

Browse files
authored
Fix null reference exception when running tests with Npgsql 8 (#3480)
1 parent 04cec6d commit 0978f4c

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
2-
using System.Data.Common;
32
using NHibernate.Exceptions;
3+
using Npgsql;
44

55
namespace NHibernate.Test.ExceptionsTest
66
{
@@ -10,23 +10,23 @@ public class PostgresExceptionConverterExample : ISQLExceptionConverter
1010

1111
public Exception Convert(AdoExceptionContextInfo exInfo)
1212
{
13-
var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as DbException;
14-
if (sqle != null)
13+
if (ADOExceptionHelper.ExtractDbException(exInfo.SqlException) is PostgresException pge)
1514
{
16-
string code = (string)sqle.GetType().GetProperty("Code").GetValue(sqle, null);
17-
15+
string code = pge.SqlState;
1816
if (code == "23503")
1917
{
20-
return new ConstraintViolationException(exInfo.Message, sqle.InnerException, exInfo.Sql, null);
18+
return new ConstraintViolationException(exInfo.Message, pge.InnerException, exInfo.Sql, null);
2119
}
20+
2221
if (code == "42P01")
2322
{
24-
return new SQLGrammarException(exInfo.Message, sqle.InnerException, exInfo.Sql);
23+
return new SQLGrammarException(exInfo.Message, pge.InnerException, exInfo.Sql);
2524
}
2625
}
26+
2727
return SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql);
2828
}
2929

3030
#endregion
3131
}
32-
}
32+
}

0 commit comments

Comments
 (0)