up
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
AOC Guard CI / aoc-guard (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
Concelier Attestation Tests / attestation-tests (push) Has been cancelled

This commit is contained in:
StellaOps Bot
2025-11-28 20:55:22 +02:00
parent d040c001ac
commit 2548abc56f
231 changed files with 47468 additions and 68 deletions

View File

@@ -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;
}
}