Skip to content

Commit

Permalink
Fix PostgresExceptionConverterExample to work with Npgsql v8
Browse files Browse the repository at this point in the history
  • Loading branch information
hazzik committed Nov 30, 2023
1 parent fe9d737 commit 541dcb7
Showing 1 changed file with 7 additions and 8 deletions.
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,22 @@ 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 sqle)
{
string code = (string)sqle.GetType().GetProperty("Code").GetValue(sqle, null);

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

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

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

#endregion
}
}
}

0 comments on commit 541dcb7

Please sign in to comment.