errors

package
v0.33.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultLinesBefore the number of lines before the error line that are printed.
	DefaultLinesBefore = 5
	// DefaultLinesAfter the number of lines after the error line that are printed.
	DefaultLinesAfter = 5
	// DefaultLineNumberPrefixLength is the fixed width of the code line prefix containing the line number.
	DefaultLineNumberPrefixLength = 6
)

Variables

View Source
var ErrInvalidUserInput = errors.New("invalid user input")

ErrInvalidUserInput indicates invalid user input.

Functions

func CreateSourceSnippet added in v0.32.0

func CreateSourceSnippet(errorLine, errorColumn int, source []string, linesBefore, linesAfter, lineNumberPrefixLength int) string

CreateSourceSnippet creates an excerpt of lines of source code, containing some lines before and after the error line. The error line and column will be highlighted and looks like this: 14: updateStrategy: patch 15: 16: name: test 17: namespace: {{ .imports.namespace }}

ˆ≈≈≈≈≈≈≈

18: 19: exportsFromManifests: 20: - key: ingressClass

func IgnoreInvalidUserInput added in v0.29.0

func IgnoreInvalidUserInput(err error) error

IgnoreUserInputError returns nil on invalid user input.

Types

type ErrorWithReason

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

ErrorWithReason wraps an error and adds a reason to it. The reason is meant to be a CamelCased, machine-readable, enum-like string. Use WithReason(err, reason) to wrap a normal error into an *ErrorWithReason.

func (*ErrorWithReason) Reason

func (e *ErrorWithReason) Reason() string

Reason returns the reason for this error.

type ReasonableError

type ReasonableError interface {
	error
	Reason() string
}

ReasonableError enhances an error with a reason. The reason is meant to be a CamelCased, machine-readable, enum-like string. Use WithReason(err, reason) to wrap a normal error into an ReasonableError.

func Errorf

func Errorf(format string, err ReasonableError, a ...any) ReasonableError

Errorf works similarly to fmt.Errorf, with the exception that it requires an ErrorWithReason as second argument and returns nil if that one is nil. Otherwise, it calls fmt.Errorf to construct an error and wraps it in an ErrorWithReason, using the reason from the given error. This is useful for expanding the error message without losing the reason.

func Join

func Join(errs ...error) ReasonableError

Join joins multiple errors into a single one. Returns nil if all given errors are nil. This is equivalent to NewErrorList(errs...).Aggregate().

func WithReason

func WithReason(err error, reason string) ReasonableError

WithReason wraps an error together with a reason into ErrorWithReason. The reason is meant to be a CamelCased, machine-readable, enum-like string. If the given error is nil, nil is returned.

type ReasonableErrorList

type ReasonableErrorList struct {
	Errs    []error
	Reasons []string
}

ReasonableErrorList is a helper struct for situations in which multiple errors (with or without reasons) should be returned as a single one.

func NewReasonableErrorList

func NewReasonableErrorList(errs ...error) *ReasonableErrorList

NewReasonableErrorList creates a new *ErrorListWithReasons containing the provided errors.

func (*ReasonableErrorList) Aggregate

func (el *ReasonableErrorList) Aggregate() ReasonableError

Aggregate aggregates all errors in the list into a single ErrorWithReason. Returns nil if the list is either nil or empty. If the list contains a single error, that error is returned. Otherwise, a new error is constructed by appending all contained errors' messages. The reason in the returned error is the first reason that was added to the list, or the empty string if none of the contained errors was an ErrorWithReason.

func (*ReasonableErrorList) Append

func (el *ReasonableErrorList) Append(errs ...error) *ReasonableErrorList

Append appends all given errors to the ErrorListWithReasons. This modifies the receiver object. If a given error is of type ErrorWithReason, its reason is added to the list of reasons. nil pointers in the arguments are ignored. Returns the receiver for chaining.

func (*ReasonableErrorList) Reason

func (el *ReasonableErrorList) Reason() string

Reason returns the first reason from the list of reasons contained in this error list. If the list is nil or no reasons are contained, the empty string is returned. This is equivalent to el.Aggregate().Reason(), except that it also works for an empty error list.

type TemplateError added in v0.32.0

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

TemplateError wraps a go templating error and adds more human-readable information.

func TemplateErrorBuilder added in v0.32.0

func TemplateErrorBuilder(err error) *TemplateError

TemplateErrorBuilder creates a new TemplateError.

func (*TemplateError) Build added in v0.32.0

func (e *TemplateError) Build() *TemplateError

Build builds the error message.

func (*TemplateError) Error added in v0.32.0

func (e *TemplateError) Error() string

Error returns the error message.

func (*TemplateError) WithFormattedOutput added in v0.32.0

func (e *TemplateError) WithFormattedOutput(output *string) *TemplateError

WithFormattedOutput adds the rendered template output to the error. Used when the failure occurs after successful Go-template execution (e.g. the generated YAML is syntactically invalid or does not match the expected schema) - the user cannot otherwise see the generated document, so surface a snippet around the failing line.

func (*TemplateError) WithInput added in v0.32.0

func (e *TemplateError) WithInput(input map[string]any, inputFormatter *TemplateInputFormatter) *TemplateError

WithInput adds the template input with a formatter to the error.

func (*TemplateError) WithSource added in v0.32.0

func (e *TemplateError) WithSource(source *string) *TemplateError

WithSource adds the template source code to the error.

func (*TemplateError) WithSourceSnippetFormat added in v0.32.0

func (e *TemplateError) WithSourceSnippetFormat(linesBefore, linesAfter, lineNumberPrefixLength int) *TemplateError

WithSourceSnippetFormat adds the number of lines before and after the error line that are printed in the source code snippet. The lineNumberPrefixLength specifies the total length of the line number prefix, consisting of the line number, a colon, and then whitespace to align the code.

type TemplateInputFormatter added in v0.32.0

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

The TemplateInputFormatter formats the inputs of a template in a human-readable format.

func NewTemplateInputFormatter added in v0.32.0

func NewTemplateInputFormatter(prettyPrint bool, sensitiveKeys ...string) *TemplateInputFormatter

NewTemplateInputFormatter creates a new template input formatter. When prettyPrint is set to true, the json output will be formatted with easier readable indentation. The parameter sensitiveKeys can contain template input keys which may contain sensitive data. When such a key is encountered during formatting, the values of the respective key will be removed.

func (*TemplateInputFormatter) Format added in v0.32.0

func (f *TemplateInputFormatter) Format(input map[string]interface{}, prefix string) string

Format formats the template input into a string value. The given prefix is prepended to each line of the formatted output.

Jump to

Keyboard shortcuts

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