forked from mtusi2026/mtusi2026lab2
35 lines
700 B
Go
35 lines
700 B
Go
package httpx
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"mtusi2026-messenger/internal/dto"
|
|
)
|
|
|
|
func WriteJSON(w http.ResponseWriter, status int, body any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
_ = json.NewEncoder(w).Encode(body)
|
|
}
|
|
|
|
func WriteError(w http.ResponseWriter, status int, code dto.ErrorCode, message, requestID string) {
|
|
WriteJSON(w, status, dto.ErrorResponse{
|
|
Error: dto.Error{
|
|
Code: code,
|
|
Message: message,
|
|
RequestID: requestID,
|
|
},
|
|
})
|
|
}
|
|
|
|
func WriteInternal(w http.ResponseWriter, requestID string) {
|
|
WriteError(
|
|
w,
|
|
http.StatusInternalServerError,
|
|
dto.ErrorCodeInternalError,
|
|
"internal server error",
|
|
requestID,
|
|
)
|
|
}
|