Files
2026-06-22 14:31:01 +05:00

37 lines
879 B
Go

package httpx
import (
"encoding/json"
"errors"
"net/http"
"secunda-test/internal/service"
)
func WriteJSON(w http.ResponseWriter, status int, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(v)
}
func WriteError(w http.ResponseWriter, status int, message string) {
WriteJSON(w, status, map[string]string{"error": message})
}
func StatusFromError(err error) int {
switch {
case errors.Is(err, service.ErrUnauthorized):
return http.StatusUnauthorized
case errors.Is(err, service.ErrForbidden):
return http.StatusForbidden
case errors.Is(err, service.ErrNotFound):
return http.StatusNotFound
case errors.Is(err, service.ErrBadRequest):
return http.StatusBadRequest
case errors.Is(err, service.ErrConflict):
return http.StatusConflict
default:
return http.StatusInternalServerError
}
}