Skip to content

Fix null reference exception when running tests with Npgsql 8 #3480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
}
}
}