errors

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2025 License: MPL-2.0 Imports: 6 Imported by: 1

Documentation

Overview

Package errors defines custom error types for the SDK.

Package errors provides a single, unified custom error type for the CortexCloud SDK, designed to encapsulate all error information, whether originating from an upstream API or from internal SDK operations.

Index

Constants

View Source
const (
	// Error Codes/Messages
	CodePreRequestValidationFailure = "PreRequestArgumentValidationFailure"
	MsgPreRequestValidationFailure  = "Pre-request argument validation failed. See details for more information."

	// Error Detail Codes/Messages
	DetailCodeUnexpectedValidationError = "UnexpectedValiadationError"
	DetailMsgUnexpectedValidationError  = "Encountered unexpected error during validation. See \"Error\" field for more information."

	DetailCodeUnknownValidationTag = "UnknownValidationTag"
	DetailMsgUnknownValidationTag  = "" /* 140-byte string literal not displayed */

	DetailCodeMissingRequiredValue = "MissingRequiredValue"
	DetailMsgMissingRequiredValue  = "\"%s\" is a required value."

	DetailCodeMinimumNumberOfValues = "MinimumNumberOfValues"
	DetailMsgMinimumNumberOfValues  = "Field \"%s\" must have at least %s value(s) provided (recieved %d)."

	DetailCodeInvalidEnumValue = "InvalidEnumValue"
	DetailMsgInvalidEnumValue  = "Invalid %s value \"%v\" - expected one of: %s"

	CodeAPIResponseParsingFailure             = ""
	CodeSDKInitializationFailure              = ""
	CodeRequestSerializationFailure           = ""
	CodeURLConstructionFailure                = ""
	CodeContextCancellation                   = ""
	CodeHTTPRequestCreationFailure            = ""
	CodeAuthenticationHeaderGenerationFailure = ""
	CodeNetworkError                          = ""
	CodeNoResponseReceived                    = ""
	CodeResponseBodyReadFailure               = ""
	CodeResponseDeserializationFailure        = ""
)

Variables

View Source
var (
	GitCommit           = "NOCOMMIT"
	CortexServerVersion = "UNKNOWN"
	CortexPAPIVersion   = "UNKNOWN"
	GoVersion           = "UNKNOWN"
	BuildDate           = "UNKNOWN"
)

Functions

func AsCortexCloudSdkError

func AsCortexCloudSdkError(err error, target **CortexCloudSdkError) bool

AsCortexCloudSdkError attempts to cast the given error to *CortexCloudSdkError. It returns true if the conversion is successful, and the sdkErr pointer will point to the CortexCloudSdkError instance. This is a wrapper around errors.As.

func IsCortexCloudSdkError

func IsCortexCloudSdkError(err error) bool

IsCortexCloudSdkError checks if the given error is of type *CortexCloudSdkError. It uses errors.As to safely perform the type assertion.

Types

type CortexCloudAPIError

type CortexCloudAPIError struct {
	Reply   *CortexCloudAPIErrorReply   `json:"reply,omitempty"`
	Data    *CortexCloudAPIErrorData    `json:"data,omitempty"`
	Code    *string                     `json:"errorCode,omitempty"`
	Message *string                     `json:"message,omitempty"`
	Details *CortexCloudAPIErrorDetails `json:"details,omitempty"`
}

func NewCortexCloudAPIError

func NewCortexCloudAPIError(code string, message string, details CortexCloudAPIErrorDetails) CortexCloudAPIError

func (CortexCloudAPIError) Error

func (e CortexCloudAPIError) Error() string

func (CortexCloudAPIError) ToBuiltin

func (e CortexCloudAPIError) ToBuiltin() error

type CortexCloudAPIErrorContext

type CortexCloudAPIErrorContext struct {
	Expected  string `json:"expected,omitempty"`
	MinLength int    `json:"min_length,omitempty"`
}

type CortexCloudAPIErrorData added in v1.0.1

type CortexCloudAPIErrorData struct {
	Message  string                       `json:"err_msg"`
	Metadata *CortexCloudAPIErrorMetadata `json:"metadata,omitempty"`
}

type CortexCloudAPIErrorDetail

type CortexCloudAPIErrorDetail struct {
	Type     string                     `json:"type"`
	Location []any                      `json:"loc"`
	Message  string                     `json:"msg"`
	Input    any                        `json:"input"`
	Context  CortexCloudAPIErrorContext `json:"ctx"`
}

type CortexCloudAPIErrorDetails

type CortexCloudAPIErrorDetails struct {
	Params CortexCloudAPIErrorParams `json:"params"`
}

type CortexCloudAPIErrorExtra

type CortexCloudAPIErrorExtra struct {
	Type     string                     `json:"type"`
	Location []any                      `json:"loc"`
	Message  string                     `json:"msg"`
	Input    any                        `json:"input"`
	Context  CortexCloudAPIErrorContext `json:"ctx"`
}

type CortexCloudAPIErrorMetadata added in v1.0.1

type CortexCloudAPIErrorMetadata struct {
	Code  int             `json:"err_code"`
	Extra ErrorExtraField `json:"err_extra"`
}

type CortexCloudAPIErrorParams

type CortexCloudAPIErrorParams struct {
	Message string `json:"message"`
}

type CortexCloudAPIErrorReply

type CortexCloudAPIErrorReply struct {
	Code    int             `json:"err_code"`
	Message string          `json:"err_msg"`
	Extra   ErrorExtraField `json:"err_extra"`
}

type CortexCloudSdkError

type CortexCloudSdkError struct {
	Code                string                      `json:"code"`                  // A unique, machine-readable error code (e.g., "INVALID_ARGUMENT", "UNAUTHORIZED", "SDK_INIT_FAILED").
	Message             string                      `json:"message"`               // A human-readable message describing the error.
	Details             []CortexCloudSdkErrorDetail `json:"details"`               // Optional, additional context or validation errors.
	InternalErrorsCount int                         `json:"internal_errors_count"` // Total number of internal errors returned.
	HTTPStatus          *int                        `json:"http_status"`           // The HTTP status code associated with this error. This value is nil for internal errors.
	Err                 error                       `json:"underlying_error"`      // The underlying Go error returned by the validation module.
}

CortexCloudSdkError represents a structured error within the CortexCloud SDK. It can represent errors returned by an upstream API (with HTTPStatus populated) or internal SDK errors (with HTTPStatus as nil).

func NewBadRequest

func NewBadRequest(code, message string, details []CortexCloudSdkErrorDetail) *CortexCloudSdkError

NewBadRequest creates a CortexCloudSdkError for HTTP 400 Bad Request. Use this for client-side input validation errors from an API.

func NewConflict

func NewConflict(code, message string, details []CortexCloudSdkErrorDetail) *CortexCloudSdkError

NewConflict creates a CortexCloudSdkError for HTTP 409 Conflict. Use this when an API request conflicts with the current state of the target resource.

func NewCortexCloudSdkError

func NewCortexCloudSdkError(
	code string,
	message string,
	details []CortexCloudSdkErrorDetail,
	httpStatus *int,
	err error,
) *CortexCloudSdkError

NewCortexCloudSdkError creates a new CortexCloudSdkError. Use this as the primary constructor for all SDK errors.

Parameters:

code: A machine-readable error code.
message: A human-readable message.
details: Optional slice of CortexCloudSdkErrorDetail for more context.
httpStatus: Optional HTTP status code (pass nil for internal SDK errors).
err: Optional underlying Go error to wrap.

func NewForbidden

func NewForbidden(code, message string) *CortexCloudSdkError

NewForbidden creates a CortexCloudSdkError for HTTP 403 Forbidden. Use this when the client is authenticated but does not have permission to access the resource via API.

func NewInternalSDKError

func NewInternalSDKError(code, message string, err error) *CortexCloudSdkError

NewInternalSDKError creates a CortexCloudSdkError for errors originating purely within the SDK's logic. These errors will have HTTPStatus as nil.

func NewInternalServerError

func NewInternalServerError(code, message string, err error) *CortexCloudSdkError

NewInternalServerError creates a CortexCloudSdkError for HTTP 500 Internal Server Error. Use this for unexpected server-side errors from an API, potentially wrapping an underlying Go error.

func NewNotFound

func NewNotFound(code, message string) *CortexCloudSdkError

NewNotFound creates a CortexCloudSdkError for HTTP 404 Not Found. Use this when the requested API resource does not exist.

func NewPreRequestValidationError

func NewPreRequestValidationError(details []CortexCloudSdkErrorDetail, err error) *CortexCloudSdkError

func NewServiceUnavailable

func NewServiceUnavailable(code, message string) *CortexCloudSdkError

NewServiceUnavailable creates a CortexCloudSdkError for HTTP 503 Service Unavailable. Use this when the upstream API server is not ready to handle the request.

func NewUnauthorized

func NewUnauthorized(code, message string) *CortexCloudSdkError

NewUnauthorized creates a CortexCloudSdkError for HTTP 401 Unauthorized. Use this when authentication credentials are missing or invalid for API calls.

func (CortexCloudSdkError) DetailsToJSON

func (e CortexCloudSdkError) DetailsToJSON() (string, error)

DetailsToJSON serializes the CortexCloudSdkError.Details into a JSON string without indentation. It excludes the `Err` field as it's marked with `json:"-"`.

func (CortexCloudSdkError) DetailsToPrettyJSON

func (e CortexCloudSdkError) DetailsToPrettyJSON() (string, error)

DetailsToPrettyJSON serializes the CortexCloudSdkErrors.Details into a JSON string with indentation. It excludes the `Err` field.

func (*CortexCloudSdkError) Error

func (e *CortexCloudSdkError) Error() string

Error implements the error interface for CortexCloudSdkError. It returns a formatted string representation of the error.

func (CortexCloudSdkError) ToJSON

func (e CortexCloudSdkError) ToJSON() (string, error)

ToJSON serializes the CortexCloudSdkError into a JSON string without indentation. It excludes the `Err` field as it's marked with `json:"-"`.

func (CortexCloudSdkError) ToPrettyJSON

func (e CortexCloudSdkError) ToPrettyJSON() (string, error)

ToPrettyJSON serializes the CortexCloudSdkError into a JSON string with indentation. It excludes the `Err` field.

func (*CortexCloudSdkError) Unwrap

func (e *CortexCloudSdkError) Unwrap() error

Unwrap returns the underlying Go error, allowing for error chain inspection using errors.Unwrap from the standard library.

type CortexCloudSdkErrorDetail

type CortexCloudSdkErrorDetail struct {
	Location string `json:"location"`         // The path or field where the error occurred (e.g., "body.user_id").
	Code     string `json:"code"`             // A specific code for this detail (e.g., "INVALID_FORMAT").
	Message  string `json:"message"`          // A human-readable message for this specific detail.
	Error    error  `json:"underlying_error"` // The underlying Go error.
}

CortexCloudSdkErrorDetail provides granular context about specific error instances, often used for validation errors or specific problem details within the main error.

func NewInvalidEnumValidationErrorDetail

func NewInvalidEnumValidationErrorDetail(err error, location, fieldName string, value any, enums []string) CortexCloudSdkErrorDetail

func NewMinimumNumberOfValuesValidationErrorDetail

func NewMinimumNumberOfValuesValidationErrorDetail(err error, location, fieldName string, expected string, actual int) CortexCloudSdkErrorDetail

func NewRequiredValidationErrorDetail

func NewRequiredValidationErrorDetail(err error, location, fieldName string) CortexCloudSdkErrorDetail

func NewUnexpectedValidationErrorDetail

func NewUnexpectedValidationErrorDetail(err error, location string) CortexCloudSdkErrorDetail

func NewUnknownValidationTagErrorDetail

func NewUnknownValidationTagErrorDetail(err error, location, tag string) CortexCloudSdkErrorDetail

type ErrorExtraField added in v1.0.1

type ErrorExtraField struct {
	// contains filtered or unexported fields
}

ErrorExtraField handles both string and array formats for err_extra field

func (ErrorExtraField) MarshalJSON added in v1.0.1

func (e ErrorExtraField) MarshalJSON() ([]byte, error)

MarshalJSON implements custom JSON marshaling

func (*ErrorExtraField) UnmarshalJSON added in v1.0.1

func (e *ErrorExtraField) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling to handle both string and array formats

func (ErrorExtraField) Values added in v1.0.1

Values returns the underlying slice of error extra objects

Jump to

Keyboard shortcuts

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