github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/api/handlers/utils/errors.go (about)

     1  package utils
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/hanks177/podman/v4/libpod/define"
     7  	"github.com/hanks177/podman/v4/pkg/errorhandling"
     8  	"github.com/containers/storage"
     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, 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 := errorhandling.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  	Error(w, http.StatusNotFound, err)
    40  }
    41  
    42  func ContainerNotFound(w http.ResponseWriter, name string, err error) {
    43  	switch errors.Cause(err) {
    44  	case define.ErrNoSuchCtr, define.ErrCtrExists:
    45  		Error(w, http.StatusNotFound, err)
    46  	default:
    47  		InternalServerError(w, err)
    48  	}
    49  }
    50  
    51  func ImageNotFound(w http.ResponseWriter, name string, err error) {
    52  	if errors.Cause(err) != storage.ErrImageUnknown {
    53  		InternalServerError(w, err)
    54  	}
    55  	Error(w, http.StatusNotFound, err)
    56  }
    57  
    58  func NetworkNotFound(w http.ResponseWriter, name string, err error) {
    59  	if errors.Cause(err) != define.ErrNoSuchNetwork {
    60  		InternalServerError(w, err)
    61  	}
    62  	Error(w, http.StatusNotFound, err)
    63  }
    64  
    65  func PodNotFound(w http.ResponseWriter, name string, err error) {
    66  	if errors.Cause(err) != define.ErrNoSuchPod {
    67  		InternalServerError(w, err)
    68  	}
    69  	Error(w, http.StatusNotFound, err)
    70  }
    71  
    72  func SessionNotFound(w http.ResponseWriter, name string, err error) {
    73  	if errors.Cause(err) != define.ErrNoSuchExecSession {
    74  		InternalServerError(w, err)
    75  	}
    76  	Error(w, http.StatusNotFound, err)
    77  }
    78  
    79  func SecretNotFound(w http.ResponseWriter, nameOrID string, err error) {
    80  	if errors.Cause(err).Error() != "no such secret" {
    81  		InternalServerError(w, err)
    82  	}
    83  	Error(w, http.StatusNotFound, err)
    84  }
    85  
    86  func ContainerNotRunning(w http.ResponseWriter, containerID string, err error) {
    87  	Error(w, http.StatusConflict, err)
    88  }
    89  
    90  func InternalServerError(w http.ResponseWriter, err error) {
    91  	Error(w, http.StatusInternalServerError, err)
    92  }
    93  
    94  func BadRequest(w http.ResponseWriter, key string, value string, err error) {
    95  	e := errors.Wrapf(err, "failed to parse query parameter '%s': %q", key, value)
    96  	Error(w, http.StatusBadRequest, e)
    97  }
    98  
    99  // UnsupportedParameter logs a given param by its string name as not supported.
   100  func UnSupportedParameter(param string) {
   101  	log.Infof("API parameter %q: not supported", param)
   102  }