Class ConflictException
Exception thrown when an operation conflicts with the current state of a resource.
public class ConflictException : ResourceException, ISerializable
- Inheritance
-
ConflictException
- Implements
- Derived
- Inherited Members
Examples
if (await repository.ExistsAsync(user.Email, cancellationToken))
{
throw new ConflictException("User", "email", "A user with this email already exists.");
}
Remarks
This exception indicates that the requested operation cannot be completed because it would conflict with the current state of the target resource. It maps to HTTP status code 409 (Conflict).
Use this exception when:
- Attempting to create a resource that already exists
- Modifying a resource in a way that violates business rules
- State transition is not allowed from the current state
For optimistic locking failures (version mismatches), use ConcurrencyException instead.
Constructors
ConflictException()
Initializes a new instance of the ConflictException class.
public ConflictException()
ConflictException(string)
Initializes a new instance of the ConflictException class with a specified error message.
public ConflictException(string message)
Parameters
messagestringThe error message that explains the reason for the exception.
ConflictException(string, Exception?)
Initializes a new instance of the ConflictException class with a message and inner exception.
public ConflictException(string message, Exception? innerException)
Parameters
messagestringThe error message that explains the reason for the exception.
innerExceptionExceptionThe exception that is the cause of the current exception.
ConflictException(string, string, string)
Initializes a new instance of the ConflictException class with resource and field information.
public ConflictException(string resource, string field, string reason)
Parameters
resourcestringThe type or name of the resource involved in the conflict.
fieldstringThe field that caused the conflict.
reasonstringThe reason for the conflict.
Properties
Field
Gets the field that caused the conflict, if applicable.
public string? Field { get; protected init; }
Property Value
Reason
Gets the reason for the conflict.
public string? Reason { get; protected init; }
Property Value
Methods
AlreadyExists(string, string)
Creates a ConflictException for a duplicate resource scenario.
public static ConflictException AlreadyExists(string resource, string identifier)
Parameters
resourcestringThe type or name of the resource.
identifierstringThe identifier that already exists.
Returns
- ConflictException
A new ConflictException instance.
GetProblemDetailsExtensions()
Gets additional extension data to include in problem details.
protected override IDictionary<string, object?>? GetProblemDetailsExtensions()
Returns
- IDictionary<string, object>
A dictionary of extension data to include in the problem details response, or null if no extensions should be added.
Remarks
Derived types can override this method to add type-specific data to the
problem details response. For example, a ValidationException might
include a dictionary of field-level validation errors.
The returned dictionary keys will be added to the problem details Extensions collection.
InvalidStateTransition(string, string, string)
Creates a ConflictException for an invalid state transition.
public static ConflictException InvalidStateTransition(string resource, string currentState, string targetState)
Parameters
resourcestringThe type or name of the resource.
currentStatestringThe current state.
targetStatestringThe desired target state.
Returns
- ConflictException
A new ConflictException instance.
WithReason(string, string)
Creates a ConflictException with resource and reason information.
public static ConflictException WithReason(string resource, string reason)
Parameters
resourcestringThe type or name of the resource involved in the conflict.
reasonstringThe reason for the conflict.
Returns
- ConflictException
A new ConflictException instance.