⚠️ Work in Progress
Kure is currently under active development and has not been released yet. APIs and features are subject to change.

Errors

Errors - Structured Error Handling

The errors package provides structured error types with contextual information for Kubernetes resource operations. All Kure packages use this instead of fmt.Errorf.

Overview

Errors in Kure carry context: the type of error, what resource was affected, suggestions for fixing the problem, and the original cause. This makes debugging easier and enables programmatic error handling.

Error Types

TypeUse CaseKey Fields
ValidationErrorField validation failuresField, Value, ValidValues, Suggestion
ResourceErrorResource-specific issuesKind, Name, Namespace, Available
PatchErrorPatch operation failuresOperation, Path, ResourceName
ParseErrorFile/YAML parsing errorsSource, Line, Column
FileErrorFile system operationsOperation, Path
ConfigErrorConfiguration problemsSource, Field, Value, ValidValues

Usage

Wrapping Errors

import "github.com/go-kure/kure/pkg/errors"

// Wrap with context
if err != nil {
    return errors.Wrap(err, "failed to load cluster config")
}

// Wrap with formatted message
return errors.Wrapf(err, "resource %s/%s not found", kind, name)

Creating Errors

// Simple error
return errors.New("invalid configuration")

// Formatted error
return errors.Errorf("unknown generator: %s", name)

Typed Errors

// Validation error with suggestion
return errors.NewValidationError(
    "replicas",          // field
    "-1",                // value
    "Deployment",        // component
    []string{"1", "3"},  // valid values
)

// Resource not found
return errors.ResourceNotFoundError(
    "Deployment",                    // resource type
    "my-app",                        // name
    "default",                       // namespace
    []string{"web-app", "api-app"},  // available resources
)

// Patch error
return errors.NewPatchError(
    "set",                           // operation
    "spec.replicas",                 // path
    "my-deployment",                 // resource name
    "field not found",               // reason
    originalErr,                     // cause
)

// Parse error with location
return errors.NewParseError(
    "config.yaml",   // source file
    "invalid YAML",  // reason
    42,              // line
    10,              // column
    originalErr,     // cause
)

// File error
return errors.NewFileError("read", "/path/to/file", "permission denied", originalErr)

// Configuration error
return errors.NewConfigError(
    "mise.toml",                    // source
    "go",                           // field
    "1.21",                         // value
    "version too old",              // reason
    []string{"1.23", "1.24"},       // valid values
)

Inspecting Errors

// Check if error is a Kure error
if errors.IsKureError(err) {
    kErr := errors.GetKureError(err)
    fmt.Println(kErr.Type())
    fmt.Println(kErr.Suggestion())
}

// Check specific error type
if errors.IsType(err, errors.ErrorTypeValidation) {
    // Handle validation error
}

Predefined Errors

Common nil-resource errors are predefined for use throughout Kure:

errors.ErrNilDeployment
errors.ErrNilService
errors.ErrNilConfigMap
errors.ErrNilSecret
errors.ErrNilBundle
// ... and more for each resource type

File and GVK errors:

errors.ErrFileNotFound
errors.ErrDirectoryNotFound
errors.ErrInvalidPath
errors.ErrGVKNotFound
errors.ErrGVKNotAllowed
errors.ErrNilObject

All Kure packages import this package for error handling. Never use fmt.Errorf directly.