errors

package
v0.3.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	HttpMap = map[Code]int{
		CodeConflict:           http.StatusConflict,
		CodeInternal:           http.StatusInternalServerError,
		CodeInvalidArgument:    http.StatusBadRequest,
		CodeNotFound:           http.StatusNotFound,
		CodeNotImplemented:     http.StatusNotImplemented,
		CodeUnauthenticated:    http.StatusUnauthorized,
		CodeUnauthorized:       http.StatusForbidden,
		CodePreconditionFailed: http.StatusPreconditionFailed,
		CodeAborted:            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 Status 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

Source read status error source.

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 Code added in v0.3.0

type Code string
const (
	CodeConflict           Code = "conflict"
	CodeInternal           Code = "internal"
	CodeInvalidArgument    Code = "invalid"
	CodeNotFound           Code = "not_found"
	CodeNotImplemented     Code = "not_implemented"
	CodeUnauthenticated    Code = "unauthenticated"
	CodeUnauthorized       Code = "unauthorized"
	CodePreconditionFailed Code = "precondition_failed"
	CodeAborted            Code = "aborted"
)

func AsCode added in v0.3.0

func AsCode(err error) Code

AsCode unwraps an error and returns its code. Non-application errors always return CodeInternal.

type Status

type Status struct {
	// Source error
	Err error `json:"source_error,omitempty"`

	// Machine-readable status code.
	Code Code `json:"code"`

	// Human-readable error message.
	Message string `json:"message"`

	// Payload
	Payload any `json:"detail,omitempty"`
}

func Aborted

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

Aborted is a helper function to return aborted error status.

func AsStatus

func AsStatus(err error) (e *Status)

AsStatus return err as Status rror.

func Conflict

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

Conflict is a helper function to return an conflict Error.

func Format

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

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

func Internal

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

Internal is a helper function to return an internal Error.

func InvalidArgument

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

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

func NotFound

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

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

func NotImplemented

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

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

func PreconditionFailed

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

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

func Unauthenticated

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

Unauthenticated is a helper function to return unauthenticated error status.

func Unauthorized

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

Unauthorized is a helper function to return unauthorized error status.

func (*Status) Detail added in v0.3.0

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

func (*Status) Error added in v0.3.0

func (e *Status) Error() string

Error implements the error interface.

func (*Status) Http added in v0.3.0

func (e *Status) Http() int

Http returns http status code mapped to error status code.

func (*Status) Source added in v0.3.0

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

Source sets the origin err and return error.

func (*Status) Unwrap added in v0.3.0

func (e *Status) Unwrap() error

Unwrap status error and return source error.

Jump to

Keyboard shortcuts

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