Skip to content

Commit 541dcb7

Browse files
committed
Fix PostgresExceptionConverterExample to work with Npgsql v8
1 parent fe9d737 commit 541dcb7

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-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,22 @@ 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 sqle)
1514
{
16-
string code = (string)sqle.GetType().GetProperty("Code").GetValue(sqle, null);
17-
18-
if (code == "23503")
15+
if (sqle.SqlState == "23503")
1916
{
2017
return new ConstraintViolationException(exInfo.Message, sqle.InnerException, exInfo.Sql, null);
2118
}
22-
if (code == "42P01")
19+
20+
if (sqle.SqlState == "42P01")
2321
{
2422
return new SQLGrammarException(exInfo.Message, sqle.InnerException, exInfo.Sql);
2523
}
2624
}
25+
2726
return SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql);
2827
}
2928

3029
#endregion
3130
}
32-
}
31+
}

0 commit comments

Comments
 (0)