forked from mtusi2026/mtusi2026lab6
84 lines
2.9 KiB
Go
84 lines
2.9 KiB
Go
package http
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/domain"
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/validate"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
const (
|
|
CodeInvalidRequest = "invalid_request"
|
|
CodeUnauthorized = "unauthorized"
|
|
CodeNotFound = "not_found"
|
|
CodeConflict = "conflict"
|
|
CodePayloadTooLarge = "payload_too_large"
|
|
CodeUnsupportedMedia = "unsupported_media_type"
|
|
CodeRateLimited = "rate_limited"
|
|
CodeUnavailable = "unavailable"
|
|
CodeInternalError = "internal_error"
|
|
)
|
|
|
|
type errorBody struct {
|
|
Error errorPayload `json:"error"`
|
|
}
|
|
|
|
type errorPayload struct {
|
|
Code string `json:"code"`
|
|
Message string `json:"message"`
|
|
RequestID string `json:"request_id"`
|
|
}
|
|
|
|
var (
|
|
ErrUnauthorized = errors.New("unauthorized")
|
|
ErrUnsupportedMedia = errors.New("unsupported media type")
|
|
)
|
|
|
|
func Classify(err error) (status int, code, message string) {
|
|
switch {
|
|
case errors.Is(err, domain.ErrIdempotencyConflict):
|
|
return http.StatusConflict, CodeConflict, "Idempotency-Key was already used with a different request"
|
|
case errors.Is(err, domain.ErrNotFound):
|
|
return http.StatusNotFound, CodeNotFound, "Object not found"
|
|
case errors.Is(err, domain.ErrConflict):
|
|
return http.StatusConflict, CodeConflict, "Resource already exists"
|
|
case errors.Is(err, domain.ErrInvalidArgument):
|
|
return http.StatusBadRequest, CodeInvalidRequest, "Invalid request arguments"
|
|
case errors.Is(err, domain.ErrUnavailable):
|
|
return http.StatusServiceUnavailable, CodeUnavailable, "Service temporarily unavailable"
|
|
case errors.Is(err, domain.ErrPayloadTooLarge):
|
|
return http.StatusRequestEntityTooLarge, CodePayloadTooLarge, "Request body too large"
|
|
case errors.Is(err, domain.ErrRateLimited):
|
|
return http.StatusTooManyRequests, CodeRateLimited, "Too many concurrent requests"
|
|
case errors.Is(err, ErrUnauthorized), errors.Is(err, domain.ErrUnauthorized):
|
|
return http.StatusUnauthorized, CodeUnauthorized, "Identification required"
|
|
case errors.Is(err, domain.ErrUnsupportedMedia):
|
|
return http.StatusUnsupportedMediaType, CodeUnsupportedMedia, "Expected a valid PNG or JPEG image"
|
|
case errors.Is(err, ErrUnsupportedMedia):
|
|
return http.StatusUnsupportedMediaType, CodeUnsupportedMedia, "Expected Content-Type: application/json"
|
|
}
|
|
|
|
var vErr *validate.Error
|
|
if errors.As(err, &vErr) {
|
|
return http.StatusBadRequest, CodeInvalidRequest, vErr.Error()
|
|
}
|
|
|
|
return http.StatusInternalServerError, CodeInternalError, "Internal server error"
|
|
}
|
|
|
|
func WriteError(c echo.Context, err error) error {
|
|
status, code, msg := Classify(err)
|
|
if status == http.StatusTooManyRequests {
|
|
c.Response().Header().Set("Retry-After", "1")
|
|
}
|
|
return c.JSON(status, errorBody{
|
|
Error: errorPayload{
|
|
Code: code,
|
|
Message: msg,
|
|
RequestID: c.Response().Header().Get(echo.HeaderXRequestID),
|
|
},
|
|
})
|
|
}
|