示例#1
0
func HandleError(c context.Context, w http.ResponseWriter, e error) {
	c.Errorf(e.Error())
	handleStatus(c, w, e, http.StatusBadRequest)
	if Notifier != nil {
		Notifier.Notify(c, e, c.HTTPRequest(), nil)
	}
}
示例#2
0
func HandleUnauthorized(c context.Context, w http.ResponseWriter, e error) {
	w.WriteHeader(http.StatusUnauthorized)

	// Error can be nil.
	errStr := "You are not authorized to access this page."
	if e != nil {
		errStr = e.Error()
		c.Errorf("HandleUnauthorized: %v", errStr)
	}

	data := &errResult{
		Err: errStr,
		C:   c,
	}

	switch w.Header().Get("Content-Type") {
	case "application/json":
		if err := json.NewEncoder(w).Encode(data); err != nil {
			c.Errorf("json.Encode failed: %v", err)
			return
		}
	case "text/xml":
		w.Write([]byte(xml.Header))
		if err := xml.NewEncoder(w).Encode(data); err != nil {
			c.Errorf("xml.Encode failed: %v", err)
			return
		}
	default:
		HandleTemplate(c, w, UnauthorizedTpl, data)
	}
}
示例#3
0
func HandleNotFound(c context.Context, w http.ResponseWriter, e error) {
	w.WriteHeader(http.StatusNotFound)

	// Error can be nil.
	var errStr string
	if e != nil {
		errStr = e.Error()
		c.Infof("HandleNotFound: %v", errStr)
	}

	data := &errResult{
		Err: errStr,
		C:   c,
	}
	switch w.Header().Get("Content-Type") {
	case "application/json":
		if err := json.NewEncoder(w).Encode(data); err != nil {
			c.Errorf("json.Encode failed: %v", err)
			return
		}
	case "text/xml":
		w.Write([]byte(xml.Header))
		if err := xml.NewEncoder(w).Encode(data); err != nil {
			c.Errorf("xml.Encode failed: %v", err)
			return
		}
	default:
		HandleTemplate(c, w, NotFoundTpl, data)
	}
}
示例#4
0
func handleStatus(c context.Context, w http.ResponseWriter, e error, status int) {
	w.WriteHeader(status)

	data := &errResult{
		C:   c,
		Err: e.Error(),
	}
	switch w.Header().Get("Content-Type") {
	case "application/json":
		if err := json.NewEncoder(w).Encode(data); err != nil {
			c.Errorf("json.Encode failed: %v", err)
			return
		}
	case "text/xml":
		w.Write([]byte(xml.Header))
		if err := xml.NewEncoder(w).Encode(data); err != nil {
			c.Errorf("xml.Encode failed: %v", err)
			return
		}
	default:
		if err := ErrorTpl.Execute(w, data); err != nil {
			c.Errorf("errLayout.Execute failed: %v", err)
			return
		}
	}
}
示例#5
0
func HandleResult(c context.Context, w http.ResponseWriter, result interface{}) {
	switch ct := w.Header().Get("Content-Type"); ct {
	case "application/json":
		if err := json.NewEncoder(w).Encode(result); err != nil {
			c.Errorf("json.Encode failed: %v", err)
			return
		}
	case "text/xml":
		w.Write([]byte(xml.Header))
		if err := xml.NewEncoder(w).Encode(result); err != nil {
			c.Errorf("xml.Encode failed: %v", err)
			return
		}
	default:
		c.Errorf("httphelper: unsupported mime type %v", ct)
	}
}
示例#6
0
func HandleWarning(c context.Context, w http.ResponseWriter, e error) {
	c.Warningf(e.Error())
	handleStatus(c, w, e, http.StatusBadRequest)
}
示例#7
0
func HandleStatus(c context.Context, w http.ResponseWriter, e error, status int) {
	c.Warningf(e.Error())
	handleStatus(c, w, e, status)
}