up
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
using Npgsql;
|
||||
|
||||
namespace StellaOps.Infrastructure.Postgres.Exceptions;
|
||||
|
||||
/// <summary>
|
||||
/// Helper methods for handling PostgreSQL exceptions.
|
||||
/// </summary>
|
||||
public static class PostgresExceptionHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// PostgreSQL error code for unique constraint violation.
|
||||
/// </summary>
|
||||
public const string UniqueViolation = "23505";
|
||||
|
||||
/// <summary>
|
||||
/// PostgreSQL error code for foreign key violation.
|
||||
/// </summary>
|
||||
public const string ForeignKeyViolation = "23503";
|
||||
|
||||
/// <summary>
|
||||
/// PostgreSQL error code for not null violation.
|
||||
/// </summary>
|
||||
public const string NotNullViolation = "23502";
|
||||
|
||||
/// <summary>
|
||||
/// PostgreSQL error code for check constraint violation.
|
||||
/// </summary>
|
||||
public const string CheckViolation = "23514";
|
||||
|
||||
/// <summary>
|
||||
/// PostgreSQL error code for serialization failure (retry needed).
|
||||
/// </summary>
|
||||
public const string SerializationFailure = "40001";
|
||||
|
||||
/// <summary>
|
||||
/// PostgreSQL error code for deadlock detected (retry needed).
|
||||
/// </summary>
|
||||
public const string DeadlockDetected = "40P01";
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the exception is a unique constraint violation.
|
||||
/// </summary>
|
||||
public static bool IsUniqueViolation(PostgresException ex)
|
||||
=> string.Equals(ex.SqlState, UniqueViolation, StringComparison.Ordinal);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the exception is a unique constraint violation for a specific constraint.
|
||||
/// </summary>
|
||||
public static bool IsUniqueViolation(PostgresException ex, string constraintName)
|
||||
=> IsUniqueViolation(ex) &&
|
||||
string.Equals(ex.ConstraintName, constraintName, StringComparison.Ordinal);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the exception is a foreign key violation.
|
||||
/// </summary>
|
||||
public static bool IsForeignKeyViolation(PostgresException ex)
|
||||
=> string.Equals(ex.SqlState, ForeignKeyViolation, StringComparison.Ordinal);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the exception is a not null violation.
|
||||
/// </summary>
|
||||
public static bool IsNotNullViolation(PostgresException ex)
|
||||
=> string.Equals(ex.SqlState, NotNullViolation, StringComparison.Ordinal);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the exception is a check constraint violation.
|
||||
/// </summary>
|
||||
public static bool IsCheckViolation(PostgresException ex)
|
||||
=> string.Equals(ex.SqlState, CheckViolation, StringComparison.Ordinal);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the exception is retryable (serialization failure or deadlock).
|
||||
/// </summary>
|
||||
public static bool IsRetryable(PostgresException ex)
|
||||
=> string.Equals(ex.SqlState, SerializationFailure, StringComparison.Ordinal) ||
|
||||
string.Equals(ex.SqlState, DeadlockDetected, StringComparison.Ordinal);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if any exception in the chain is retryable.
|
||||
/// </summary>
|
||||
public static bool IsRetryable(Exception ex)
|
||||
{
|
||||
var current = ex;
|
||||
while (current != null)
|
||||
{
|
||||
if (current is PostgresException pgEx && IsRetryable(pgEx))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
current = current.InnerException;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user