errors

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2021 License: Apache-2.0 Imports: 5 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

View Source
const (
	DefaultErrorCode = 500
	DefaultErrorName = "***SERVICE ERROR***"
)

Variables

This section is empty.

Functions

This section is empty.

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
}

func Map added in v1.1.2

func Map(err error) (codeErr 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

Jump to

Keyboard shortcuts

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