Init
This commit is contained in:
59
Prefab/Endpoints/ApiProblemDetails.cs
Normal file
59
Prefab/Endpoints/ApiProblemDetails.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Prefab.Endpoints;
|
||||
|
||||
/// <summary>
|
||||
/// Shared helpers for building consistent HTTP problem responses across modules.
|
||||
/// </summary>
|
||||
public static class ApiProblemDetails
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a 404 <see cref="ProblemDetails"/> payload with standard metadata and resource identifiers.
|
||||
/// </summary>
|
||||
/// <param name="resource">Logical name of the missing resource (for example, "Product").</param>
|
||||
/// <param name="identifier">Identifier value supplied by the caller.</param>
|
||||
/// <param name="detail">Human-readable explanation for logging or the client.</param>
|
||||
public static ProblemDetails NotFound(string resource, string identifier, string detail) =>
|
||||
new()
|
||||
{
|
||||
Title = "Resource not found.",
|
||||
Detail = detail,
|
||||
Type = "https://prefab.dev/problems/not-found",
|
||||
Status = StatusCodes.Status404NotFound,
|
||||
Extensions =
|
||||
{
|
||||
["resource"] = resource,
|
||||
["identifier"] = identifier
|
||||
}
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Creates a 409 <see cref="ProblemDetails"/> payload used when a request cannot be completed due to a resource conflict.
|
||||
/// </summary>
|
||||
/// <param name="detail">Human-readable explanation of the conflict.</param>
|
||||
/// <param name="resource">Logical name of the resource involved, when known.</param>
|
||||
/// <param name="identifier">Identifier value supplied by the caller, when known.</param>
|
||||
public static ProblemDetails Conflict(string detail, string? resource = null, string? identifier = null)
|
||||
{
|
||||
var problem = new ProblemDetails
|
||||
{
|
||||
Title = "Request conflict.",
|
||||
Detail = detail,
|
||||
Type = "https://prefab.dev/problems/conflict",
|
||||
Status = StatusCodes.Status409Conflict
|
||||
};
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(resource))
|
||||
{
|
||||
problem.Extensions["resource"] = resource;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(identifier))
|
||||
{
|
||||
problem.Extensions["identifier"] = identifier;
|
||||
}
|
||||
|
||||
return problem;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user