Skip to content

Commit

Permalink
Fix null reference exception when running tests with Npgsql 8 (#3480)
Browse files Browse the repository at this point in the history
  • Loading branch information
hazzik committed Feb 4, 2024
1 parent 04cec6d commit 0978f4c
Showing 1 changed file with 8 additions and 8 deletions.
@@ -1,6 +1,6 @@
using System;
using System.Data.Common;
using NHibernate.Exceptions;
using Npgsql;

namespace NHibernate.Test.ExceptionsTest
{
Expand All @@ -10,23 +10,23 @@ public class PostgresExceptionConverterExample : ISQLExceptionConverter

public Exception Convert(AdoExceptionContextInfo exInfo)
{
var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as DbException;
if (sqle != null)
if (ADOExceptionHelper.ExtractDbException(exInfo.SqlException) is PostgresException pge)
{
string code = (string)sqle.GetType().GetProperty("Code").GetValue(sqle, null);

string code = pge.SqlState;
if (code == "23503")
{
return new ConstraintViolationException(exInfo.Message, sqle.InnerException, exInfo.Sql, null);
return new ConstraintViolationException(exInfo.Message, pge.InnerException, exInfo.Sql, null);
}

if (code == "42P01")
{
return new SQLGrammarException(exInfo.Message, sqle.InnerException, exInfo.Sql);
return new SQLGrammarException(exInfo.Message, pge.InnerException, exInfo.Sql);
}
}

return SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql);
}

#endregion
}
}
}

0 comments on commit 0978f4c

Please sign in to comment.