Выполнение 3 лабораторной работы

This commit is contained in:
2026-11-06 20:26:11 +03:00
parent 0ab3a341aa
commit 87f2a65448
105 changed files with 8359 additions and 25 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);
}
}