/
theosov
/
Radiola
Обзор
Документация
Войти
/
theosov
/
Radiola
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
internal/api/response.go
31 строка
870 B
Theosov
P0
25 июл 2026, 01:31
25 июл 2026, 01:31
1f4159e
Код
Авторство
О чём код?
package api import ( "encoding/json" "net/http" ) // ErrorResponse is the standard error envelope for the API. type ErrorResponse struct { Error ErrorDetail `json:"error"` } type ErrorDetail struct { Code string `json:"code"` Message string `json:"message"` } // WriteError writes a JSON error response and returns the HTTP status code used. func WriteError(w http.ResponseWriter, code string, status int, message string) int { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) _ = json.NewEncoder(w).Encode(ErrorResponse{Error: ErrorDetail{Code: code, Message: message}}) return status } // WriteJSON writes a JSON response with the given status. func WriteJSON(w http.ResponseWriter, status int, payload any) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) _ = json.NewEncoder(w).Encode(payload) }