Table of Contents

ConflictException Class

Definition

Namespace
Cuemon.AspNetCore.Http
Assemblies
Cuemon.AspNetCore.dll
Source
src/Cuemon.AspNetCore/Http/ConflictException.cs

The exception that is thrown when a request conflicts with the current state of the server.

public class ConflictException : HttpStatusCodeException, ISerializable
Inheritance
ConflictException
Implements
Inherited Members

Examples

The following example demonstrates how to use to signal HTTP 409 Conflict responses. It compares the default, custom-message, inner-exception, and status-code parsing paths before applying a timestamp check that represents optimistic concurrency in an application service. The console output exposes the status code, reason phrase, and caller-facing message that an API exception handler can return to a client.

// Program.cs
using System;
using Cuemon.AspNetCore.Http;

// Create a ConflictException with default message
var ex = new ConflictException();
Console.WriteLine(ex.StatusCode);  // 409
Console.WriteLine(ex.ReasonPhrase); // Conflict
Console.WriteLine(ex.Message);     // The request could not be completed due to a conflict...

// Create with a custom message
var custom = new ConflictException("A record with the same email address already exists.");
Console.WriteLine(custom.Message);

// Create with inner exception
var inner = new InvalidOperationException("Duplicate key violation.");
var withInner = new ConflictException("Resource update conflict.", inner);
Console.WriteLine(withInner.InnerException?.Message); // Duplicate key violation.

// Use TryParse from the base class to resolve by status code
if (HttpStatusCodeException.TryParse(409, out var parsed))
{
    Console.WriteLine(parsed.Message);          // The request could not be completed due to a conflict.
    Console.WriteLine(parsed.StatusCode);     // 409
}

// Simulate a conflict check without throwing
var dbTimestamp = DateTime.UtcNow;
var clientTimestamp = dbTimestamp.AddHours(-1);
if (clientTimestamp < dbTimestamp)
{
    var conflict = new ConflictException(
        "The resource was modified by another user. Please refresh and retry.");
    Console.WriteLine(conflict.Message); // The resource was modified by another user...

}

Constructors

Name Description
ConflictException()

Initializes a new instance of the ConflictException class.

ConflictException(Exception)

Initializes a new instance of the ConflictException class.

ConflictException(string, Exception)

Initializes a new instance of the ConflictException class.

See Also