lab1
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/ogen-go/ogen/ogenerrors"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
"mtusi-messenger/internal/transport/rest/oas"
|
||||
)
|
||||
|
||||
var fallbackErrorBody = []byte(`{"error":{"code":"internal_error","message":"Internal error","request_id":""}}`)
|
||||
|
||||
func newErrorHandler(log *slog.Logger) oas.ErrorHandler {
|
||||
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, err error) {
|
||||
status := statusForError(err)
|
||||
|
||||
if preMiddlewareError(err) {
|
||||
logRejected(ctx, log, r, status, err)
|
||||
}
|
||||
|
||||
writeError(ctx, log, w, r, status)
|
||||
}
|
||||
}
|
||||
|
||||
func statusForError(err error) int {
|
||||
if status := ogenerrors.ErrorCode(err); status != http.StatusInternalServerError {
|
||||
return status
|
||||
}
|
||||
|
||||
switch {
|
||||
case errors.Is(err, ErrUnauthenticated):
|
||||
return http.StatusUnauthorized
|
||||
case errors.Is(err, domain.ErrNotFound):
|
||||
return http.StatusNotFound
|
||||
case errors.Is(err, domain.ErrUsernameTaken):
|
||||
return http.StatusConflict
|
||||
case errors.Is(err, domain.ErrValidation),
|
||||
errors.Is(err, domain.ErrUnknownMember),
|
||||
errors.Is(err, domain.ErrInvalidCursor):
|
||||
return http.StatusBadRequest
|
||||
default:
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
}
|
||||
|
||||
func newNotFoundHandler(log *slog.Logger) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
logRejected(r.Context(), log, r, http.StatusNotFound, nil)
|
||||
writeError(r.Context(), log, w, r, http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
func newMethodNotAllowedHandler(log *slog.Logger) func(http.ResponseWriter, *http.Request, string) {
|
||||
return func(w http.ResponseWriter, r *http.Request, allowed string) {
|
||||
w.Header().Set("Allow", allowed)
|
||||
logRejected(r.Context(), log, r, http.StatusMethodNotAllowed, nil)
|
||||
writeError(r.Context(), log, w, r, http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func writeError(ctx context.Context, log *slog.Logger, w http.ResponseWriter, r *http.Request, status int) {
|
||||
requestID := requestIDFromContext(ctx)
|
||||
if requestID == "" {
|
||||
requestID = r.Header.Get(requestIDHeader)
|
||||
}
|
||||
|
||||
body := oas.Error{
|
||||
Error: oas.ErrorError{
|
||||
Code: codeForStatus(status),
|
||||
Message: http.StatusText(status),
|
||||
RequestID: requestID,
|
||||
},
|
||||
}
|
||||
|
||||
payload, marshalErr := json.Marshal(body)
|
||||
if marshalErr != nil {
|
||||
status = http.StatusInternalServerError
|
||||
payload = fallbackErrorBody
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Header().Set(requestIDHeader, requestID)
|
||||
w.WriteHeader(status)
|
||||
|
||||
if _, err := w.Write(payload); err != nil && log != nil {
|
||||
log.WarnContext(ctx, "response write failed", slog.String("error", err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
func codeForStatus(status int) oas.ErrorErrorCode {
|
||||
switch status {
|
||||
case http.StatusBadRequest, http.StatusMethodNotAllowed:
|
||||
return oas.ErrorErrorCodeInvalidRequest
|
||||
case http.StatusUnauthorized:
|
||||
return oas.ErrorErrorCodeUnauthorized
|
||||
case http.StatusNotFound:
|
||||
return oas.ErrorErrorCodeNotFound
|
||||
case http.StatusConflict:
|
||||
return oas.ErrorErrorCodeConflict
|
||||
case http.StatusRequestEntityTooLarge:
|
||||
return oas.ErrorErrorCodePayloadTooLarge
|
||||
case http.StatusUnsupportedMediaType:
|
||||
return oas.ErrorErrorCodeUnsupportedMediaType
|
||||
case http.StatusTooManyRequests:
|
||||
return oas.ErrorErrorCodeRateLimited
|
||||
case http.StatusServiceUnavailable:
|
||||
return oas.ErrorErrorCodeUnavailable
|
||||
default:
|
||||
return oas.ErrorErrorCodeInternalError
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user