60 lines
2.1 KiB
C#
60 lines
2.1 KiB
C#
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;
|
|
}
|
|
}
|