29 lines
771 B
Go
29 lines
771 B
Go
package handlers
|
||
|
||
import (
|
||
"encoding/json"
|
||
"net/http"
|
||
)
|
||
|
||
// ErrorResponse представляет ответ с ошибкой
|
||
type ErrorResponse struct {
|
||
Error string `json:"error"`
|
||
}
|
||
|
||
// respondJSON отправляет JSON ответ
|
||
func respondJSON(w http.ResponseWriter, statusCode int, data interface{}) {
|
||
w.Header().Set("Content-Type", "application/json")
|
||
w.WriteHeader(statusCode)
|
||
|
||
if data != nil {
|
||
if err := json.NewEncoder(w).Encode(data); err != nil {
|
||
http.Error(w, "failed to encode response", http.StatusInternalServerError)
|
||
}
|
||
}
|
||
}
|
||
|
||
// respondError отправляет ошибку в формате JSON
|
||
func respondError(w http.ResponseWriter, statusCode int, message string) {
|
||
respondJSON(w, statusCode, ErrorResponse{Error: message})
|
||
}
|