Introduce UniqueKeyConstraintViolationException

Refactor DataProcessingException to remove unique key logic and create a dedicated UniqueKeyConstraintViolationException. Update DbExceptionTransformer to use the new exception for unique key violations, improving code clarity and separation of concerns.
This commit is contained in:
Raymond den Haan 2024-08-20 12:37:48 +02:00
parent 8a417ca765
commit 302292255c
3 changed files with 8 additions and 7 deletions

View file

@ -20,8 +20,9 @@ public class DbExceptionTransformer : IDbExceptionHandler<AlterationsElsaDbConte
/// Transforms database exceptions encountered when using a postgreSQL database into more generic exceptions.
public void Handle(DbUpdateException exception)
{
var ex = exception.InnerException as PostgresException;
if (exception.InnerException is PostgresException { SqlState: "23505" })
throw new UniqueKeyConstraintViolationException("Unable to save data", exception);
throw new DataProcessingException(ex?.SqlState == "23505", "Unable to save data", exception);
throw new DataProcessingException("Unable to save data", exception);
}
}

View file

@ -1,8 +1,4 @@
namespace Elsa.Workflows.Exceptions;
/// An exception that occurs during data processing.
public class DataProcessingException(bool isUkViolation, string message, Exception exception) : Exception(message, exception)
{
/// Gets a value indicating whether the exception is a Unique Key violation.
public bool IsUkViolation { get; } = isUkViolation;
}
public class DataProcessingException(string message, Exception exception) : Exception(message, exception);

View file

@ -0,0 +1,4 @@
namespace Elsa.Workflows.Exceptions;
/// An exception that when a unique key constraint has been violated.
public class UniqueKeyConstraintViolationException(string message, Exception exception) : Exception(message, exception);