34 lines
674 B
Go
34 lines
674 B
Go
// Path: Backend/internal/httpx/response.go
|
|
|
|
package httpx
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
type ErrorResponse struct {
|
|
Error string `json:"error"`
|
|
Message string `json:"message"`
|
|
RequestID string `json:"requestId,omitempty"`
|
|
}
|
|
|
|
func WriteJSON(w http.ResponseWriter, status int, payload any) {
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.WriteHeader(status)
|
|
|
|
if payload == nil {
|
|
return
|
|
}
|
|
|
|
_ = json.NewEncoder(w).Encode(payload)
|
|
}
|
|
|
|
func WriteError(w http.ResponseWriter, status int, requestID, code, message string) {
|
|
WriteJSON(w, status, ErrorResponse{
|
|
Error: code,
|
|
Message: message,
|
|
RequestID: requestID,
|
|
})
|
|
}
|