Выполнениие лабораторной работы на 5, трек 2.
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
2026-10-23 21:13:27 +03:00
parent 65003b6119
commit f511c95962
82 changed files with 4784 additions and 13 deletions
+37
View File
@@ -0,0 +1,37 @@
namespace Messenger.Api.Services;
public static class ApiErrors
{
public static int CodePoints(string value) => value.EnumerateRunes().Count();
// 401 с обязательным заголовком WWW-Authenticate (контракт: Basic для входа, Bearer для остального API).
public static IResult Unauthorized(HttpContext context, string challenge, string message)
{
context.Response.Headers.WWWAuthenticate = challenge;
return Error(context, 401, "unauthorized", message);
}
public static IResult Error(
HttpContext context,
int statusCode,
string code,
string message)
{
var requestId = context.Items["request_id"]?.ToString() ?? context.TraceIdentifier;
// UseExceptionHandler очищает заголовки ответа, поэтому выставляем X-Request-Id заново.
context.Response.Headers["X-Request-Id"] = requestId;
return Results.Json(
new
{
error = new
{
code,
message,
request_id = requestId
}
},
statusCode: statusCode);
}
}