clierrors

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2023 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

clierrors defines error types that are useful in the CLI for customizing the experience when a failure occurs. This package is named clierrors to avoid a name collision with the standard library errors package. Code that needs to handle errors will need to import both packages.

Most error handling code in the CLI should use the clierrors.Message function to return user-friendly error messages.

For "expected" error cases that can be classified, use clierrors.MessageWithCause to return a user-friendly error message and wrap the original error.

For "unexpected" or "unclassified" error cases the error should be returned as-is. This will result in a generic error experience for the user that encourages them to file a bug report.

Types in other packages can also implement the FriendlyError interface to give their error types special handling. This removes the need for error handling at other levels of the code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsFriendlyError

func IsFriendlyError(err error) bool

IsFriendlyError returns true if the error should be handled gracefully by the CLI.

Types

type ErrorMessage

type ErrorMessage struct {
	// Message is the error message.
	Message string

	// Cause is the root cause of the error. If provided it will be included in the message displayed to users.
	Cause error
}

ErrorMessage represents a basic error message that can be returned by the CLI.

func Message

func Message(message string, args ...any) *ErrorMessage

Message returns a new ErrorMessage with the given message. The message can be formatted with args.

func MessageWithCause

func MessageWithCause(cause error, message string, args ...any) *ErrorMessage

Message returns a new ErrorMessage with the given message and cause. The message can be formatted with args.

func (*ErrorMessage) Error

func (e *ErrorMessage) Error() string

Error returns the error message for the error.

func (*ErrorMessage) IsFriendlyError

func (*ErrorMessage) IsFriendlyError() bool

IsFriendlyError returns true for ErrorMessage. These errors are always handled gracefully by the CLI.

func (*ErrorMessage) Unwrap

func (e *ErrorMessage) Unwrap() error

Unwrap returns the cause of the error.

type FriendlyError

type FriendlyError interface {
	error

	// IsFriendlyError returns true if the error should be handled gracefully by the CLI.
	IsFriendlyError() bool
}

FriendlyError defines an interface for errors that should be gracefully handled by the CLI and display a friendly error message to the user.

Jump to

Keyboard shortcuts

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