errors

package
v0.0.0-...-ea047a2 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeError

func MakeError(actualError, underlyingError error) error

MakeError creates a new error and includes the underlyingError in the message. However, it does not expose/wrap the underlyingError.

Following the recommendation of https://go.dev/blog/go1.13-errors, the actualError is encapsulated into a new error and not returned directly. This forces callers to use errors.Is(err, pkg.ErrPermission) instead of err == pkg.ErrPermission { … }.

Example
package main

import (
	"errors"
	"fmt"

	pkgerrors "github.com/kyma-project/kyma/components/eventing-controller/pkg/errors"
)

func main() {
	actualError := errors.New("failed to connect to NATS")
	underlyingError := errors.New("some error coming from the NATS package")
	fmt.Println(pkgerrors.MakeError(actualError, underlyingError))
}
Output:

failed to connect to NATS: some error coming from the NATS package

Types

type ArgumentError

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

ArgumentError is a generic error which can be used to create errors that shall include any kind of argument in their Error() message. By using the ArgumentError you don't need to create a custom error for each of these errors that have this custom argument. Instead, a custom error can be defined as a variable once. The equality of two ArgumentError is defined through the equality of errorFormatType. errorFormatType is supposed to be a format string so that the argument provided with WithArg can be printed in Error().

See ExampleArgumentError for an example.

Example
package main

import (
	"fmt"

	pkgerrors "github.com/kyma-project/kyma/components/eventing-controller/pkg/errors"
)

var errInvalidStorageType = pkgerrors.NewArgumentError("invalid stream storage type: %q")

func main() {
	e := errInvalidStorageType
	e = e.WithArg("some storage type")

	fmt.Println(e.Error())
}
Output:

invalid stream storage type: "some storage type"

func NewArgumentError

func NewArgumentError(errorFormatType string) *ArgumentError

NewArgumentError creates a new ArgumentError.

func (*ArgumentError) Error

func (e *ArgumentError) Error() string

Error returns a human-readable string representation of the error.

func (*ArgumentError) Is

func (e *ArgumentError) Is(target error) bool

Is defines the equality of an ArgumentError. Two ArgumentError are equal if their errorFormatType is equal. Use as following:

var errInvalidStorageType = ArgumentError{errorFormatType: "invalid stream storage type: %q"} ok := errors.Is(err, &errInvalidStorageType).

func (*ArgumentError) WithArg

func (e *ArgumentError) WithArg(argument string) *ArgumentError

WithArg shall be used to provide the argument which will be printed according to the format string provided in errorFormatType via NewArgumentError.

Jump to

Keyboard shortcuts

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