package rest import ( "encoding/json" "net/http" ) // ErrorResponse represents an API error response type ErrorResponse struct { Error ErrorDetail `json:"error"` } // ErrorDetail contains error details type ErrorDetail struct { Code string `json:"code"` Message string `json:"message"` Details string `json:"details,omitempty"` } // writeError writes an error response func writeError(w http.ResponseWriter, statusCode int, code, message string) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(statusCode) json.NewEncoder(w).Encode(ErrorResponse{ Error: ErrorDetail{ Code: code, Message: message, }, }) } // writeNotFound writes a 404 error response func writeNotFound(w http.ResponseWriter, resource string) { writeError(w, http.StatusNotFound, "NOT_FOUND", resource+" not found") } // writeInternalError writes a 500 error response func writeInternalError(w http.ResponseWriter, message string) { writeError(w, http.StatusInternalServerError, "INTERNAL_ERROR", message) } // writeUnauthorized writes a 401 error response func writeUnauthorized(w http.ResponseWriter) { writeError(w, http.StatusUnauthorized, "UNAUTHORIZED", "Authentication required") } // writeForbidden writes a 403 error response func writeForbidden(w http.ResponseWriter) { writeError(w, http.StatusForbidden, "FORBIDDEN", "Access denied") } // writeMethodNotAllowed writes a 405 error response (JSON) func writeMethodNotAllowed(w http.ResponseWriter) { writeError(w, http.StatusMethodNotAllowed, "METHOD_NOT_ALLOWED", "Method not allowed") }