From b94e7813ee70445977a9f383aa30e20592a3d649 Mon Sep 17 00:00:00 2001 From: kk10 Date: Mon, 5 Oct 2026 11:41:34 +0300 Subject: [PATCH] refactor(httpx): drop duplicate WriteError, add WriteInternal --- internal/handler/chat.go | 24 +++--------------------- internal/handler/error.go | 27 --------------------------- internal/handler/health.go | 3 ++- internal/handler/message.go | 16 ++-------------- internal/handler/user.go | 16 ++-------------- internal/httpx/response.go | 10 ++++++++++ 6 files changed, 19 insertions(+), 77 deletions(-) delete mode 100644 internal/handler/error.go diff --git a/internal/handler/chat.go b/internal/handler/chat.go index 24a91788..8b6b23d5 100644 --- a/internal/handler/chat.go +++ b/internal/handler/chat.go @@ -71,13 +71,7 @@ func (h *ChatHandler) Create(w http.ResponseWriter, r *http.Request) { ) return case err != nil: - httpx.WriteError( - w, - http.StatusInternalServerError, - dto.ErrorCodeInternalError, - "internal server error", - requestID, - ) + httpx.WriteInternal(w, requestID) return } @@ -112,13 +106,7 @@ func (h *ChatHandler) Get(w http.ResponseWriter, r *http.Request) { ) return case err != nil: - httpx.WriteError( - w, - http.StatusInternalServerError, - dto.ErrorCodeInternalError, - "internal server error", - requestID, - ) + httpx.WriteInternal(w, requestID) return } @@ -146,13 +134,7 @@ func (h *ChatHandler) List(w http.ResponseWriter, r *http.Request) { ) return case err != nil: - httpx.WriteError( - w, - http.StatusInternalServerError, - dto.ErrorCodeInternalError, - "internal server error", - requestID, - ) + httpx.WriteInternal(w, requestID) return } diff --git a/internal/handler/error.go b/internal/handler/error.go deleted file mode 100644 index 34bb0bb0..00000000 --- a/internal/handler/error.go +++ /dev/null @@ -1,27 +0,0 @@ -package handler - -import ( - "encoding/json" - "net/http" - - "mtusi2026-messenger/internal/dto" -) - -func WriteError( - w http.ResponseWriter, - status int, - code dto.ErrorCode, - message string, - requestID string, -) { - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(status) - - _ = json.NewEncoder(w).Encode(dto.ErrorResponse{ - Error: dto.Error{ - Code: code, - Message: message, - RequestID: requestID, - }, - }) -} diff --git a/internal/handler/health.go b/internal/handler/health.go index 00c6cc25..ee8a2b30 100644 --- a/internal/handler/health.go +++ b/internal/handler/health.go @@ -8,6 +8,7 @@ import ( "time" "mtusi2026-messenger/internal/dto" + "mtusi2026-messenger/internal/httpx" ) const readyPingTimeout = 2 * time.Second @@ -40,7 +41,7 @@ func (h *HealthHandler) Ready(w http.ResponseWriter, r *http.Request) { if err := h.db.Ping(ctx); err != nil { h.log.ErrorContext(r.Context(), "readiness check failed", "error", err) - WriteError( + httpx.WriteError( w, http.StatusServiceUnavailable, dto.ErrorCodeUnavailable, diff --git a/internal/handler/message.go b/internal/handler/message.go index 191590e1..85814865 100644 --- a/internal/handler/message.go +++ b/internal/handler/message.go @@ -65,13 +65,7 @@ func (h *MessageHandler) Create(w http.ResponseWriter, r *http.Request) { ) return case err != nil: - httpx.WriteError( - w, - http.StatusInternalServerError, - dto.ErrorCodeInternalError, - "internal server error", - requestID, - ) + httpx.WriteInternal(w, requestID) return } @@ -120,13 +114,7 @@ func (h *MessageHandler) List(w http.ResponseWriter, r *http.Request) { ) return case err != nil: - httpx.WriteError( - w, - http.StatusInternalServerError, - dto.ErrorCodeInternalError, - "internal server error", - requestID, - ) + httpx.WriteInternal(w, requestID) return } diff --git a/internal/handler/user.go b/internal/handler/user.go index 5356546d..40d6086b 100644 --- a/internal/handler/user.go +++ b/internal/handler/user.go @@ -54,13 +54,7 @@ func (h *UserHandler) Create(w http.ResponseWriter, r *http.Request) { return } if err != nil { - httpx.WriteError( - w, - http.StatusInternalServerError, - dto.ErrorCodeInternalError, - "internal server error", - requestID, - ) + httpx.WriteInternal(w, requestID) return } @@ -88,13 +82,7 @@ func (h *UserHandler) Me(w http.ResponseWriter, r *http.Request) { return } if err != nil { - httpx.WriteError( - w, - http.StatusInternalServerError, - dto.ErrorCodeInternalError, - "internal server error", - requestID, - ) + httpx.WriteInternal(w, requestID) return } diff --git a/internal/httpx/response.go b/internal/httpx/response.go index 9a12f0ba..51e240b5 100644 --- a/internal/httpx/response.go +++ b/internal/httpx/response.go @@ -22,3 +22,13 @@ func WriteError(w http.ResponseWriter, status int, code dto.ErrorCode, message, }, }) } + +func WriteInternal(w http.ResponseWriter, requestID string) { + WriteError( + w, + http.StatusInternalServerError, + dto.ErrorCodeInternalError, + "internal server error", + requestID, + ) +}