// NewMethodNotSupported returns an error indicating the requested action is not supported on this kind. func NewMethodNotSupported(qualifiedResource schema.GroupResource, action string) *StatusError { return &StatusError{metav1.Status{ Status: metav1.StatusFailure, Code: http.StatusMethodNotAllowed, Reason: metav1.StatusReasonMethodNotAllowed, Details: &metav1.StatusDetails{ Group: qualifiedResource.Group, Kind: qualifiedResource.Resource, }, Message: fmt.Sprintf("%s is not supported on resources of kind %q", action, qualifiedResource.String()), }} }
// NewNotFound returns a new error which indicates that the resource of the kind and the name was not found. func NewNotFound(qualifiedResource schema.GroupResource, name string) *StatusError { return &StatusError{metav1.Status{ Status: metav1.StatusFailure, Code: http.StatusNotFound, Reason: metav1.StatusReasonNotFound, Details: &metav1.StatusDetails{ Group: qualifiedResource.Group, Kind: qualifiedResource.Resource, Name: name, }, Message: fmt.Sprintf("%s %q not found", qualifiedResource.String(), name), }} }
// NewConflict returns an error indicating the item can't be updated as provided. func NewConflict(qualifiedResource schema.GroupResource, name string, err error) *StatusError { return &StatusError{metav1.Status{ Status: metav1.StatusFailure, Code: http.StatusConflict, Reason: metav1.StatusReasonConflict, Details: &metav1.StatusDetails{ Group: qualifiedResource.Group, Kind: qualifiedResource.Resource, Name: name, }, Message: fmt.Sprintf("Operation cannot be fulfilled on %s %q: %v", qualifiedResource.String(), name, err), }} }
// NewForbidden returns an error indicating the requested action was forbidden func NewForbidden(qualifiedResource schema.GroupResource, name string, err error) *StatusError { return &StatusError{metav1.Status{ Status: metav1.StatusFailure, Code: http.StatusForbidden, Reason: metav1.StatusReasonForbidden, Details: &metav1.StatusDetails{ Group: qualifiedResource.Group, Kind: qualifiedResource.Resource, Name: name, }, Message: fmt.Sprintf("%s %q is forbidden: %v", qualifiedResource.String(), name, err), }} }
// NewAlreadyExists returns an error indicating the item requested exists by that identifier. func NewAlreadyExists(qualifiedResource schema.GroupResource, name string) *StatusError { return &StatusError{metav1.Status{ Status: metav1.StatusFailure, Code: http.StatusConflict, Reason: metav1.StatusReasonAlreadyExists, Details: &metav1.StatusDetails{ Group: qualifiedResource.Group, Kind: qualifiedResource.Resource, Name: name, }, Message: fmt.Sprintf("%s %q already exists", qualifiedResource.String(), name), }} }
// NewGenericServerResponse returns a new error for server responses that are not in a recognizable form. func NewGenericServerResponse(code int, verb string, qualifiedResource schema.GroupResource, name, serverMessage string, retryAfterSeconds int, isUnexpectedResponse bool) *StatusError { reason := metav1.StatusReasonUnknown message := fmt.Sprintf("the server responded with the status code %d but did not return more information", code) switch code { case http.StatusConflict: if verb == "POST" { reason = metav1.StatusReasonAlreadyExists } else { reason = metav1.StatusReasonConflict } message = "the server reported a conflict" case http.StatusNotFound: reason = metav1.StatusReasonNotFound message = "the server could not find the requested resource" case http.StatusBadRequest: reason = metav1.StatusReasonBadRequest message = "the server rejected our request for an unknown reason" case http.StatusUnauthorized: reason = metav1.StatusReasonUnauthorized message = "the server has asked for the client to provide credentials" case http.StatusForbidden: reason = metav1.StatusReasonForbidden // the server message has details about who is trying to perform what action. Keep its message. message = serverMessage case http.StatusMethodNotAllowed: reason = metav1.StatusReasonMethodNotAllowed message = "the server does not allow this method on the requested resource" case StatusUnprocessableEntity: reason = metav1.StatusReasonInvalid message = "the server rejected our request due to an error in our request" case StatusServerTimeout: reason = metav1.StatusReasonServerTimeout message = "the server cannot complete the requested operation at this time, try again later" case StatusTooManyRequests: reason = metav1.StatusReasonTimeout message = "the server has received too many requests and has asked us to try again later" default: if code >= 500 { reason = metav1.StatusReasonInternalError message = fmt.Sprintf("an error on the server (%q) has prevented the request from succeeding", serverMessage) } } switch { case !qualifiedResource.Empty() && len(name) > 0: message = fmt.Sprintf("%s (%s %s %s)", message, strings.ToLower(verb), qualifiedResource.String(), name) case !qualifiedResource.Empty(): message = fmt.Sprintf("%s (%s %s)", message, strings.ToLower(verb), qualifiedResource.String()) } var causes []metav1.StatusCause if isUnexpectedResponse { causes = []metav1.StatusCause{ { Type: metav1.CauseTypeUnexpectedServerResponse, Message: serverMessage, }, } } else { causes = nil } return &StatusError{metav1.Status{ Status: metav1.StatusFailure, Code: int32(code), Reason: reason, Details: &metav1.StatusDetails{ Group: qualifiedResource.Group, Kind: qualifiedResource.Resource, Name: name, Causes: causes, RetryAfterSeconds: int32(retryAfterSeconds), }, Message: message, }} }
// NewServerTimeout returns an error indicating the requested action could not be completed due to a // transient error, and the client should try again. func NewServerTimeout(qualifiedResource schema.GroupResource, operation string, retryAfterSeconds int) *StatusError { return &StatusError{metav1.Status{ Status: metav1.StatusFailure, Code: http.StatusInternalServerError, Reason: metav1.StatusReasonServerTimeout, Details: &metav1.StatusDetails{ Group: qualifiedResource.Group, Kind: qualifiedResource.Resource, Name: operation, RetryAfterSeconds: int32(retryAfterSeconds), }, Message: fmt.Sprintf("The %s operation against %s could not be completed at this time, please try again.", operation, qualifiedResource.String()), }} }