github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/api/handlers/utils/errors.go (about)

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/containers/libpod/libpod/define"
     8  	"github.com/pkg/errors"
     9  	log "github.com/sirupsen/logrus"
    10  )
    11  
    12  var (
    13  	ErrLinkNotSupport = errors.New("Link is not supported")
    14  )
    15  
    16  // Error formats an API response to an error
    17  //
    18  // apiMessage and code must match the container API, and are sent to client
    19  // err is logged on the system running the podman service
    20  func Error(w http.ResponseWriter, apiMessage string, code int, err error) {
    21  	// Log detailed message of what happened to machine running podman service
    22  	log.Infof("Request Failed(%s): %s", http.StatusText(code), err.Error())
    23  	em := ErrorModel{
    24  		Because:      (errors.Cause(err)).Error(),
    25  		Message:      err.Error(),
    26  		ResponseCode: code,
    27  	}
    28  	WriteJSON(w, code, em)
    29  }
    30  
    31  func VolumeNotFound(w http.ResponseWriter, name string, err error) {
    32  	if errors.Cause(err) != define.ErrNoSuchVolume {
    33  		InternalServerError(w, err)
    34  	}
    35  	msg := fmt.Sprintf("No such volume: %s", name)
    36  	Error(w, msg, http.StatusNotFound, err)
    37  }
    38  func ContainerNotFound(w http.ResponseWriter, name string, err error) {
    39  	if errors.Cause(err) != define.ErrNoSuchCtr {
    40  		InternalServerError(w, err)
    41  	}
    42  	msg := fmt.Sprintf("No such container: %s", name)
    43  	Error(w, msg, http.StatusNotFound, err)
    44  }
    45  
    46  func ImageNotFound(w http.ResponseWriter, name string, err error) {
    47  	if errors.Cause(err) != define.ErrNoSuchImage {
    48  		InternalServerError(w, err)
    49  	}
    50  	msg := fmt.Sprintf("No such image: %s", name)
    51  	Error(w, msg, http.StatusNotFound, err)
    52  }
    53  
    54  func PodNotFound(w http.ResponseWriter, name string, err error) {
    55  	if errors.Cause(err) != define.ErrNoSuchPod {
    56  		InternalServerError(w, err)
    57  	}
    58  	msg := fmt.Sprintf("No such pod: %s", name)
    59  	Error(w, msg, http.StatusNotFound, err)
    60  }
    61  
    62  func ContainerNotRunning(w http.ResponseWriter, containerID string, err error) {
    63  	msg := fmt.Sprintf("Container %s is not running", containerID)
    64  	Error(w, msg, http.StatusConflict, err)
    65  }
    66  
    67  func InternalServerError(w http.ResponseWriter, err error) {
    68  	Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError, err)
    69  }
    70  
    71  func BadRequest(w http.ResponseWriter, key string, value string, err error) {
    72  	e := errors.Wrapf(err, "Failed to parse query parameter '%s': %q", key, value)
    73  	Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, e)
    74  }
    75  
    76  type ErrorModel struct {
    77  	// API root cause formatted for automated parsing
    78  	// example: API root cause
    79  	Because string `json:"cause"`
    80  	// human error message, formatted for a human to read
    81  	// example: human error message
    82  	Message string `json:"message"`
    83  	// http response code
    84  	ResponseCode int `json:"response"`
    85  }
    86  
    87  func (e ErrorModel) Error() string {
    88  	return e.Message
    89  }
    90  
    91  func (e ErrorModel) Cause() error {
    92  	return errors.New(e.Because)
    93  }
    94  
    95  func (e ErrorModel) Code() int {
    96  	return e.ResponseCode
    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  }