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) } }
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) } }
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) } }
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 } } }
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) } }
func HandleWarning(c context.Context, w http.ResponseWriter, e error) { c.Warningf(e.Error()) handleStatus(c, w, e, http.StatusBadRequest) }
func HandleStatus(c context.Context, w http.ResponseWriter, e error, status int) { c.Warningf(e.Error()) handleStatus(c, w, e, status) }