Выполнениие лабораторной работы на 5, трек 2.
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user