forked from mtusi2026/mtusi2026lab2
refactor(httpx): drop duplicate WriteError, add WriteInternal
This commit is contained in:
@@ -71,13 +71,7 @@ func (h *ChatHandler) Create(w http.ResponseWriter, r *http.Request) {
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
case err != nil:
|
case err != nil:
|
||||||
httpx.WriteError(
|
httpx.WriteInternal(w, requestID)
|
||||||
w,
|
|
||||||
http.StatusInternalServerError,
|
|
||||||
dto.ErrorCodeInternalError,
|
|
||||||
"internal server error",
|
|
||||||
requestID,
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,13 +106,7 @@ func (h *ChatHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
case err != nil:
|
case err != nil:
|
||||||
httpx.WriteError(
|
httpx.WriteInternal(w, requestID)
|
||||||
w,
|
|
||||||
http.StatusInternalServerError,
|
|
||||||
dto.ErrorCodeInternalError,
|
|
||||||
"internal server error",
|
|
||||||
requestID,
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,13 +134,7 @@ func (h *ChatHandler) List(w http.ResponseWriter, r *http.Request) {
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
case err != nil:
|
case err != nil:
|
||||||
httpx.WriteError(
|
httpx.WriteInternal(w, requestID)
|
||||||
w,
|
|
||||||
http.StatusInternalServerError,
|
|
||||||
dto.ErrorCodeInternalError,
|
|
||||||
"internal server error",
|
|
||||||
requestID,
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"mtusi2026-messenger/internal/dto"
|
"mtusi2026-messenger/internal/dto"
|
||||||
|
"mtusi2026-messenger/internal/httpx"
|
||||||
)
|
)
|
||||||
|
|
||||||
const readyPingTimeout = 2 * time.Second
|
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 {
|
if err := h.db.Ping(ctx); err != nil {
|
||||||
h.log.ErrorContext(r.Context(), "readiness check failed", "error", err)
|
h.log.ErrorContext(r.Context(), "readiness check failed", "error", err)
|
||||||
WriteError(
|
httpx.WriteError(
|
||||||
w,
|
w,
|
||||||
http.StatusServiceUnavailable,
|
http.StatusServiceUnavailable,
|
||||||
dto.ErrorCodeUnavailable,
|
dto.ErrorCodeUnavailable,
|
||||||
|
|||||||
@@ -65,13 +65,7 @@ func (h *MessageHandler) Create(w http.ResponseWriter, r *http.Request) {
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
case err != nil:
|
case err != nil:
|
||||||
httpx.WriteError(
|
httpx.WriteInternal(w, requestID)
|
||||||
w,
|
|
||||||
http.StatusInternalServerError,
|
|
||||||
dto.ErrorCodeInternalError,
|
|
||||||
"internal server error",
|
|
||||||
requestID,
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,13 +114,7 @@ func (h *MessageHandler) List(w http.ResponseWriter, r *http.Request) {
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
case err != nil:
|
case err != nil:
|
||||||
httpx.WriteError(
|
httpx.WriteInternal(w, requestID)
|
||||||
w,
|
|
||||||
http.StatusInternalServerError,
|
|
||||||
dto.ErrorCodeInternalError,
|
|
||||||
"internal server error",
|
|
||||||
requestID,
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -54,13 +54,7 @@ func (h *UserHandler) Create(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpx.WriteError(
|
httpx.WriteInternal(w, requestID)
|
||||||
w,
|
|
||||||
http.StatusInternalServerError,
|
|
||||||
dto.ErrorCodeInternalError,
|
|
||||||
"internal server error",
|
|
||||||
requestID,
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,13 +82,7 @@ func (h *UserHandler) Me(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpx.WriteError(
|
httpx.WriteInternal(w, requestID)
|
||||||
w,
|
|
||||||
http.StatusInternalServerError,
|
|
||||||
dto.ErrorCodeInternalError,
|
|
||||||
"internal server error",
|
|
||||||
requestID,
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user