// MWError is the main middleware. When an error is returned, it send
// the data to the client if the header hasn't been sent yet, otherwise, log them.
357406
func (sm *ServeMux) MWError(handler HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		ww := NewResponseWriter(w)
		if err := handler(ww, req); err != nil {
			sm.HandleError(ww, req, err)
			return
		}
	}
}
352741
func encodeError(_ context.Context, err error, w http.ResponseWriter) {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	switch err {
	case cargo.ErrUnknown:
		w.WriteHeader(http.StatusNotFound)
	case ErrInvalidArgument:
		w.WriteHeader(http.StatusBadRequest)
	default:
		w.WriteHeader(http.StatusInternalServerError)
	}
	json.NewEncoder(w).Encode(map[string]interface{}{
		"error": err.Error(),
	})
}
1116915
func (api *API) RespondError(err error) error {
	var response errorResponse
	response.Error = err.Error()

	return sendSerializedJSONMessage(response, api.Writer)
}
777336
func writeError(c *routing.Context, err error) {
	if httpError, ok := err.(routing.HTTPError); ok {
		c.Response.WriteHeader(httpError.StatusCode())
	} else {
		c.Response.WriteHeader(http.StatusInternalServerError)
	}
	c.Write(err)
}
4951
func replyError(c context.Context, rw http.ResponseWriter, err error) {
	logging.Errorf(c, "Error while processing PubSub notification - %s", err)
	if transient.Tag.In(err) {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
	} else {
		http.Error(rw, err.Error(), http.StatusBadRequest)
	}
}
111171
func writeError(w http.ResponseWriter, status int, err error) {
	w.WriteHeader(status)
	enc := json.NewEncoder(w)
	resp := baseapitypes.Error{
		Type: baseapitypes.InternalServerError,
		Err:  []string{err.Error()},
	}

	enc.Encode(resp)
}
43842
func ServerError(w http.ResponseWriter, r *http.Request, err error) {
	handlerMu.RLock()
	f, ok := handlerMap[http.StatusInternalServerError]
	handlerMu.RUnlock()
	if ok {
		r = ctxSetErr(r, err)
		f.ServeHTTP(w, r)
	} else {
		defaultServerError(w, r, err)
	}
}
1222137
func Error(w http.ResponseWriter, code int) {
	http.Error(w, http.StatusText(code), code)
}
288009
func httpError(w http.ResponseWriter, body string, code int) {
	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
	w.Header().Set("Content-Length", strconv.Itoa(len(body)))
	w.WriteHeader(code)
	w.Write([]byte(body))
}
94535
func (r *RequestError) Error() error {
	if r.Message != "" {
		return errors.New(r.Message)
	}

	return nil
}
1139540
func (c *Client) RequestError(level string, r *http.Request, err error) {
	c.RequestErrorWithExtras(level, r, err, noExtras)
}
847316
func EncodeError(ctx context.Context, err error, w http.ResponseWriter) {
	if err == nil {
		return
	}

	if pe, ok := err.(*platform.Error); ok {
		code := platform.ErrorCode(pe)
		httpCode, ok := statusCodePlatformError[code]
		if !ok {
			httpCode = http.StatusBadRequest
		}
		w.Header().Set(PlatformErrorCodeHeader, code)
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.WriteHeader(httpCode)
		b, _ := json.Marshal(&platform.Error{
			Code: code,
			Op:   platform.ErrorOp(pe),
			Msg:  platform.ErrorMessage(pe),
			Err:  pe.Err,
		})
		_, _ = w.Write(b)
		return
	}
	e, ok := err.(kerrors.Error)
	if !ok {
		e = kerrors.Error{
			Reference: kerrors.InternalError,
			Err:       err.Error(),
		}
	}
	encodeKError(e, w)
}
1181939
func (sm *ServeMux) HandleError(w ResponseWriter, req *http.Request, err error) {
	if code := w.Code(); code != 0 {
		sm.log.Printf("HTTP Error (header already sent): %s (%d)", err, code)
		return
	}
	if sm.errorContentType != "" {
		w.Header().Set("Content-Type", sm.errorContentType)
	}
	if err == nil {
		return
	}
	if e1, ok := err.(*Error); ok && e1.Code() != 0 {
		w.WriteHeader(e1.Code())
	} else {
		w.WriteHeader(http.StatusInternalServerError)
	}

	sm.sendError(w, req, err)
}
449709
func Error(w http.ResponseWriter, code int, fmtStr string, vars ...interface{}) {
	http.Error(w, fmt.Sprintf(fmtStr, vars...), code)
}
700214
func (c *Client) FormatError(e ErrEntry) string {
	return fmt.Sprintf("%d %s [%s] %s request-%d retry-%d error: %s\n",
		e.Time.Unix(), e.Method, e.Verb, e.URL, e.Request, e.Retry, e.Err)
}
319770
func (m *Module) HandleError(rw http.ResponseWriter, req *http.Request, err error) int {
	switch err {
	case ErrNotFound:
		err = NewRequestError(req, http.StatusNotFound, "not found: "+req.URL.String())
	case ErrNotAuthorized:
		err = NewRequestError(req, http.StatusUnauthorized, "not authenticated")
	case ErrForbidden:
		err = NewRequestError(req, http.StatusForbidden, "forbidden")
	case ErrInternal:
		err = NewRequestError(req, http.StatusInternalServerError, "internal server error")
	}

	switch e := err.(type) {
	// handle wrapped error created by errors.Wrap
	case *errors.Error:
		// log (warn) the stack trace and then recurse on the original err, which
		// is either a known *Error or unknown error
		m.Logger.Warning(errString(err))
		return m.HandleError(rw, req, e.Err)

	case *Error:
		status := int(e.err.GetCode())
		if status >= 500 {
			m.Logger.Error(e) // log the original error (with request)
		} else {
			m.Logger.Warning(err)
		}
		if e.silent {
			rw.WriteHeader(status)
		} else if e.redirect {
			m.ErrorPage(rw, req, status)
		} else {
			Proto(rw, int(e.err.GetCode()), &api.ErrorResponse{Errors: []*api.Error{&e.err}})
		}
		return status

	default:
		// log error with request
		m.Logger.Error(&Error{
			request: req,
			err:     newAPIError(500, errString(e)),
		})
		ae := newAPIError(http.StatusInternalServerError, "internal server error")
		Proto(rw, http.StatusInternalServerError, &api.ErrorResponse{Errors: []*api.Error{&ae}})
		return http.StatusInternalServerError
	}
}
1181094
func Error(v ...interface{}) {
	if Severity >= LevelError {
		std.Output(5, fmt.Sprint(v...), LevelError)
	}
}