errors

package
v0.2.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	HttpMap = map[Status]int{
		StatusConflict:           http.StatusConflict,
		StatusInternal:           http.StatusInternalServerError,
		StatusInvalidArgument:    http.StatusBadRequest,
		StatusNotFound:           http.StatusNotFound,
		StatusNotImplemented:     http.StatusNotImplemented,
		StatusUnauthenticated:    http.StatusUnauthorized,
		StatusUnauthorized:       http.StatusForbidden,
		StatusPreconditionFailed: http.StatusPreconditionFailed,
		StatusAborted:            http.StatusInternalServerError,
	}
)

Functions

func As

func As(err error, target any) bool

As finds the first error in err's tree that matches target, and if one is found, sets target to that error value and returns true. Otherwise, it returns false.

func Detail

func Detail[T any](err error) (detail *T)

Detail returns generic type stored in details.

func HttpStatus

func HttpStatus(err error) int

Http returns http status.

func Is

func Is(err error, target error) bool

Is reports whether any error in err's tree matches target.

func IsAborted

func IsAborted(err error) bool

IsAborted checks if err is aborted error.

func IsConflict

func IsConflict(err error) bool

IsConflict checks if err is conflict error.

func IsInternal

func IsInternal(err error) bool

IsInternal checks if err is internal error.

func IsInvalidArgument

func IsInvalidArgument(err error) bool

IsInvalidArgument checks if err is invalid argument error.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound checks if err is not found error.

func IsNotImplemented

func IsNotImplemented(err error) bool

IsNotImplemented checks if err is not implemented error.

func IsPreconditionFailed

func IsPreconditionFailed(err error) bool

IsPreconditionFailed checks if err is precondition failed error.

func IsStatus

func IsStatus(err error) bool

IsStatus checks if err is Error type.

func IsUnauthenticated

func IsUnauthenticated(err error) bool

IsUnauthenticated checks if err is unauthenticated error.

func IsUnauthorized

func IsUnauthorized(err error) bool

IsUnauthorized checks if err is unauthorized error.

func Join

func Join(errs ...error) error

Join returns an error that wraps the given errors.

func Message

func Message(err error) string

Message unwraps an error and returns its message.

func New

func New(text string) error

New returns an error that formats as the given text. Each call to New returns a distinct error value even if the text is identical.

func Source

func Source(err error) error

func Unwrap

func Unwrap(err error) error

Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.

Types

type Error

type Error struct {
	// Source error
	Err error

	// Machine-readable status code.
	Status Status

	// Human-readable error message.
	Message string
	// contains filtered or unexported fields
}

func Aborted

func Aborted(format string, args ...interface{}) *Error

Aborted is a helper function to return aborted error status.

func AsError

func AsError(err error) (e *Error)

AsError return err as Error.

func Conflict

func Conflict(format string, args ...interface{}) *Error

Conflict is a helper function to return an conflict Error.

func Format

func Format(code Status, format string, args ...interface{}) *Error

Format is a helper function to return an Error with a given status and formatted message.

func Internal

func Internal(format string, args ...interface{}) *Error

Internal is a helper function to return an internal Error.

func InvalidArgument

func InvalidArgument(format string, args ...interface{}) *Error

InvalidArgument is a helper function to return an invalid argument Error.

func NotFound

func NotFound(format string, args ...interface{}) *Error

NotFound is a helper function to return an not found Error.

func NotImplemented

func NotImplemented(format string, args ...interface{}) *Error

NotImplemented is a helper function to return an not found Error.

func PreconditionFailed

func PreconditionFailed(format string, args ...interface{}) *Error

PreconditionFailed is a helper function to return an precondition failed error.

func Unauthenticated

func Unauthenticated(format string, args ...interface{}) *Error

Unauthenticated is a helper function to return unauthenticated error status.

func Unauthorized

func Unauthorized(format string, args ...interface{}) *Error

Unauthorized is a helper function to return unauthorized error status.

func (*Error) Detail

func (e *Error) Detail(arg any) *Error

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface.

func (*Error) Http

func (e *Error) Http() int

Http returns http status based on error status.

func (*Error) Source

func (e *Error) Source(err error) *Error

Source sets the origin err and return error.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap error and return source error.

type Status

type Status string
const (
	StatusConflict           Status = "conflict"
	StatusInternal           Status = "internal"
	StatusInvalidArgument    Status = "invalid"
	StatusNotFound           Status = "not_found"
	StatusNotImplemented     Status = "not_implemented"
	StatusUnauthenticated    Status = "unauthenticated"
	StatusUnauthorized       Status = "unauthorized"
	StatusPreconditionFailed Status = "precondition_failed"
	StatusAborted            Status = "aborted"
)

func AsStatus

func AsStatus(err error) Status

AsStatus unwraps an error and returns its code. Non-application errors always return StatusInternal.

Jump to

Keyboard shortcuts

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