libhttp/return_json_error.go

21 lines
427 B
Go

package libhttp
import (
"encoding/json"
"fmt"
"net/http"
)
func ReturnJSONError(w http.ResponseWriter, status int, errStr string, vars ...interface{}) error {
newErr := map[string]interface{}{
"status": status,
"text": http.StatusText(status),
"detail": fmt.Sprintf(errStr, vars...),
}
w.Header().Set(HeaderContentType, MimeApplicationJSON)
w.WriteHeader(status)
return json.NewEncoder(w).Encode(newErr)
}