package main import ( "encoding/json" "net/http" ) const authProxyKeyIDHeader = "Auth-Proxy-Key-ID" type apiError struct { Status int Code string Description string } func (e *apiError) Error() string { return e.Description } func invalidRequestError(description string) *apiError { return &apiError{ Status: http.StatusBadRequest, Code: "invalid_request", Description: description, } } func upstreamRequestError(description string) *apiError { return &apiError{ Status: http.StatusBadGateway, Code: "server_error", Description: description, } } func writeAPIError(w http.ResponseWriter, err error) { if apiErr, ok := err.(*apiError); ok { writeJSONError(w, apiErr.Status, apiErr.Code, apiErr.Description) return } writeJSONError(w, http.StatusInternalServerError, "server_error", "internal server error") } func writeJSONError(w http.ResponseWriter, status int, code string, description string) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) _ = json.NewEncoder(w).Encode(map[string]string{ "error": code, "error_description": description, }) }