errors

package module
v1.13.12 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: Apache-2.0 Imports: 10 Imported by: 128

README

概要

Code Error 是一个继承标准 error 并增加 code 内容进行扩展的错误结构。

获取

go get github.com/aacfactory/errors

使用

// 基本使用
err := errors.New(500, errors.DefaultErrorName, "foo")
fmt.Printf("%v\n", err)
// Output: foo
fmt.Printf("%+v\n", err)
// Output: 
/*
   >>>>>>>>>>>>>
   ID      = [c4h3pfde2f60qd5fde8g]
   CN      = [500][***SERVICE ERROR***]
   MESSAGE = foo
   STACK   = github.com/aacfactory/errors_test.TestNew github.com/aacfactory/errors/error_test.go:11
   <<<<<<<<<<<<<
*/
// Map (标准 error 转换 CodeError)
cause := fmt.Errorf("foo")
err := errors.Map(cause)
fmt.Printf("%v\n", err)
fmt.Printf("%+v\n", err)
// WithCause (引入其它错误)
err := errors.New(500, errors.DefaultErrorName, "foo")
err = err.WithCause(fmt.Errorf("bar")).WithCause(fmt.Errorf("baz"))
fmt.Printf("%v\n", err)
fmt.Printf("%+v\n", err)
fmt.Println("contains foo", err.Contains(fmt.Errorf("foo"))) // true
fmt.Println("contains bar", err.Contains(fmt.Errorf("bar"))) // true
fmt.Println("contains baz", err.Contains(fmt.Errorf("baz"))) // true
fmt.Println("contains x  ", err.Contains(fmt.Errorf("x"))) // false
// WithMeta (添加员元数据,一般用于参数校验错误)
err := errors.New(500, errors.DefaultErrorName, "foo")
err = err.WithMeta("a", time.Now().String()).WithMeta("b", "b")
fmt.Printf("%v\n", err)
fmt.Printf("%+v\n", err)

Json 输出

{
  "id": "c4h3rsde2f60j14h822g",
  "code": 500,
  "name": "***SERVICE ERROR***",
  "message": "foo",
  "meta": {
    "a": "2021-08-22 20:07:13.0526095 +0800 CST m=+0.002060201",
    "b": "b"
  },
  "stacktrace": {
    "fn": "github.com/aacfactory/errors_test.Test_Json",
    "file": "github.com/aacfactory/errors/error_test.go",
    "line": 43
  },
  "cause": {
    "id": "c4h3rsde2f60j14h8230",
    "code": 500,
    "name": "***SERVICE ERROR***",
    "message": "bar",
    "stacktrace": {
      "fn": "testing.tRunner",
      "file": "testing/testing.go",
      "line": 1193
    },
    "cause": {
      "id": "c4h3rsde2f60j14h823g",
      "code": 500,
      "name": "***SERVICE ERROR***",
      "message": "baz",
      "stacktrace": {
        "fn": "testing.tRunner",
        "file": "testing/testing.go",
        "line": 1193
      }
    }
  }
}

引用感谢

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains added in v1.13.5

func Contains(a error, b error) (has bool)

Types

type CodeError

type CodeError interface {
	Id() string
	Code() int
	Name() string
	Message() string
	Stacktrace() (fn string, file string, line int)
	WithMeta(key string, value string) (err CodeError)
	WithCause(cause error) (err CodeError)
	Contains(err error) (has bool)
	Error() string
	Format(state fmt.State, r rune)
	String() string
	json.Marshaler
}

func As added in v1.13.6

func As(err error) (e CodeError, ok bool)

func BadRequest added in v1.4.0

func BadRequest(message string) CodeError

func Decode added in v1.13.3

func Decode(p []byte) (err CodeError)

func Empty added in v1.5.0

func Empty() CodeError

func Forbidden added in v1.4.0

func Forbidden(message string) CodeError

func New added in v1.3.0

func New(code int, name string, message string) CodeError

func NewWithDepth added in v1.3.0

func NewWithDepth(code int, name string, message string, skip int) CodeError

func NilError added in v1.6.0

func NilError() CodeError

func NotAcceptable added in v1.4.0

func NotAcceptable(message string) CodeError

func NotFound added in v1.4.0

func NotFound(message string) CodeError

func NotImplemented added in v1.4.0

func NotImplemented(message string) CodeError

func ServiceError

func ServiceError(message string) CodeError

func Timeout added in v1.4.0

func Timeout(message string) CodeError

func TooEarly added in v1.13.6

func TooEarly(message string) CodeError

func TooMayRequest added in v1.13.6

func TooMayRequest(message string) CodeError

func Unauthorized added in v1.4.0

func Unauthorized(message string) CodeError

func Unavailable added in v1.4.0

func Unavailable(message string) CodeError

func Warning added in v1.5.0

func Warning(message string) CodeError

func Wrap added in v1.13.10

func Wrap(err error) (codeErr CodeError)

type CodeErrorImpl added in v1.13.12

type CodeErrorImpl struct {
	Id_         string         `json:"id,omitempty" avro:"id"`
	Code_       int            `json:"code,omitempty" avro:"code"`
	Name_       string         `json:"name,omitempty" avro:"name"`
	Message_    string         `json:"message,omitempty" avro:"message"`
	Meta_       Meta           `json:"meta,omitempty" avro:"meta"`
	Stacktrace_ Stacktrace     `json:"stacktrace,omitempty" avro:"stacktrace"`
	Cause_      *CodeErrorImpl `json:"cause,omitempty" avro:"cause"`
}

func (CodeErrorImpl) Code added in v1.13.12

func (e CodeErrorImpl) Code() int

func (CodeErrorImpl) Contains added in v1.13.12

func (e CodeErrorImpl) Contains(err error) (has bool)

func (CodeErrorImpl) Error added in v1.13.12

func (e CodeErrorImpl) Error() string

func (CodeErrorImpl) Format added in v1.13.12

func (e CodeErrorImpl) Format(state fmt.State, verb rune)

func (CodeErrorImpl) Id added in v1.13.12

func (e CodeErrorImpl) Id() string

func (CodeErrorImpl) MarshalJSON added in v1.13.12

func (e CodeErrorImpl) MarshalJSON() (p []byte, err error)

func (CodeErrorImpl) Message added in v1.13.12

func (e CodeErrorImpl) Message() string

func (CodeErrorImpl) Name added in v1.13.12

func (e CodeErrorImpl) Name() string

func (CodeErrorImpl) Stacktrace added in v1.13.12

func (e CodeErrorImpl) Stacktrace() (fn string, file string, line int)

func (CodeErrorImpl) String added in v1.13.12

func (e CodeErrorImpl) String() string

func (CodeErrorImpl) WithCause added in v1.13.12

func (e CodeErrorImpl) WithCause(cause error) (err CodeError)

func (CodeErrorImpl) WithMeta added in v1.13.12

func (e CodeErrorImpl) WithMeta(key string, value string) (err CodeError)

type Errors added in v1.13.0

type Errors []CodeError

func MakeErrors added in v1.13.0

func MakeErrors() Errors

func (*Errors) Append added in v1.13.0

func (e *Errors) Append(err error)

func (*Errors) Error added in v1.13.0

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

type JoinedErrors added in v1.13.6

type JoinedErrors interface {
	Unwrap() []error
}

type Meta added in v1.13.12

type Meta []Pair

func (Meta) Add added in v1.13.12

func (m Meta) Add(key string, value string) Meta

func (Meta) Len added in v1.13.12

func (m Meta) Len() int

func (Meta) Less added in v1.13.12

func (m Meta) Less(i, j int) bool

func (Meta) MarshalJSON added in v1.13.12

func (m Meta) MarshalJSON() (p []byte, err error)

func (Meta) Swap added in v1.13.12

func (m Meta) Swap(i, j int)

type Pair added in v1.13.9

type Pair struct {
	Key   string `json:"key" avro:"key"`
	Value string `json:"value" avro:"value"`
}

func (Pair) MarshalJSON added in v1.13.9

func (pair Pair) MarshalJSON() (p []byte, err error)

type Stacktrace added in v1.13.12

type Stacktrace struct {
	Fn   string `json:"fn" avro:"fn"`
	File string `json:"file" avro:"file"`
	Line int    `json:"line" avro:"line"`
}

func (Stacktrace) MarshalJSON added in v1.13.12

func (s Stacktrace) MarshalJSON() (p []byte, err error)

Jump to

Keyboard shortcuts

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