mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
Merge pull request #155 from BinaryAlley/feature/web-result-primitives
Adds Result and Error primitives to the web project
This commit is contained in:
20 files changed
+850
No files matched your search
@@ -0,0 +1,42 @@
|
|||||||
|
namespace Lumina.Presentation.Web.Common.Enums.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Enumeration for the type of an error.
|
||||||
|
/// </summary>
|
||||||
|
public enum ErrorType
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a generic failure.
|
||||||
|
/// </summary>
|
||||||
|
Failure,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents an unexpected error.
|
||||||
|
/// </summary>
|
||||||
|
Unexpected,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a validation error.
|
||||||
|
/// </summary>
|
||||||
|
Validation,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a conflict error.
|
||||||
|
/// </summary>
|
||||||
|
Conflict,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a not found error.
|
||||||
|
/// </summary>
|
||||||
|
NotFound,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents an unauthorized error.
|
||||||
|
/// </summary>
|
||||||
|
Unauthorized,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a forbidden error.
|
||||||
|
/// </summary>
|
||||||
|
Forbidden,
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#region ========================================================================= USING =====================================================================================
|
||||||
|
using Lumina.Presentation.Web.Common.Enums.Primitives;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Error representing that the operation could not be completed due to a conflict with the current state.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Code">The error code that uniquely identifies this error type.</param>
|
||||||
|
/// <param name="Description">The human-readable description of the error.</param>
|
||||||
|
public record ConflictError(string Code, string Description) : Error
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the type of this error.
|
||||||
|
/// </summary>
|
||||||
|
public override ErrorType Type => ErrorType.Conflict;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the error code that uniquely identifies this error type.
|
||||||
|
/// </summary>
|
||||||
|
public override string Code { get; } = Code;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the human-readable description of the error.
|
||||||
|
/// </summary>
|
||||||
|
public override string Description { get; } = Description;
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a successful creation operation result.
|
||||||
|
/// </summary>
|
||||||
|
public readonly record struct Created;
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a successful deletion operation result.
|
||||||
|
/// </summary>
|
||||||
|
public readonly record struct Deleted;
|
||||||
@@ -0,0 +1,167 @@
|
|||||||
|
#region ========================================================================= USING =====================================================================================
|
||||||
|
using Lumina.Presentation.Web.Common.Enums.Primitives;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents an error that can be implicitly converted to a Result.
|
||||||
|
/// </summary>
|
||||||
|
public abstract record Error
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the type of this error.
|
||||||
|
/// </summary>
|
||||||
|
public abstract ErrorType Type { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the error code that uniquely identifies this error type.
|
||||||
|
/// </summary>
|
||||||
|
public abstract string Code { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the human-readable description of the error.
|
||||||
|
/// </summary>
|
||||||
|
public abstract string Description { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a string representation of the error.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A formatted string containing the error code and description.</returns>
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return $"{Code}: {Description}";
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new NotFoundError.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">The error code, defaults to "General.NotFound" when null.</param>
|
||||||
|
/// <param name="description">The human-readable description of the error.</param>
|
||||||
|
/// <returns>A new NotFoundError instance.</returns>
|
||||||
|
public static NotFoundError NotFound(string? code = null, string? description = null)
|
||||||
|
{
|
||||||
|
return new(code ?? "General.NotFound", description ?? "A 'Not Found' error has occurred.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new InvalidOperationError.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">The error code, defaults to "General.InvalidOperation" when null.</param>
|
||||||
|
/// <param name="description">The human-readable description of the error.</param>
|
||||||
|
/// <returns>A new InvalidOperationError instance.</returns>
|
||||||
|
public static InvalidOperationError InvalidOperation(string? code = null, string? description = null)
|
||||||
|
{
|
||||||
|
return new(code ?? "General.InvalidOperation", description ?? "An invalid operation error has occurred.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new ForbiddenError.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">The error code, defaults to "General.Forbidden" when null.</param>
|
||||||
|
/// <param name="description">The human-readable description of the error.</param>
|
||||||
|
/// <returns>A new ForbiddenError instance.</returns>
|
||||||
|
public static ForbiddenError Forbidden(string? code = null, string? description = null)
|
||||||
|
{
|
||||||
|
return new(code ?? "General.Forbidden", description ?? "A 'Forbidden' error has occurred.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new FailureError.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">The error code, defaults to "General.Failure" when null.</param>
|
||||||
|
/// <param name="description">The human-readable description of the error.</param>
|
||||||
|
/// <returns>A new FailureError instance.</returns>
|
||||||
|
public static FailureError Failure(string? code = null, string? description = null)
|
||||||
|
{
|
||||||
|
return new(code ?? "General.Failure", description ?? "A failure has occurred.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new ValidationError.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">The error code, defaults to "General.Validation" when null.</param>
|
||||||
|
/// <param name="description">The human-readable description of the error.</param>
|
||||||
|
/// <returns>A new ValidationError instance.</returns>
|
||||||
|
public static ValidationError Validation(string? code = null, string? description = null)
|
||||||
|
{
|
||||||
|
return new(code ?? "General.Validation", description ?? "A validation error has occurred.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new ConflictError.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">The error code, defaults to "General.Conflict" when null.</param>
|
||||||
|
/// <param name="description">The human-readable description of the error.</param>
|
||||||
|
/// <returns>A new ConflictError instance.</returns>
|
||||||
|
public static ConflictError Conflict(string? code = null, string? description = null)
|
||||||
|
{
|
||||||
|
return new(code ?? "General.Conflict", description ?? "A conflict error has occurred.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new TimeoutError.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">The error code, defaults to "General.Timeout" when null.</param>
|
||||||
|
/// <param name="description">The human-readable description of the error.</param>
|
||||||
|
/// <returns>A new TimeoutError instance.</returns>
|
||||||
|
public static TimeoutError Timeout(string? code = null, string? description = null)
|
||||||
|
{
|
||||||
|
return new(code ?? "General.Timeout", description ?? "The operation timed out.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new UnauthorizedError.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">The error code, defaults to "General.Unauthorized" when null.</param>
|
||||||
|
/// <param name="description">The human-readable description of the error.</param>
|
||||||
|
/// <returns>A new UnauthorizedError instance.</returns>
|
||||||
|
public static UnauthorizedError Unauthorized(string? code = null, string? description = null)
|
||||||
|
{
|
||||||
|
return new(code ?? "General.Unauthorized", description ?? "An 'Unauthorized' error has occurred.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new UnexpectedError.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">The error code, defaults to "General.Unexpected" when null.</param>
|
||||||
|
/// <param name="description">The human-readable description of the error.</param>
|
||||||
|
/// <returns>A new UnexpectedError instance.</returns>
|
||||||
|
public static UnexpectedError Unexpected(string? code = null, string? description = null)
|
||||||
|
{
|
||||||
|
return new(code ?? "General.Unexpected", description ?? "An unexpected error has occurred.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new InternalError.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">The error code, defaults to "General.Internal" when null.</param>
|
||||||
|
/// <param name="description">The human-readable description of the error.</param>
|
||||||
|
/// <returns>A new InternalError instance.</returns>
|
||||||
|
public static InternalError Internal(string? code = null, string? description = null)
|
||||||
|
{
|
||||||
|
return new(code ?? "General.Internal", description ?? "An internal error has occurred.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new ResourceUnavailableError.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">The error code, defaults to "General.ResourceUnavailable" when null.</param>
|
||||||
|
/// <param name="description">The human-readable description of the error.</param>
|
||||||
|
/// <returns>A new ResourceUnavailableError instance.</returns>
|
||||||
|
public static ResourceUnavailableError ResourceUnavailable(string? code = null, string? description = null)
|
||||||
|
{
|
||||||
|
return new(code ?? "General.ResourceUnavailable", description ?? "The required resource is currently unavailable.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new NotImplementedError.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="code">The error code, defaults to "General.NotImplemented" when null.</param>
|
||||||
|
/// <param name="description">The human-readable description of the error.</param>
|
||||||
|
/// <returns>A new NotImplementedError instance.</returns>
|
||||||
|
public static NotImplementedError NotImplemented(string? code = null, string? description = null)
|
||||||
|
{
|
||||||
|
return new(code ?? "General.NotImplemented", description ?? "The requested functionality is not implemented.");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#region ========================================================================= USING =====================================================================================
|
||||||
|
using Lumina.Presentation.Web.Common.Enums.Primitives;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Error representing that some operation failing.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Code">The error code that uniquely identifies this error type.</param>
|
||||||
|
/// <param name="Description">The human-readable description of the error.</param>
|
||||||
|
public record FailureError(string Code, string Description) : Error
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the type of this error.
|
||||||
|
/// </summary>
|
||||||
|
public override ErrorType Type => ErrorType.Failure;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the error code that uniquely identifies this error type.
|
||||||
|
/// </summary>
|
||||||
|
public override string Code { get; } = Code;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the human-readable description of the error.
|
||||||
|
/// </summary>
|
||||||
|
public override string Description { get; } = Description;
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#region ========================================================================= USING =====================================================================================
|
||||||
|
using Lumina.Presentation.Web.Common.Enums.Primitives;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Error representing that the operation is not allowed due to insufficient permissions or restrictions.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Code">The error code that uniquely identifies this error type.</param>
|
||||||
|
/// <param name="Description">The human-readable description of the error.</param>
|
||||||
|
public record ForbiddenError(string Code, string Description) : Error
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the type of this error.
|
||||||
|
/// </summary>
|
||||||
|
public override ErrorType Type => ErrorType.Forbidden;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the error code that uniquely identifies this error type.
|
||||||
|
/// </summary>
|
||||||
|
public override string Code { get; } = Code;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the human-readable description of the error.
|
||||||
|
/// </summary>
|
||||||
|
public override string Description { get; } = Description;
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#region ========================================================================= USING =====================================================================================
|
||||||
|
using Lumina.Presentation.Web.Common.Enums.Primitives;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Error representing that an unexpected internal error occurred.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Code">The error code that uniquely identifies this error type.</param>
|
||||||
|
/// <param name="Description">The human-readable description of the error.</param>
|
||||||
|
public record InternalError(string Code, string Description) : Error
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the type of this error.
|
||||||
|
/// </summary>
|
||||||
|
public override ErrorType Type => ErrorType.Unexpected;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the error code that uniquely identifies this error type.
|
||||||
|
/// </summary>
|
||||||
|
public override string Code { get; } = Code;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the human-readable description of the error.
|
||||||
|
/// </summary>
|
||||||
|
public override string Description { get; } = Description;
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#region ========================================================================= USING =====================================================================================
|
||||||
|
using Lumina.Presentation.Web.Common.Enums.Primitives;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Error representing that the operation is invalid in the current state or context.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Code">The error code that uniquely identifies this error type.</param>
|
||||||
|
/// <param name="Description">The human-readable description of the error.</param>
|
||||||
|
public record InvalidOperationError(string Code, string Description) : Error
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the type of this error.
|
||||||
|
/// </summary>
|
||||||
|
public override ErrorType Type => ErrorType.Unexpected;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the error code that uniquely identifies this error type.
|
||||||
|
/// </summary>
|
||||||
|
public override string Code { get; } = Code;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the human-readable description of the error.
|
||||||
|
/// </summary>
|
||||||
|
public override string Description { get; } = Description;
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#region ========================================================================= USING =====================================================================================
|
||||||
|
using Lumina.Presentation.Web.Common.Enums.Primitives;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Error representing that the requested resource or entity was not found.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Code">The error code that uniquely identifies this error type.</param>
|
||||||
|
/// <param name="Description">The human-readable description of the error.</param>
|
||||||
|
public record NotFoundError(string Code, string Description) : Error
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the type of this error.
|
||||||
|
/// </summary>
|
||||||
|
public override ErrorType Type => ErrorType.NotFound;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the error code that uniquely identifies this error type.
|
||||||
|
/// </summary>
|
||||||
|
public override string Code { get; } = Code;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the human-readable description of the error.
|
||||||
|
/// </summary>
|
||||||
|
public override string Description { get; } = Description;
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#region ========================================================================= USING =====================================================================================
|
||||||
|
using Lumina.Presentation.Web.Common.Enums.Primitives;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Error representing that the requested functionality is not implemented.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Code">The error code that uniquely identifies this error type.</param>
|
||||||
|
/// <param name="Description">The human-readable description of the error.</param>
|
||||||
|
public record NotImplementedError(string Code, string Description) : Error
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the type of this error.
|
||||||
|
/// </summary>
|
||||||
|
public override ErrorType Type => ErrorType.Unexpected;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the error code that uniquely identifies this error type.
|
||||||
|
/// </summary>
|
||||||
|
public override string Code { get; } = Code;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the human-readable description of the error.
|
||||||
|
/// </summary>
|
||||||
|
public override string Description { get; } = Description;
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#region ========================================================================= USING =====================================================================================
|
||||||
|
using Lumina.Presentation.Web.Common.Enums.Primitives;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Error representing that the required resource is currently unavailable.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Code">The error code that uniquely identifies this error type.</param>
|
||||||
|
/// <param name="Description">The human-readable description of the error.</param>
|
||||||
|
public record ResourceUnavailableError(string Code, string Description) : Error
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the type of this error.
|
||||||
|
/// </summary>
|
||||||
|
public override ErrorType Type => ErrorType.Unexpected;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the error code that uniquely identifies this error type.
|
||||||
|
/// </summary>
|
||||||
|
public override string Code { get; } = Code;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the human-readable description of the error.
|
||||||
|
/// </summary>
|
||||||
|
public override string Description { get; } = Description;
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Provides predefined success results for operations that don't return meaningful data, and factory methods for results.
|
||||||
|
/// </summary>
|
||||||
|
public static class Result
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a successful operation.
|
||||||
|
/// </summary>
|
||||||
|
public static Success Success { get; } = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a successful creation operation.
|
||||||
|
/// </summary>
|
||||||
|
public static Created Created { get; } = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a successful update operation.
|
||||||
|
/// </summary>
|
||||||
|
public static Updated Updated { get; } = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a successful deletion operation.
|
||||||
|
/// </summary>
|
||||||
|
public static Deleted Deleted { get; } = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a successful result wrapping the provided value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TValue">The type of the value to wrap.</typeparam>
|
||||||
|
/// <param name="value">The value to wrap in a successful result.</param>
|
||||||
|
/// <returns>A successful result containing the provided value.</returns>
|
||||||
|
public static Result<TValue> From<TValue>(TValue value)
|
||||||
|
{
|
||||||
|
return Result<TValue>.Success(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,243 @@
|
|||||||
|
#region ========================================================================= USING =====================================================================================
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Primitive struct for explicitly handling success and failure cases in a functional manner.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TValue">The type of the success value.</typeparam>
|
||||||
|
public readonly struct Result<TValue> : IEquatable<Result<TValue>>
|
||||||
|
{
|
||||||
|
private readonly TValue? _value;
|
||||||
|
private readonly List<Error>? _errors;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a value indicating whether this result represents a success.
|
||||||
|
/// </summary>
|
||||||
|
public bool IsSuccess { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a value indicating whether this result represents a failure.
|
||||||
|
/// </summary>
|
||||||
|
public bool IsFailure => !IsSuccess;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the success value. Only valid when IsSuccess is true.
|
||||||
|
/// </summary>
|
||||||
|
public TValue Value => IsSuccess ? _value! : throw new InvalidOperationException("Cannot access Value of a failed result.");
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the list of errors. Only valid when IsFailure is true.
|
||||||
|
/// </summary>
|
||||||
|
public List<Error> Errors => IsFailure
|
||||||
|
? _errors ?? []
|
||||||
|
: throw new InvalidOperationException("Cannot access Errors of a successful result.");
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the first error, if present.
|
||||||
|
/// </summary>
|
||||||
|
public Error FirstError => Errors[0];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="Result{TValue}"/> structure.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The success value.</param>
|
||||||
|
/// <param name="errors">The list of errors.</param>
|
||||||
|
/// <param name="isSuccess">Whether this represents a success or failure.</param>
|
||||||
|
private Result(TValue? value, List<Error>? errors, bool isSuccess)
|
||||||
|
{
|
||||||
|
_value = value;
|
||||||
|
_errors = errors;
|
||||||
|
IsSuccess = isSuccess;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a successful result with the specified value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The success value.</param>
|
||||||
|
/// <returns>A successful result containing the specified value.</returns>
|
||||||
|
public static Result<TValue> Success(TValue value)
|
||||||
|
{
|
||||||
|
return new Result<TValue>(value, default, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a failed result with the specified errors.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="errors">The errors value.</param>
|
||||||
|
/// <returns>A failed result containing the specified error.</returns>
|
||||||
|
public static Result<TValue> Failure(List<Error> errors)
|
||||||
|
{
|
||||||
|
return new Result<TValue>(default, errors, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a failed result with the specified errors.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="errors">The errors value.</param>
|
||||||
|
/// <returns>A failed result containing the specified error.</returns>
|
||||||
|
public static Result<TValue> Failure(params Error[] errors)
|
||||||
|
{
|
||||||
|
return new(default, [.. errors], false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Custom implementation operator for converting <paramref name="value"/> to a <see cref="Result{TValue}"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The value to be converted.</param>
|
||||||
|
public static implicit operator Result<TValue>(TValue value)
|
||||||
|
{
|
||||||
|
return Success(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Custom implementation operator for converting <paramref name="error"/> to a failed <see cref="Result{TValue}"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="error">The error to be converted.</param>
|
||||||
|
public static implicit operator Result<TValue>(Error error)
|
||||||
|
{
|
||||||
|
return Failure(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Custom implementation operator for converting <paramref name="errors"/> to a failed <see cref="Result{TValue}"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="errors">The errors to be converted.</param>
|
||||||
|
public static implicit operator Result<TValue>(List<Error> errors)
|
||||||
|
{
|
||||||
|
return Failure(errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Maps the success value to a new type using the provided function.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TNewValue">The type of the new success value.</typeparam>
|
||||||
|
/// <param name="mapper">The function to transform the success value.</param>
|
||||||
|
/// <returns>A new result with the transformed value, or the original errors.</returns>
|
||||||
|
public Result<TNewValue> Map<TNewValue>(Func<TValue, TNewValue> mapper)
|
||||||
|
{
|
||||||
|
return IsSuccess
|
||||||
|
? Result<TNewValue>.Success(mapper(Value))
|
||||||
|
: Result<TNewValue>.Failure(Errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Chains another operation that returns a Result, allowing for monadic composition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TNewValue">The type of the new success value.</typeparam>
|
||||||
|
/// <param name="binder">The function that returns a new Results.</param>
|
||||||
|
/// <returns>The result of the chained operation, or the original error.</returns>
|
||||||
|
public Result<TNewValue> Bind<TNewValue>(Func<TValue, Result<TNewValue>> binder)
|
||||||
|
{
|
||||||
|
return IsSuccess ? binder(Value) : Result<TNewValue>.Failure(Errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Executes an action if the result is successful, otherwise does nothing.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="action">The action to execute on success.</param>
|
||||||
|
/// <returns>The original result for chaining.</returns>
|
||||||
|
public Result<TValue> OnSuccess(Action<TValue> action)
|
||||||
|
{
|
||||||
|
if (IsSuccess)
|
||||||
|
action(Value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Executes an action if the result is a failure, otherwise does nothing.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="action">The action to execute on failure.</param>
|
||||||
|
/// <returns>The original result for chaining.</returns>
|
||||||
|
public Result<TValue> OnFailure(Action<IReadOnlyList<Error>> action)
|
||||||
|
{
|
||||||
|
if (IsFailure)
|
||||||
|
action(Errors);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Matches the result and executes the appropriate function.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TResult">The type of the result.</typeparam>
|
||||||
|
/// <param name="onSuccess">Function to execute on success.</param>
|
||||||
|
/// <param name="onFailure">Function to execute on failure.</param>
|
||||||
|
/// <returns>The result of the executed function.</returns>
|
||||||
|
public TResult Match<TResult>(Func<TValue, TResult> onSuccess, Func<List<Error>, TResult> onFailure)
|
||||||
|
{
|
||||||
|
return IsSuccess ? onSuccess(Value) : onFailure(Errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines whether the specified Result is equal to the current Results.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="other">The Result to compare with the current Results.</param>
|
||||||
|
/// <returns>true if the specified Result is equal to the current Result; otherwise, false.</returns>
|
||||||
|
public bool Equals(Result<TValue> other)
|
||||||
|
{
|
||||||
|
if (IsSuccess != other.IsSuccess)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return IsSuccess
|
||||||
|
? EqualityComparer<TValue>.Default.Equals(Value, other.Value)
|
||||||
|
: Errors.SequenceEqual(other.Errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines whether the specified object is equal to the current Results.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="obj">The object to compare with the current Results.</param>
|
||||||
|
/// <returns>true if the specified object is equal to the current Result; otherwise, false.</returns>
|
||||||
|
public override bool Equals(object? obj)
|
||||||
|
{
|
||||||
|
return obj is Result<TValue> other && Equals(other);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines whether two specified Result instances are equal.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="left">The first Result to compare.</param>
|
||||||
|
/// <param name="right">The second Result to compare.</param>
|
||||||
|
/// <returns>true if the two Result instances are equal; otherwise, false.</returns>
|
||||||
|
public static bool operator ==(Result<TValue> left, Result<TValue> right)
|
||||||
|
{
|
||||||
|
return left.Equals(right);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines whether two specified Result instances are not equal.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="left">The first Result to compare.</param>
|
||||||
|
/// <param name="right">The second Result to compare.</param>
|
||||||
|
/// <returns>true if the two Result instances are not equal; otherwise, false.</returns>
|
||||||
|
public static bool operator !=(Result<TValue> left, Result<TValue> right)
|
||||||
|
{
|
||||||
|
return !(left == right);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the hash code for this Results.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A 32-bit signed integer hash code.</returns>
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
return IsSuccess
|
||||||
|
? HashCode.Combine(IsSuccess, Value)
|
||||||
|
: HashCode.Combine(IsSuccess, Errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Customized ToString() method.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Custom string value showing relevant data for current result.</returns>
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return IsSuccess
|
||||||
|
? $"Success: {Value?.ToString()}"
|
||||||
|
: $"Failure: [{string.Join(", ", Errors.Select(error => error.ToString()))}]";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a successful operation result.
|
||||||
|
/// </summary>
|
||||||
|
public readonly record struct Success;
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#region ========================================================================= USING =====================================================================================
|
||||||
|
using Lumina.Presentation.Web.Common.Enums.Primitives;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Error representing that the operation timed out before completion.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Code">The error code that uniquely identifies this error type.</param>
|
||||||
|
/// <param name="Description">The human-readable description of the error.</param>
|
||||||
|
public record TimeoutError(string Code, string Description) : Error
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the type of this error.
|
||||||
|
/// </summary>
|
||||||
|
public override ErrorType Type => ErrorType.Unexpected;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the error code that uniquely identifies this error type.
|
||||||
|
/// </summary>
|
||||||
|
public override string Code { get; } = Code;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the human-readable description of the error.
|
||||||
|
/// </summary>
|
||||||
|
public override string Description { get; } = Description;
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#region ========================================================================= USING =====================================================================================
|
||||||
|
using Lumina.Presentation.Web.Common.Enums.Primitives;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Error representing that authentication is required or has failed.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Code">The error code that uniquely identifies this error type.</param>
|
||||||
|
/// <param name="Description">The human-readable description of the error.</param>
|
||||||
|
public record UnauthorizedError(string Code, string Description) : Error
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the type of this error.
|
||||||
|
/// </summary>
|
||||||
|
public override ErrorType Type => ErrorType.Unauthorized;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the error code that uniquely identifies this error type.
|
||||||
|
/// </summary>
|
||||||
|
public override string Code { get; } = Code;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the human-readable description of the error.
|
||||||
|
/// </summary>
|
||||||
|
public override string Description { get; } = Description;
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#region ========================================================================= USING =====================================================================================
|
||||||
|
using Lumina.Presentation.Web.Common.Enums.Primitives;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Error representing that an unexpected error occurred.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Code">The error code that uniquely identifies this error type.</param>
|
||||||
|
/// <param name="Description">The human-readable description of the error.</param>
|
||||||
|
public record UnexpectedError(string Code, string Description) : Error
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the type of this error.
|
||||||
|
/// </summary>
|
||||||
|
public override ErrorType Type => ErrorType.Unexpected;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the error code that uniquely identifies this error type.
|
||||||
|
/// </summary>
|
||||||
|
public override string Code { get; } = Code;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the human-readable description of the error.
|
||||||
|
/// </summary>
|
||||||
|
public override string Description { get; } = Description;
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a successful update operation result.
|
||||||
|
/// </summary>
|
||||||
|
public readonly record struct Updated;
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#region ========================================================================= USING =====================================================================================
|
||||||
|
using Lumina.Presentation.Web.Common.Enums.Primitives;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
namespace Lumina.Presentation.Web.Common.Primitives;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Error representing that the input or data failed validation rules.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Code">The error code that uniquely identifies this error type.</param>
|
||||||
|
/// <param name="Description">The human-readable description of the error.</param>
|
||||||
|
public record ValidationError(string Code, string Description) : Error
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the type of this error.
|
||||||
|
/// </summary>
|
||||||
|
public override ErrorType Type => ErrorType.Validation;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the error code that uniquely identifies this error type.
|
||||||
|
/// </summary>
|
||||||
|
public override string Code { get; } = Code;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the human-readable description of the error.
|
||||||
|
/// </summary>
|
||||||
|
public override string Description { get; } = Description;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user