package handler import ( "encoding/json" "net/http" ) type APIResponse struct { // Indicates if the request was successful Success bool `json:"success"` // The response data Data any `json:"data,omitempty"` // List of errors if any occurred Errors []APIError `json:"errors,omitempty"` // Optional message Message string `json:"message,omitempty"` } type APIError struct { // Error code Code int `json:"code"` // Human-readable error message Message string `json:"message"` // Additional error details Details string `json:"details,omitempty"` } const content_type = "Content-Type" const application_json = "application/json" func JSONSuccess(w http.ResponseWriter, data any, message string, statusCode int) { w.Header().Set(content_type, application_json) w.WriteHeader(statusCode) json.NewEncoder(w).Encode(APIResponse{ Success: true, Data: data, Message: message, }) } func JSONError(w http.ResponseWriter, message, details string, code, statusCode int) { w.Header().Set(content_type, application_json) w.WriteHeader(statusCode) json.NewEncoder(w).Encode(APIResponse{ Success: false, Errors: []APIError{ { Code: code, Message: message, Details: details, }, }, }) } func JSONErrors(w http.ResponseWriter, errors []APIError, statusCode int) { w.Header().Set(content_type, application_json) w.WriteHeader(statusCode) json.NewEncoder(w).Encode(APIResponse{ Success: false, Errors: errors, }) }