lab2
Application / Lint (pull_request) Canceled after 0s
Application / Unit tests (pull_request) Canceled after 0s
Application / Docker build (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
Vladislav Kunetskiy
2026-10-06 00:19:15 +03:00
parent 51b236a69f
commit 5ea57db639
119 changed files with 9935 additions and 2138 deletions
+11 -3
View File
@@ -10,7 +10,7 @@ import (
"github.com/ogen-go/ogen/ogenerrors"
"mtusi-messenger/internal/domain"
"mtusi-messenger/internal/transport/rest/oas"
"mtusi-messenger/internal/transport/rest/internal/oas"
)
var fallbackErrorBody = []byte(`{"error":{"code":"internal_error","message":"Internal error","request_id":""}}`)
@@ -33,11 +33,12 @@ func statusForError(err error) int {
}
switch {
case errors.Is(err, ErrUnauthenticated):
case errors.Is(err, ErrUnauthenticated), errors.Is(err, domain.ErrUnauthenticated):
return http.StatusUnauthorized
case errors.Is(err, domain.ErrNotFound):
return http.StatusNotFound
case errors.Is(err, domain.ErrUsernameTaken):
case errors.Is(err, domain.ErrUsernameTaken),
errors.Is(err, domain.ErrConflict):
return http.StatusConflict
case errors.Is(err, domain.ErrValidation),
errors.Is(err, domain.ErrUnknownMember),
@@ -85,6 +86,13 @@ func writeError(ctx context.Context, log *slog.Logger, w http.ResponseWriter, r
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set(requestIDHeader, requestID)
if status == http.StatusUnauthorized {
challenge := `Bearer realm="messenger"`
if r.URL.Path == "/api/v1/auth/sessions" {
challenge = `Basic realm="messenger", charset="UTF-8"`
}
w.Header().Set("WWW-Authenticate", challenge)
}
w.WriteHeader(status)
if _, err := w.Write(payload); err != nil && log != nil {