refactor(httpx): drop duplicate WriteError, add WriteInternal

This commit is contained in:
kk10
2026-10-05 11:41:34 +03:00
parent 5d6f5a397f
commit b94e7813ee
6 changed files with 19 additions and 77 deletions
+3 -21
View File
@@ -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
}
-27
View File
@@ -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,
},
})
}
+2 -1
View File
@@ -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,
+2 -14
View File
@@ -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
}
+2 -14
View File
@@ -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
}
+10
View File
@@ -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,
)
}