(READ ONLY) Margin is an open annotation layer for the internet. Powered by the AT Protocol.
Something went wrong. Try again.
Go
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859package api
import ( "encoding/json" "net/http")
type APIError struct { Error string `json:"error"` Code string `json:"code,omitempty"` Details string `json:"details,omitempty"`}
func WriteJSONError(w http.ResponseWriter, statusCode int, message string) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(statusCode) json.NewEncoder(w).Encode(APIError{Error: message})}
func WriteJSONErrorWithCode(w http.ResponseWriter, statusCode int, message, code string) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(statusCode) json.NewEncoder(w).Encode(APIError{Error: message, Code: code})}
func WriteJSON(w http.ResponseWriter, statusCode int, data interface{}) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(statusCode) json.NewEncoder(w).Encode(data)}
func WriteSuccess(w http.ResponseWriter, data interface{}) { WriteJSON(w, http.StatusOK, data)}
func WriteBadRequest(w http.ResponseWriter, message string) { WriteJSONError(w, http.StatusBadRequest, message)}
func WriteUnauthorized(w http.ResponseWriter, message string) { WriteJSONError(w, http.StatusUnauthorized, message)}
func WriteForbidden(w http.ResponseWriter, message string) { WriteJSONError(w, http.StatusForbidden, message)}
func WriteNotFound(w http.ResponseWriter, message string) { WriteJSONError(w, http.StatusNotFound, message)}
func WriteConflict(w http.ResponseWriter, message string) { WriteJSONError(w, http.StatusConflict, message)}
func WriteInternalError(w http.ResponseWriter, message string) { WriteJSONError(w, http.StatusInternalServerError, message)}