func HandleError(w http.ResponseWriter, err error) (code int) {
	if err == nil {
		return http.StatusOK
	}
	msg := err.Error()
	httpCode := http.StatusInternalServerError
	switch err := err.(type) {
	case *errors.HTTPError:
		httpCode = err.StatusCode
		code = err.StatusCode
	case *errors.Error:
		httpCode = http.StatusBadRequest
		code = err.ErrorCode
		msg = err.Message
	}
	response := NewErrorResponse(msg, code)
	jsonMessage, err := json.Marshal(response)
	if err != nil {
		log.Errorf("Failed to marshal JSON: %v", err)
	} else {
		msg = string(jsonMessage)
	}
	http.Error(w, msg, httpCode)
	return code
}
HandleError is the centralised error handling and reporting.
package api
import (
	"encoding/json"
	"io/ioutil"
	"net/http"
	"github.com/cloudflare/cfssl/errors"
	"github.com/cloudflare/cfssl/log"
)
type Handler interface {
	Handle(w http.ResponseWriter, r *http.Request) error
}
type HTTPHandler struct {
	Handler           
	Methods []string  
}
type HandlerFunc func(http.ResponseWriter, *http.Request) error
func (f HandlerFunc) Handle(w http.ResponseWriter, r *http.Request) error {
	w.Header().Set("Content-Type", "application/json")
	return f(w, r)
}