rama creada apartir de master en j
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
namespace adas_core.Authentication.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a generic response in the Universal Chess Interface (UCI) protocol, encapsulating a payload of type <typeparamref name="T"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the response payload.</typeparam>
|
||||
public class UciResponse<T>
|
||||
{
|
||||
protected UciResponse()
|
||||
@@ -21,27 +25,43 @@ public class UciResponse<T>
|
||||
public T? Data { get; protected set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates a successful <see cref="UciResponse{T}"/> wrapping the specified entity.
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity to include in the response payload.</param>
|
||||
/// <returns>A <see cref="UciResponse{T}"/> containing the provided entity.</returns>
|
||||
public static UciResponse<T> FromSuccess(T entity)
|
||||
{
|
||||
return new UciResponse<T>(entity);
|
||||
}
|
||||
|
||||
|
||||
public static UciResponse<T> 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);
|
||||
}
|
||||
|
||||
public static UciResponse<T> FromError(string error, string? message = null)
|
||||
{
|
||||
return new UciResponse<T>
|
||||
{
|
||||
Success = false,
|
||||
Error = error,
|
||||
Message = message
|
||||
};
|
||||
}
|
||||
return new UciResponse<T>(entity);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="UciResponse{T}"/> 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.
|
||||
/// </summary>
|
||||
/// <param name="ex">The exception whose type name and message are used to populate the error response.</param>
|
||||
/// <returns>A <see cref="UciResponse{T}"/> containing the cleaned exception type name and the exception message as the error details.</returns>
|
||||
public static UciResponse<T> 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="UciResponse{T}"/> instance representing a failed operation, populating the error information and an optional descriptive message.
|
||||
/// </summary>
|
||||
/// <param name="error">The error code or identifier describing the failure.</param>
|
||||
/// <param name="message">An optional human-readable message providing additional context about the error.</param>
|
||||
/// <returns>A <see cref="UciResponse{T}"/> with <c>Success</c> set to <c>false</c>, the specified <paramref name="error"/>, and the optional <paramref name="message"/>.</returns>
|
||||
public static UciResponse<T> FromError(string error, string? message = null)
|
||||
{
|
||||
return new UciResponse<T>
|
||||
{
|
||||
Success = false,
|
||||
Error = error,
|
||||
Message = message
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user