github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/api/server/errorhandler.go (about) 1 package server 2 3 import ( 4 "net/http" 5 6 "github.com/docker/docker/api/server/httpstatus" 7 "github.com/docker/docker/api/server/httputils" 8 "github.com/docker/docker/api/types" 9 "github.com/docker/docker/api/types/versions" 10 "github.com/gorilla/mux" 11 "google.golang.org/grpc/status" 12 ) 13 14 // makeErrorHandler makes an HTTP handler that decodes a Docker error and 15 // returns it in the response. 16 func makeErrorHandler(err error) http.HandlerFunc { 17 return func(w http.ResponseWriter, r *http.Request) { 18 statusCode := httpstatus.FromError(err) 19 vars := mux.Vars(r) 20 if apiVersionSupportsJSONErrors(vars["version"]) { 21 response := &types.ErrorResponse{ 22 Message: err.Error(), 23 } 24 _ = httputils.WriteJSON(w, statusCode, response) 25 } else { 26 http.Error(w, status.Convert(err).Message(), statusCode) 27 } 28 } 29 } 30 31 func apiVersionSupportsJSONErrors(version string) bool { 32 const firstAPIVersionWithJSONErrors = "1.23" 33 return version == "" || versions.GreaterThan(version, firstAPIVersionWithJSONErrors) 34 }