Skip to main content

Errors

Go Reference Go Reference

The errors package provides structured error types and thin wrapping helpers used across launcher. Application code is meant to use these helpers instead of calling fmt.Errorf directly, so that error wrapping is consistent and machine-inspectable.

Structured error types

ValidationError is returned when a semantic constraint is violated. It serves three shapes:

  • Enum errors (unknown value): populate Value + ValidValues via NewValidationError(field, value, component, validValues).
  • Custom-message errors (missing field, bad format): populate Message.
  • Wrapping errors: Cause holds the error being reported and Unwrap returns it, so errors.Is and errors.As see through the ValidationError. An unresolved type reference, for example, matches oam.ErrTypeReferenceUnknown.

ParseError is returned when a document fails YAML parsing; it carries Kind, File, Line, Column, and the wrapped cause (NewParseError(...)), and supports errors.Unwrap.

PatchError is returned when a patch operation (pkg/patch) fails to apply. It carries Operation, Path, ResourceName, Reason, and the wrapped Cause (NewPatchError(operation, path, resourceName, reason, cause)), and supports errors.Unwrap.

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

return errors.NewValidationError("type", "frobnicate", "webservice",
    []string{"webservice", "worker", "cronjob"})

var ve *errors.ValidationError
if errors.As(err, &ve) {
    // inspect ve.Field, ve.ValidValues, …
}

Wrapping helpers

return errors.Wrap(err, "reading application file")
return errors.Wrapf(err, "accessing %q", path)
return errors.New("description of error")
return errors.Errorf("invalid value: %s", val)

Wrap/Wrapf return nil when the input error is nil, so they are safe to use unconditionally.

API overview

SymbolPurpose
ValidationError, NewValidationErrorSemantic constraint violations (enum, custom, or wrapping a cause).
ParseError, NewParseErrorYAML parse failures with location.
PatchError, NewPatchErrorPatch operations (pkg/patch) that fail to apply.
New, ErrorfCreate plain/formatted errors.
Wrap, WrapfWrap an error with context (nil-safe).
Is, Aserrors.Is/errors.As passthroughs for inspection.

The package comment in doc.go gives the same summary on pkg.go.dev.