namespace adas_core.Authentication.Models; /// /// Represents a generic response in the Universal Chess Interface (UCI) protocol, encapsulating a payload of type . /// /// The type of the response payload. public class UciResponse { protected UciResponse() { } protected UciResponse(T entity) { Success = true; Data = entity; Message = null; Error = null; } public string? Error { get; set; } public DateTime Date { get; set; } = DateTime.Now; public bool Success { get; protected set; } public string? Message { get; protected set; } public T? Data { get; protected set; } /// /// Creates a successful wrapping the specified entity. /// /// The entity to include in the response payload. /// A containing the provided entity. public static UciResponse FromSuccess(T entity) { return new UciResponse(entity); } /// /// Creates a representing an error state derived from the supplied exception, using the exception's type name (with the "Exception" suffix stripped) as the error code and its message as the error description. /// /// The exception whose type name and message are used to populate the error response. /// A containing the cleaned exception type name and the exception message as the error details. public static UciResponse FromError(Exception ex) { var error = ex.GetType().Name; var index = error.LastIndexOf("Exception", StringComparison.Ordinal); if (index > -1) error = error[..index]; return FromError(error, ex.Message); } /// /// Creates a instance representing a failed operation, populating the error information and an optional descriptive message. /// /// The error code or identifier describing the failure. /// An optional human-readable message providing additional context about the error. /// A with Success set to false, the specified , and the optional . public static UciResponse FromError(string error, string? message = null) { return new UciResponse { Success = false, Error = error, Message = message }; } }