summaryrefslogtreecommitdiff
path: root/pkg/api/handlers/utils/errors.go
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2020-01-10 21:28:05 +0100
committerGitHub <noreply@github.com>2020-01-10 21:28:05 +0100
commite1ffac6cc73eb36640cbaf6a1a28ba44749a96d9 (patch)
tree61134dad4b2efdaa65dee6e7206e5ce20080302c /pkg/api/handlers/utils/errors.go
parent6ed88e047579bd2d1eac99a6089cc617f0c4773d (diff)
parent986feef2e80cfaed7cbce0df4fd5d619bcffefd7 (diff)
downloadpodman-e1ffac6cc73eb36640cbaf6a1a28ba44749a96d9.tar.gz
podman-e1ffac6cc73eb36640cbaf6a1a28ba44749a96d9.tar.bz2
podman-e1ffac6cc73eb36640cbaf6a1a28ba44749a96d9.zip
Merge pull request #4832 from baude/apiv2tomaster
Apiv2tomaster
Diffstat (limited to 'pkg/api/handlers/utils/errors.go')
-rw-r--r--pkg/api/handlers/utils/errors.go88
1 files changed, 88 insertions, 0 deletions
diff --git a/pkg/api/handlers/utils/errors.go b/pkg/api/handlers/utils/errors.go
new file mode 100644
index 000000000..3ec0742bd
--- /dev/null
+++ b/pkg/api/handlers/utils/errors.go
@@ -0,0 +1,88 @@
+package utils
+
+import (
+ "fmt"
+ "net/http"
+
+ "github.com/containers/libpod/libpod/define"
+ "github.com/pkg/errors"
+ log "github.com/sirupsen/logrus"
+)
+
+var (
+ ErrLinkNotSupport = errors.New("Link is not supported")
+)
+
+// Error formats an API response to an error
+//
+// apiMessage and code must match the container API, and are sent to client
+// err is logged on the system running the podman service
+func Error(w http.ResponseWriter, apiMessage string, code int, err error) {
+ // Log detailed message of what happened to machine running podman service
+ log.Infof("Request Failed(%s): %s", http.StatusText(code), err.Error())
+ em := ErrorModel{
+ Because: (errors.Cause(err)).Error(),
+ Message: err.Error(),
+ }
+ WriteJSON(w, code, em)
+}
+
+func VolumeNotFound(w http.ResponseWriter, nameOrId string, err error) {
+ if errors.Cause(err) != define.ErrNoSuchVolume {
+ InternalServerError(w, err)
+ }
+ msg := fmt.Sprintf("No such volume: %s", nameOrId)
+ Error(w, msg, http.StatusNotFound, err)
+}
+func ContainerNotFound(w http.ResponseWriter, nameOrId string, err error) {
+ if errors.Cause(err) != define.ErrNoSuchCtr {
+ InternalServerError(w, err)
+ }
+ msg := fmt.Sprintf("No such container: %s", nameOrId)
+ Error(w, msg, http.StatusNotFound, err)
+}
+
+func ImageNotFound(w http.ResponseWriter, nameOrId string, err error) {
+ if errors.Cause(err) != define.ErrNoSuchImage {
+ InternalServerError(w, err)
+ }
+ msg := fmt.Sprintf("No such image: %s", nameOrId)
+ Error(w, msg, http.StatusNotFound, err)
+}
+
+func PodNotFound(w http.ResponseWriter, nameOrId string, err error) {
+ if errors.Cause(err) != define.ErrNoSuchPod {
+ InternalServerError(w, err)
+ }
+ msg := fmt.Sprintf("No such pod: %s", nameOrId)
+ Error(w, msg, http.StatusNotFound, err)
+}
+
+func ContainerNotRunning(w http.ResponseWriter, containerID string, err error) {
+ msg := fmt.Sprintf("Container %s is not running", containerID)
+ Error(w, msg, http.StatusConflict, err)
+}
+
+func InternalServerError(w http.ResponseWriter, err error) {
+ Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError, err)
+}
+
+func BadRequest(w http.ResponseWriter, key string, value string, err error) {
+ e := errors.Wrapf(err, "Failed to parse query parameter '%s': %q", key, value)
+ Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, e)
+}
+
+type ErrorModel struct {
+ // root cause
+ Because string `json:"cause"`
+ // error message
+ Message string `json:"message"`
+}
+
+func (e ErrorModel) Error() string {
+ return e.Message
+}
+
+func (e ErrorModel) Cause() error {
+ return errors.New(e.Because)
+}