github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/api/handlers/utils/errors.go (about) 1 package utils 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/containers/podman/v2/libpod/define" 8 "github.com/containers/podman/v2/pkg/domain/entities" 9 "github.com/pkg/errors" 10 log "github.com/sirupsen/logrus" 11 ) 12 13 var ( 14 ErrLinkNotSupport = errors.New("Link is not supported") 15 ) 16 17 // TODO: document the exported functions in this file and make them more 18 // generic (e.g., not tied to one ctr/pod). 19 20 // Error formats an API response to an error 21 // 22 // apiMessage and code must match the container API, and are sent to client 23 // err is logged on the system running the podman service 24 func Error(w http.ResponseWriter, apiMessage string, code int, err error) { 25 // Log detailed message of what happened to machine running podman service 26 log.Infof("Request Failed(%s): %s", http.StatusText(code), err.Error()) 27 em := entities.ErrorModel{ 28 Because: (errors.Cause(err)).Error(), 29 Message: err.Error(), 30 ResponseCode: code, 31 } 32 WriteJSON(w, code, em) 33 } 34 35 func VolumeNotFound(w http.ResponseWriter, name string, err error) { 36 if errors.Cause(err) != define.ErrNoSuchVolume { 37 InternalServerError(w, err) 38 } 39 msg := fmt.Sprintf("No such volume: %s", name) 40 Error(w, msg, http.StatusNotFound, err) 41 } 42 43 func ContainerNotFound(w http.ResponseWriter, name string, err error) { 44 if errors.Cause(err) != define.ErrNoSuchCtr { 45 InternalServerError(w, err) 46 } 47 msg := fmt.Sprintf("No such container: %s", name) 48 Error(w, msg, http.StatusNotFound, err) 49 } 50 51 func ImageNotFound(w http.ResponseWriter, name string, err error) { 52 if errors.Cause(err) != define.ErrNoSuchImage { 53 InternalServerError(w, err) 54 } 55 msg := fmt.Sprintf("No such image: %s", name) 56 Error(w, msg, http.StatusNotFound, err) 57 } 58 59 func NetworkNotFound(w http.ResponseWriter, name string, err error) { 60 if errors.Cause(err) != define.ErrNoSuchNetwork { 61 InternalServerError(w, err) 62 } 63 msg := fmt.Sprintf("No such network: %s", name) 64 Error(w, msg, http.StatusNotFound, err) 65 } 66 67 func PodNotFound(w http.ResponseWriter, name string, err error) { 68 if errors.Cause(err) != define.ErrNoSuchPod { 69 InternalServerError(w, err) 70 } 71 msg := fmt.Sprintf("No such pod: %s", name) 72 Error(w, msg, http.StatusNotFound, err) 73 } 74 75 func SessionNotFound(w http.ResponseWriter, name string, err error) { 76 if errors.Cause(err) != define.ErrNoSuchExecSession { 77 InternalServerError(w, err) 78 } 79 msg := fmt.Sprintf("No such exec session: %s", name) 80 Error(w, msg, http.StatusNotFound, err) 81 } 82 83 func ContainerNotRunning(w http.ResponseWriter, containerID string, err error) { 84 msg := fmt.Sprintf("Container %s is not running", containerID) 85 Error(w, msg, http.StatusConflict, err) 86 } 87 88 func InternalServerError(w http.ResponseWriter, err error) { 89 Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError, err) 90 } 91 92 func BadRequest(w http.ResponseWriter, key string, value string, err error) { 93 e := errors.Wrapf(err, "failed to parse query parameter '%s': %q", key, value) 94 Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, e) 95 } 96 97 // UnsupportedParameter logs a given param by its string name as not supported. 98 func UnSupportedParameter(param string) { 99 log.Infof("API parameter %q: not supported", param) 100 }