apierror

package
v1.17.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2024 License: Apache-2.0 Imports: 13 Imported by: 32

Documentation

Index

Constants

View Source
const (
	// ErrTimedOutMsg is returned when the error is context.DeadlineExceeded.
	ErrTimedOutMsg = "operation timed out"
)

Variables

View Source
var (
	// ErrMissingAPI is thrown when the *api.API reference is null.
	ErrMissingAPI = errors.New("api reference is required for the operation")

	// ErrDeploymentID is the message returned when a provided cluster id is not of the expected length (32 chars).
	ErrDeploymentID = errors.New("deployment id should have a length of 32 characters")

	// ErrMissingElevatedPermissions is returned when the error code is 449.
	ErrMissingElevatedPermissions = errors.New("the requested operation requires elevated permissions")
)

Functions

func IsRuntimeStatusCode added in v1.2.0

func IsRuntimeStatusCode(err error, code int) bool

IsRuntimeStatusCode returns true when the error is of *runtime.APIError type and the error status code matches the one passed as the argument.

func IsRuntimeStatusCodeHasString added in v1.2.0

func IsRuntimeStatusCodeHasString(err error, code int, msg string) bool

IsRuntimeStatusCodeHasString returns true when the error is *runtime.APIError and the error status code matches the one passed as the argument.

func NewJSONError added in v1.1.0

func NewJSONError(err error) error

NewJSONError creates a marshaleable error from an error.

func Unwrap

func Unwrap(err error) error

Unwrap Deprecated: unpacks an error message returned from a client API call. It checks for a few cases where the returned API error might not have been properly casted to its error type. It covers the following cases in order:

  • error is nil, in which case nil is returned.
  • error is a context.DeadlineExceeded error, which equals a timeout.
  • error is of type *runtime.APIError, meaning the returned API error wasn't defined in the Swagger spec from which the source code has been generated
  • HTTP code is 449, the authenticated user needs to elevate-permissions.
  • The type wraps *http.Response, the body is read and tries json.Unmarshal to *models.BasicFailedResponse and each of the BasicFailedReplyElement is then added to an instance of multierror.Prefixed and returned.
  • The error is unknown, returns "<OperationName> (status <StatusCode)".
  • error is a correctly unpacked into BasicFailedReply object which needs to be unpacked from its container struct. If the error cannot be unpacked to a BasicFailedReply, then a stringified json.MarshalIndent error is formed.

func Wrap added in v1.2.0

func Wrap(err error) error

Wrap creates a new Error from the passed error, it doesn't modify the original error, but instead, tries to unwrap and extract the wrapped errors form the go-openapi/runtime operations. The original error type can be accessed by calling Unwrap() or errors.Unwrap/Is/As.

If the error is of *models.BasicFailedReply type, then the errors are unpacked into a *multierror.Prefixed which can be obtained by calling Multierror(). The other possible case is when the API returns an error with an unexpected status code which is not present in the swagger spec, in which case, the same operation will be attempted (unwrapping a BasicFailedReply) with a fallback to unmarshal any JSON which can be read:

  • error is nil, in which case nil is returned.
  • error is a context.DeadlineExceeded error, which equals a timeout.
  • error is of type *runtime.APIError, meaning the returned API error wasn't defined in the Swagger spec from which the source code has been generated
  • HTTP code is 449, the authenticated user needs to elevate-permissions.
  • The type wraps *http.Response, the body is read and tries json.Unmarshal to *models.BasicFailedResponse and each of the BasicFailedReplyElement is then added to an instance of multierror.Prefixed and returned.
  • The error is unknown, returns "<OperationName> (status <StatusCode)".

Types

type Error added in v1.2.0

type Error struct {
	// Err is the unmodified wrapped error.
	Err error
	// contains filtered or unexported fields
}

Error wraps a API error and implements both the Error and Unwrap interfaces.

func (*Error) Error added in v1.2.0

func (e *Error) Error() string

func (*Error) Is added in v1.2.0

func (e *Error) Is(target error) bool

Is implements errors.Is by comparing the current value directly.

func (*Error) Multierror added in v1.2.0

func (e *Error) Multierror() *multierror.Prefixed

Multierror returns a multierror if there's one.

func (*Error) Unwrap added in v1.2.0

func (e *Error) Unwrap() error

Unwrap returns the wrapped error.

type JSONError added in v1.1.0

type JSONError struct {
	Message string `json:"message,omitempty"`
}

JSONError wraps any incoming error inside this struct so that it can be correctly marshaled to JSON. If Error() is called, the error is still returned in a string format.

func (JSONError) Error added in v1.1.0

func (me JSONError) Error() string

Error complies with the error interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL