lab 3 done
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
"gitflic.ru/akselbant/mtusi2026lab1-fork/dto"
|
||||
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
|
||||
)
|
||||
|
||||
func (h *Chat) invalid(c echo.Context, message string) error {
|
||||
return h.writeError(c, http.StatusBadRequest, "invalid_request", message)
|
||||
}
|
||||
|
||||
func (h *Chat) jsonError(c echo.Context, err error) error {
|
||||
if errors.Is(err, errUnsupportedMediaType) {
|
||||
return h.writeError(c, http.StatusUnsupportedMediaType, "unsupported_media_type", "Content-Type должен быть application/json")
|
||||
}
|
||||
var tooLarge *http.MaxBytesError
|
||||
if errors.As(err, &tooLarge) {
|
||||
return h.writeError(c, http.StatusRequestEntityTooLarge, "payload_too_large", "Тело запроса слишком большое")
|
||||
}
|
||||
return h.invalid(c, err.Error())
|
||||
}
|
||||
|
||||
func (h *Chat) useCaseError(c echo.Context, err error) error {
|
||||
applicationError, ok := domain.AsApplicationError(err)
|
||||
if !ok {
|
||||
return err
|
||||
}
|
||||
status := http.StatusInternalServerError
|
||||
switch applicationError.Code {
|
||||
case domain.ErrorUnavailable:
|
||||
status = http.StatusServiceUnavailable
|
||||
case domain.ErrorInvalid:
|
||||
status = http.StatusBadRequest
|
||||
case domain.ErrorUnauthorized:
|
||||
status = http.StatusUnauthorized
|
||||
challenge := `Bearer realm="messenger"`
|
||||
if c.Path() == "/api/v1/auth/sessions" {
|
||||
challenge = `Basic realm="messenger", charset="UTF-8"`
|
||||
}
|
||||
c.Response().Header().Set("WWW-Authenticate", challenge)
|
||||
case domain.ErrorNotFound:
|
||||
status = http.StatusNotFound
|
||||
case domain.ErrorConflict:
|
||||
status = http.StatusConflict
|
||||
}
|
||||
return h.writeError(c, status, string(applicationError.Code), applicationError.Message)
|
||||
}
|
||||
|
||||
func (h *Chat) writeError(c echo.Context, status int, code, message string) error {
|
||||
return c.JSON(status, dto.ErrorResponse{Error: dto.ErrorDetails{
|
||||
Code: code,
|
||||
Message: message,
|
||||
RequestID: requestID(c),
|
||||
}})
|
||||
}
|
||||
|
||||
func (h *Chat) httpErrorHandler(err error, c echo.Context) {
|
||||
if c.Response().Committed {
|
||||
return
|
||||
}
|
||||
status := http.StatusInternalServerError
|
||||
code := "internal_error"
|
||||
message := "Внутренняя ошибка сервера"
|
||||
var httpError *echo.HTTPError
|
||||
if errors.As(err, &httpError) {
|
||||
status = httpError.Code
|
||||
switch status {
|
||||
case http.StatusNotFound:
|
||||
code, message = "not_found", "Объект не найден"
|
||||
case http.StatusMethodNotAllowed, http.StatusBadRequest:
|
||||
code, message = "invalid_request", "Некорректный запрос"
|
||||
}
|
||||
}
|
||||
_ = h.writeError(c, status, code, message)
|
||||
}
|
||||
|
||||
func requestID(c echo.Context) string {
|
||||
value, _ := c.Get(requestIDKey).(string)
|
||||
if value == "" {
|
||||
value = uuid.NewString()
|
||||
c.Set(requestIDKey, value)
|
||||
c.Response().Header().Set("X-Request-Id", value)
|
||||
}
|
||||
return value
|
||||
}
|
||||
Reference in New Issue
Block a user