You are viewing development documentation built from the latest commit on main (last tag v0.2.0-rc.2, source commit 25e1a892).
APIs and features are subject to change.
Errors
The errors package provides structured error types with contextual information for Kubernetes resource operations. Most Kure packages return these errors in place of fmt.Errorf; a few, such as pkg/stack’s builders, still call 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
| Type | Use Case | Key Fields |
|---|---|---|
ValidationError | Field validation failures | Field, Value, Component, ValidValues |
ResourceError | Resource-specific issues | ResourceType, Name, Namespace, Available |
PatchError | Patch operation failures | Operation, Path, ResourceName |
ParseError | File/YAML parsing errors | Source, Line, Column |
FileError | File system operations | Operation, Path |
ConfigError | Configuration problems | Source, Field, ValidValues |
PSAViolationError | Pod Security Standards violations | Field, Level |
Every type embeds BaseError, which carries the message, the cause and the suggestion, and
reports one of the ErrorType constants (ErrorTypeValidation, ErrorTypeResource,
ErrorTypePatch, ErrorTypeParse, ErrorTypeFile, ErrorTypeConfiguration, ErrorTypePSA,
ErrorTypeInternal). ParseErrors is not one of these types: it aggregates the errors found
while decoding YAML (pkg/io) and unwraps to each of them.
Usage
Each Go block in this section is the body of an Example function in example_test.go, which
go test runs: it imports this package as errors, and fmt for the lines that print the errors
the example built. In your own code, return these errors rather than print them. Wrap and
Wrapf return nil for a nil error, so they need no if err != nil guard.
Wrapping Errors
Creating Errors
Typed Errors
Suggestions for a FileError
NewFileError chooses its suggestion from the kind of the cause, found with errors.Is, and never
from the cause’s text: fs.ErrPermission (permissions), fs.ErrNotExist (the path), syscall.EISDIR
(a directory where a file is wanted) and syscall.ENOSPC or syscall.EDQUOT (disk space or quota).
The errors the os package returns are of these kinds, wrapped or not. On Windows a full disk
comes back as a native error code, 112 (ERROR_DISK_FULL) or 39 (ERROR_HANDLE_DISK_FULL), and
those two are matched there too; on other platforms the same numbers mean other errors and are not.
A cause that only says “permission denied” in words, or a nil cause, gets the default suggestion,
“Check file path and permissions”.
Inspecting Errors
IsKureError, GetKureError and IsType look through wrapping, so they find a typed error
anywhere in the chain:
Predefined Errors
A small set of sentinels is predefined, one for each error Kure code actually returns, so callers
can match them with the standard library’s errors.Is:
This package does not re-export Is or As, so import both packages under distinct names:
The block below is the body of an Example function in example_sentinel_test.go, which
go test runs: it imports the two packages as above, github.com/go-kure/kure/pkg/kubernetes for
a function that returns a sentinel, and fmt.
A sentinel that no Kure code returns is removed rather than kept exported: callers could compare
against it, but nothing would ever produce it. TestExportedSentinelsHaveProducers fails the build
if an exported Err* sentinel, declared in any file of this package, has no producer outside it: a
non-test reference through an import of this package. A comparison (errors.Is/errors.As
argument, ==/!= operand, switch tag or case) is not a producer, and neither is an assignment
to the blank identifier (var _ = errors.ErrX). The check reads the source, so any other mention,
such as a sentinel stored in a variable and only then compared, or passed to a logger, still counts
as produced.
Related Packages
Kure packages import this package to build the errors they return, in place of fmt.Errorf. Use it the same way when your code returns an error to a Kure caller.