Exception: Kube::ValidationError

Inherits:
Error
  • Object
show all
Defined in:
lib/kube/errors.rb

Overview

Raised by Resource#valid! when data fails schema validation.

Produces detailed, human-readable error messages that identify the exact key path and offending value, followed by the resource YAML with error lines highlighted in red and missing keys injected inline.

Schema validation failed for Deployment "web":
  - spec.selector is required but missing
  - spec.replicas = "not_a_number" — expected integer, got String

---
apiVersion: apps/v1
kind: Deployment
spec:
  selector:        # MISSING — required
  replicas: "nah"  # expected integer, got String

Constant Summary collapse

RED =
"\033[31m"
YELLOW =
"\033[33m"
RESET =
"\033[0m"
BOLD =
"\033[1m"
UNDERLINE =
"\033[4m"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errors, kind: nil, name: nil, manifest: nil) ⇒ ValidationError

Returns a new instance of ValidationError.

Parameters:

  • errors (Array<Hash>)

    raw JSONSchemer error hashes

  • kind (String, nil) (defaults to: nil)

    Kubernetes resource kind (e.g. “Deployment”)

  • name (String, nil) (defaults to: nil)

    resource metadata.name (e.g. “web”)

  • manifest (Hash, nil) (defaults to: nil)

    the full resource data that failed validation



46
47
48
49
50
51
52
# File 'lib/kube/errors.rb', line 46

def initialize(errors, kind: nil, name: nil, manifest: nil)
  @errors   = errors
  @kind     = kind
  @name     = name
  @manifest = manifest
  super(build_message)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



33
34
35
# File 'lib/kube/errors.rb', line 33

def errors
  @errors
end

Instance Method Details

#detailed_message(highlight: false) ⇒ Object

Ruby 3.2+ calls detailed_message for uncaught exceptions. We override it so the message ends at column 0, preventing Ruby from indenting the backtrace to match YAML indentation.



57
58
59
60
61
62
63
64
65
# File 'lib/kube/errors.rb', line 57

def detailed_message(highlight: false, **)
  out = "\n#{BOLD}#{UNDERLINE}#{RED}#{header_line}#{RESET}\n\n"
  out += error_lines.join("\n")
  if @manifest
    out += "\n\n#{annotate_manifest(@manifest, @errors)}"
  end
  out += "\n\n"
  out
end