package http_router import ( "errors" "fmt" "git-molva.ru/Molva/molva-backend/services/api_gateway/internal/constants" "log/slog" "net/http" ) // =============== CUSTOM LOGO ERRORS =============== var ( ErrEmptyFile = errors.New("file is empty") ErrMultipartParse = errors.New("failed to parse multipart form") ErrFileNotFound = errors.New("failed to get file from form") ErrTempFileCreate = errors.New("failed to create temporary file") ErrFileOperation = errors.New("file operation failed") ) // InvalidImageTypeError представляет ошибку неверного типа изображения type InvalidImageTypeError struct { ContentType string } func (e *InvalidImageTypeError) Error() string { return fmt.Sprintf("invalid image type %s: only png, jpg, and webp are allowed", e.ContentType) } // FileTooLargeError представляет ошибку превышения размера файла type FileTooLargeError struct { Size int64 MaxSize int64 } func (e *FileTooLargeError) Error() string { return fmt.Sprintf("file too large: %d bytes, max %d bytes allowed", e.Size, e.MaxSize) } // =============== ERROR HANDLERS =============== // handleAccessError обрабатывает ошибки прав или токенов func (h *handler) handleAccessError(w http.ResponseWriter, err error, handlerName string) { if errors.Is(err, constants.ErrUnauthorized) { http.Error(w, constants.ErrUnauthorized.Error(), http.StatusUnauthorized) } else if errors.Is(err, constants.ErrForbidden) { http.Error(w, constants.ErrForbidden.Error(), http.StatusForbidden) } else { http.Error(w, constants.ErrInternalServerError.Error(), http.StatusInternalServerError) h.logger.Error("some permission or authorization error", slog.String("error", err.Error()), slog.String("handler", handlerName)) } } // handleUploadError обрабатывает ошибки логотипов func (h *handler) handleUploadError(w http.ResponseWriter, err error, handlerName string) { var invalidTypeErr *InvalidImageTypeError var fileTooLargeErr *FileTooLargeError // Проверяем типизированные ошибки if errors.As(err, &invalidTypeErr) || errors.As(err, &fileTooLargeErr) || errors.Is(err, ErrEmptyFile) || errors.Is(err, ErrMultipartParse) || errors.Is(err, ErrFileNotFound) { http.Error(w, err.Error(), http.StatusBadRequest) h.logger.Error("client error uploading logo", slog.String("error", err.Error()), slog.String("handler", handlerName)) } else { http.Error(w, constants.ErrInternalServerError.Error(), http.StatusInternalServerError) h.logger.Error("error processing logo upload", slog.String("error", err.Error()), slog.String("handler", handlerName)) } }