// MakeErrorHandler makes an HTTP handler that decodes a Docker error and
// returns it in the response.
334424
func MakeErrorHandler(err error) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		statusCode := errdefs.GetHTTPErrorStatusCode(err)
		vars := mux.Vars(r)
		if apiVersionSupportsJSONErrors(vars["version"]) {
			response := &types.ErrorResponse{
				Message: err.Error(),
			}
			WriteJSON(w, statusCode, response)
		} else {
			http.Error(w, status.Convert(err).Message(), statusCode)
		}
	}
}
180272
func MakeError(err error) Error {
	if amqpErr, ok := err.(Error); ok {
		return amqpErr
	} else {
		return Error{InternalError, err.Error()}
	}
}
339685
func MakeHandler() *http.Handler {
	api := rest.NewApi()

	authMiddleware, err := CreateAuthMiddleware()
	if err != nil {
		panic(err)
	}

	api.Use(&rest.IfMiddleware{
		// Only authenticate non login or register requests
		Condition: func(request *rest.Request) bool {
			return (request.URL.Path != variables.APIPathLoginUserServer) && (request.URL.Path != variables.APIPathRegisterUserServer)
		},
		IfTrue: authMiddleware,
	})
	api.Use(rest.DefaultProdStack...)
	router, err := rest.MakeRouter(
		rest.Get("/:collection/:id", GetDoc),
		rest.Post("/:collection/:id", PostDoc),
	)
	if err != nil {
		log.Fatal(err)
	}
	api.SetApp(router)
	handler := api.MakeHandler()
	return &handler
}
1074257
func (s *Server) HTTPHandler() http.Handler {
	s.mu.Lock()
	defer s.mu.Unlock()
	return s.httpHandler
}
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
		}
	}
}
475128
func makeError(c ErrorCode, desc string, err error) Error {
	return Error{ErrorCode: c, Description: desc, Err: err}
}
362608
func (r *Router) handleError(c *Context, err error) {
	if httpError, ok := err.(HTTPError); ok {
		c.Error(httpError.Error(), httpError.StatusCode())
	} else {
		c.Error(err.Error(), http.StatusInternalServerError)
	}
}
495238
func MakeHandler(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {

		if attmpt := apiAttempt(w, r); !attmpt {
			fn(w, r)
		}
		context.Clear(r)

	}
}
1175270
func MakeHTTPHandler(s Service, logger log.Logger) http.Handler {
	r := mux.NewRouter()
	e := MakeServerEndpoints(s)
	options := []httptransport.ServerOption{
		httptransport.ServerErrorHandler(transport.NewLogErrorHandler(logger)),
		httptransport.ServerErrorEncoder(encodeError),
	}

	// POST    /profiles/                          adds another profile
	// GET     /profiles/:id                       retrieves the given profile by id
	// PUT     /profiles/:id                       post updated profile information about the profile
	// PATCH   /profiles/:id                       partial updated profile information
	// DELETE  /profiles/:id                       remove the given profile
	// GET     /profiles/:id/addresses/            retrieve addresses associated with the profile
	// GET     /profiles/:id/addresses/:addressID  retrieve a particular profile address
	// POST    /profiles/:id/addresses/            add a new address
	// DELETE  /profiles/:id/addresses/:addressID  remove an address

	r.Methods("POST").Path("/profiles/").Handler(httptransport.NewServer(
		e.PostProfileEndpoint,
		decodePostProfileRequest,
		encodeResponse,
		options...,
	))
	r.Methods("GET").Path("/profiles/{id}").Handler(httptransport.NewServer(
		e.GetProfileEndpoint,
		decodeGetProfileRequest,
		encodeResponse,
		options...,
	))
	r.Methods("PUT").Path("/profiles/{id}").Handler(httptransport.NewServer(
		e.PutProfileEndpoint,
		decodePutProfileRequest,
		encodeResponse,
		options...,
	))
	r.Methods("PATCH").Path("/profiles/{id}").Handler(httptransport.NewServer(
		e.PatchProfileEndpoint,
		decodePatchProfileRequest,
		encodeResponse,
		options...,
	))
	r.Methods("DELETE").Path("/profiles/{id}").Handler(httptransport.NewServer(
		e.DeleteProfileEndpoint,
		decodeDeleteProfileRequest,
		encodeResponse,
		options...,
	))
	r.Methods("GET").Path("/profiles/{id}/addresses/").Handler(httptransport.NewServer(
		e.GetAddressesEndpoint,
		decodeGetAddressesRequest,
		encodeResponse,
		options...,
	))
	r.Methods("GET").Path("/profiles/{id}/addresses/{addressID}").Handler(httptransport.NewServer(
		e.GetAddressEndpoint,
		decodeGetAddressRequest,
		encodeResponse,
		options...,
	))
	r.Methods("POST").Path("/profiles/{id}/addresses/").Handler(httptransport.NewServer(
		e.PostAddressEndpoint,
		decodePostAddressRequest,
		encodeResponse,
		options...,
	))
	r.Methods("DELETE").Path("/profiles/{id}/addresses/{addressID}").Handler(httptransport.NewServer(
		e.DeleteAddressEndpoint,
		decodeDeleteAddressRequest,
		encodeResponse,
		options...,
	))
	return r
}
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)
}
1192995
func (e *Echo) DefaultHTTPErrorHandler(err error, c Context) {
	code := http.StatusInternalServerError
	msg := http.StatusText(code)
	if he, ok := err.(*HTTPError); ok {
		code = he.Code
		msg = he.Message
	}
	if e.debug {
		msg = err.Error()
	}
	if !c.Response().Committed() {
		if c.Request().Method() == HEAD {
			c.NoContent(code)
		} else {
			if code > 0 {
				c.String(msg, code)
			} else {
				c.String(msg)
			}
		}
	}
	e.logger.Debug(err)
}
650198
func (l *Logger) handleError(err error) error {
	h := l.errorHandler.Load().(func(error) error)
	if h == nil {
		return err
	}
	return h(err)
}
974384
func MakeHandler(validpath *regexp.Regexp, parser Json2dataConverter, handler HttpHandler, mustHaveInputData bool) http.HandlerFunc {

	return func(w http.ResponseWriter, r *http.Request) {

		// check path
		m := validpath.FindStringSubmatch(r.URL.Path)
		if m == nil {
			http.NotFound(w, r)
			return
		}

		// handle OPTIONS request
		if r.Method == "OPTIONS" {
			setDefaultHeaders(w)
			return
		}

		// skip other methods
		if r.Method != "POST" && r.Method != "GET" {
			return
		}

		// set access and return type
		origin := r.Header.Get("Origin")
		w.Header().Set("Access-Control-Allow-Origin", origin)
		w.Header().Set("Content-Type", "application/json")

		// handle POST
		var dat []byte
		var err error
		if r.Method == "POST" {
			dat, err = ioutil.ReadAll(r.Body) // parse 'body'
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
		}

		// handle GET
		if r.Method == "GET" {
			err = r.ParseForm()
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			for str, _ := range r.Form {
				dat = []byte(str)
				break
			}
		}

		// parse json and call handler
		d := parser(dat)
		if d == nil && mustHaveInputData {
			http.Error(w, "handlermaker.go: parser failed: data is <nil>", http.StatusInternalServerError)
			return
		}
		res, err := handler(w, r, d)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
		if res != nil {
			fmt.Fprintf(w, res.String())
		}
	}
}
595510
func NewURLHandlerError(url string) error {
	return Error{
		Message:    fmt.Sprintf("no URL handler for %s", url),
		Details:    nil,
		ErrorCode:  URLHandlerError,
		Suggestion: "check the URL",
	}
}
942958
func makeHandler(binding interface{}, o *Options) martini.Handler {

	return func(context martini.Context, resp http.ResponseWriter, req *http.Request) {
		// Upgrade the request to a websocket connection
		ws, status, err := upgradeRequest(resp, req, o)
		if err != nil {
			resp.WriteHeader(status)
			resp.Write([]byte(err.Error()))
			return
		}

		// Set up the connection
		c := newBinding(binding, ws, o)

		// Set the options for the gorilla websocket package
		c.setSocketOptions()

		// Map the sending and receiving channels
		c.mapChannels(context)

		// Map the Channels <-chan error, <-chan bool and chan<- bool
		c.mapDefaultChannels(context)

		// start the send and receive goroutines
		go c.send()
		go c.recv()
		go waitForDisconnect(c)

		// call the next handler, which must block
		context.Next()
	}
}
644741
func RecoveryHandler(handler func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
	return Recoverer(http.HandlerFunc(handler)).ServeHTTP
}
349332
func (b *Base) SetErrorHandler(eh ErrorHandler) error {
	if eh == nil {
		return errors.New("error handler must be non-nil")
	}

	b.mutex.Lock()
	defer b.mutex.Unlock()
	b.errHandler = eh

	return nil
}