errors

package
v0.0.0-...-a131ce6 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Example
err := getHandler()
// Note: You can also use "%+v" to show the full stack trace about error
fmt.Printf("%v\n", err)
Output:

alice@example.com: getHandler: >> businessLogic: counter value = 3: >> getFromDB: kind database error: >> sql: no rows in result set

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target interface{}) bool

As is just a wrapper of pkg/errors.As.

func E

func E(args ...interface{}) error

E builds an error value from its arguments. There must be at least one argument or E panics. The type of each argument determines its meaning. If more than one argument of a given type is presented, only the last one is recorded.

The types are:

Op
	The operation of the function who make a call to other function that returns an error.
	E.g. taskUsecase.GetAll.
UserEmail
	The email of the user attempting the operation.
string
	Treated as an error message.
errors.Kind
	The class of error, such as internal server error, record not found, ...etc.
error
	The underlying error that triggered this one.

If the error is printed, only those items that have been set to non-zero values will appear in the result.

If Kind is not specified or Other, we set it to the Kind of the underlying error.

func Is

func Is(err, target error) bool

Is is just a wrapper of pkg/errors.Is.

func KindIs

func KindIs(err error, kind Kind) bool

KindIs returns whether err.Kind == kind. err must has the type *Error or Error, if not, we panic. If there are multiple kind specified in error chain, we take the outer-most error kind.

func New

func New(text string) error

New is the wrapper which calls pkg/errors.New().

Types

type Error

type Error struct {
	// Op is the operation being performed, usually the name of the method
	// being invoked (userUsecase.Get, tokenRepo.Insert, ...etc.).
	Op Op
	// UserEmail is the email of the user attempting the operation.
	UserEmail UserEmail
	// Kind is the class of error, such as Record not found,
	// or "Other" if its class is unknown or irrelevant.
	Kind Kind
	// Msg is some additional information you want to add.
	Msg Msg
	// The underlying error that triggered this one, if any.
	Err error
}

Error is the type that implements the error interface. It contains a number of fields, each of different type. An Error value may leave some values unset.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Format

func (e *Error) Format(s fmt.State, verb rune)

Format provide Format method to satisfy fmt.Formatter interface, it is used by function like fmt.Printf with verb like: '%+v' to display stack trace of a pkg/error Error type.

func (*Error) StackTrace

func (e *Error) StackTrace() errors.StackTrace

StackTrace() allow us to print the stacktrace message by calling e.Err 's StackTrace() method, if e.Err is nil or e.Err is not a stacktracer, we just return nil.

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Kind

type Kind uint8

Kind defines the kind of error this is.

const (
	KindOther              Kind = iota // Unclassified error. This value is not printed in the error message.
	KindRecordNotFound                 // Record not found when we request some resource in database.
	KindDuplicateEmail                 // Duplicate Email error.
	KindEditConflict                   // Edit conflict while manipulating database.
	KindInvalidCredentials             // Edit conflict while manipulating database.
	KindFailedValidation               //  Failed validation error.
	KindInternal                       // Internal server error.
	KindDatabase                       // Error happened while querying database, this should be treated as subset of internal error and logged it carefully.
)

Kinds of errors.

The values of the error kinds are common between both clients and servers.

func (Kind) String

func (k Kind) String() string

type Msg

type Msg string

func (Msg) Format

func (m Msg) Format(a ...interface{}) Msg

Format formats according to a format specifier and return formatted string.

Example
cnt := 3
e := E(Msg("counter value = %d").Format(cnt), New("something goes wrong"))
fmt.Printf("%v", e)
Output:

counter value = 3: >> something goes wrong

func (Msg) String

func (m Msg) String() string

type Op

type Op string

Op describes an operation, usually as the package and method, such as "tokenRepo.Insert", or "userUsecase.GetByEmail".

func (Op) String

func (o Op) String() string

type UserEmail

type UserEmail string

UserEmail is a string representing a user

func (UserEmail) String

func (u UserEmail) String() string

Jump to

Keyboard shortcuts

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