errors

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2022 License: MIT Imports: 4 Imported by: 5

README ¶

An errors package for web development


elliotxx/errors is a golang package for web development, inspired by pkg/errors!

📜 Language

English | 简体中文

âš¡ Usage

go get -u github.com/elliotxx/errors

✨ Features

  • More detailed error information, e.g. error code, message, cause, stack trace
  • More powerful API
  • Lightweight

📚 Examples

Example 1:

package errcodes

import "github.com/elliotxx/errors"

var (
	Success                    = errors.NewErrorCode("00000", "success")
	NotFound                   = errors.NewErrorCode("A0100", "not found")
	AccessPermissionError      = errors.NewErrorCode("A0200", "abnormal access permission")
	AbnormalUserOperation      = errors.NewErrorCode("A0300", "abnormal user operation")
	InvalidParams              = errors.NewErrorCode("A0400", "invalid params")
	BlankRequiredParams        = errors.NewErrorCode("A0401", "required parameter is blank")
	MalformedParams            = errors.NewErrorCode("A0403", "parameter format mismatch")
	ServerError                = errors.NewErrorCode("A0500", "server error")
	TooManyRequests            = errors.NewErrorCode("A0501", "too many requests")
	ConcurrentExceedLimit      = errors.NewErrorCode("A0502", "the request parallel number exceeds the limit")
	WaitUserOperation          = errors.NewErrorCode("A0503", "please wait for user operation")
	RepeatedRequest            = errors.NewErrorCode("A0504", "repeated request")
)

Example 2:

detailErr := New("name is empty")
// detailErr.Error() returns "cause [name is empty]"

ErrBlankParameter := NewErrorCode("A0401", "required parameter is blank")
// ErrBlankParameter.Error() returns "code [A0401], msg [required parameter is blank]"

detailErr = NewDetailError("A0401", "required parameter is blank", fmt.Errorf("name is empty"))
// detailErr.Error() returns "code [A0401], msg [required parameter is blank], cause [name is empty]"

detailErr = Errorf("%s is not exist", "John")
// detailErr.Error() returns "cause [John is not exist]"

detailErr = Code("A0401").Msg("required parameter is blank").Cause(fmt.Errorf("name is empty"))
// detailErr.Error() returns "code [A0401], msg [required parameter is blank], cause [name is empty]"

detailErr = Code("A0401").Msg("required parameter is blank")
// detailErr.Error() returns "code [A0401], msg [required parameter is blank]"

detailErr = Code("A0401").Cause(fmt.Errorf("name is empty"))
// detailErr.Error() returns "code [A0401], cause [name is empty]"

detailErr = Code("A0401").Causef("name is empty")
// detailErr.Error() returns "code [A0401], cause [name is empty]"

detailErr = Code("A0401").Causef("%s is empty", "name")
// detailErr.Error() returns "code [A0401], cause [name is empty]"

rootCause := fmt.Errorf("name is empty")
detailErr = Code("A0401").Causewf(rootCause, "failed to validate parameter")
// detailErr.Error() returns "code [A0401], cause [failed to validate parameter: name is empty]"

detailErr = Msg("required parameter is blank")
// detailErr.Error() returns "msg [required parameter is blank]"

detailErr = ErrBlankParameter.Cause(fmt.Errorf("name is empty"))
// detailErr.Error() returns "code [A0401], msg [required parameter is blank], cause [name is empty]"

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

func As ¶

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

func InvalidErrorCode ¶

func InvalidErrorCode(code string) bool

func Is ¶

func Is(err, target error) bool

func Unwrap ¶

func Unwrap(err error) error

func Wrap ¶

func Wrap(err error, message string) error

Wrap returns an error annotating err with a stack trace at the point Wrap is called, and the supplied message. If err is nil, Wrap returns nil.

func Wrapf ¶

func Wrapf(err error, format string, args ...interface{}) error

Wrapf returns an error annotating err with a stack trace at the point Wrapf is called, and the format specifier. If err is nil, Wrapf returns nil.

func Zero ¶ added in v0.2.1

func Zero(e DetailError) bool

Types ¶

type DetailError ¶

type DetailError interface {
	error
	fmt.Formatter

	GetCode() string
	GetMsg() string
	GetCause() error
	GetErrorCode() ErrorCode

	Code(code string) DetailError
	Msg(msg string) DetailError
	Cause(err error) DetailError
	Causef(format string, args ...interface{}) DetailError
	Causewf(err error, format string, args ...interface{}) DetailError
	ErrorCode(errorCode ErrorCode) DetailError
}

func Cause ¶

func Cause(cause error) DetailError

func Causef ¶ added in v0.2.2

func Causef(format string, args ...interface{}) DetailError

func Causewf ¶ added in v0.2.3

func Causewf(err error, format string, args ...interface{}) DetailError

func Code ¶ added in v0.2.1

func Code(code string) DetailError

func Errorf ¶

func Errorf(format string, args ...interface{}) DetailError

func Msg ¶

func Msg(msg string) DetailError

func New ¶

func New(causeStr string) DetailError

New returns an error with the supplied message. New also records the stack trace at the point it was called.

func NewDetailError ¶

func NewDetailError(code string, message string, cause error) DetailError

type ErrorCode ¶

type ErrorCode interface {
	error
	fmt.Formatter

	GetCode() string
	GetMsg() string

	Code(code string) ErrorCode
	Msg(msg string) ErrorCode

	Cause(err error) DetailError
	Causef(format string, args ...interface{}) DetailError
	Causewf(err error, format string, args ...interface{}) DetailError
}

func NewErrorCode ¶

func NewErrorCode(code string, message string) ErrorCode

Jump to

Keyboard shortcuts

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