e

package module
Version: v1.0.0 Latest Latest
Warning

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

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

README

workflows GitHub Workflow Status GitHub license

中文README

Golang graceful trace stack error packet

Golang tracks stack error package

Install

go get github.com/yezihack/e

Introduction

github.com/yezihack/e Project is a graceful way to track your stack information. Easy to store in log, It also extends the 'error' package, custom 'code, msg' information.

Features

Trace stack error messages gracefully

  1. Based ongithub.com/pkg/errorsPackage
  2. Support 'code', 'msg' custom error code and error information
  3. Convenient storage of log 'JSON' files
  4. The stack information is displayed humanized

Documentation

https://godoc.org/github.com/yezihack/e

Simple use

package main
import (
    "github.com/yezihack/e"
    "log"
)
func foo() error {
    return e.New("foo")
}
func main() {
    err := foo()
    if err != nil { // you need to determine whether it is a custom error, otherwise you cannot output stack information
        if e.Assert(err)  {
            log.Println(e.Convert(err).ToStr()) // output string form
            log.Println(e.Convert(err).ToArr()) // output array form
        } else {
            log.Println(err) // system error
        }
    }
}

Testing

go get github.com/smartystreets/goconvey
goconvey -port 8080

Example

  1. basic usage
  2. code usage
  3. compatible with error in old projects
  4. get extension error of extra
  5. used in gin)
  6. More waiting to be updated

Output general information and stack information (string or array)

package main

import (
	"log"

	"github.com/yezihack/e"
)

func foo(s string) error {
	return e.New("foo," + s)
}

func main() {
	// (1) general use
    err := foo("stack error")
    if err != nil {
        log.Println(err)
    }
    // out:
    // 2021/01/15 20:23:21 foo,stack error

    // (2)output stack information by string
    if e.Assert(err) { // you need to determine whether it is a custom error, otherwise you cannot output stack information.
        log.Println(e.Convert(err).ToStr())
    }
    // out:
    //2021/01/15 20:23:21 file:1.how.go, line:10, func:foo
    //file:1.how.go, line:15, func:main

    // (3) output stack information by array
    if e.Assert(err) { // you need to determine whether it is a custom error, otherwise you cannot output stack information.
        log.Println(e.Convert(err).ToArr())
    }
    // out
    //2021/01/15 20:23:21 [file:1.how.go, line:10, func:foo file:1.how.go, line:15, func:main]
}

Error message with custom 'code'

package main

import (
	"fmt"

	"github.com/yezihack/e"
)

// if you use error with code
func fooCode() error {
	return e.NewCode(400, "eoo error")
}

func main() {
	err := fooCode()
	if e.Assert(err) {
		e1 := e.Convert(err)
		fmt.Printf("code:%d, err:%s\n", e1.Code(), e1.Msg())
	}
	//out:
	//code:400, err:eoo error
	if e.Assert(err) {
		e1 := e.Convert(err)
		fmt.Printf("code:%d, err:%s\n", e1.Code(), e1.ToStr())
	}
	// out:
	//code:400, err:file:2.code.go, line:11, func:fooCode
	//file:2.code.go, line:15, func:main
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err, target error) bool

func Assert

func Assert(e error) bool

对 error 进行断言, 是否自定义的错误

func ErrorCodeF

func ErrorCodeF(code int, format string, args ...interface{}) error

格式+信息

func ErrorF

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

格式+信息

func Is

func Is(err, target error) bool

func New

func New(s string, extras ...error) error

信息+额外errs

func NewCode

func NewCode(code int, s string, extras ...error) error

信息+额外errs

func ToArr

func ToArr(err error) []string

打印堆栈信息 by slice

func ToStr

func ToStr(err error) string

打印堆栈信息 by str

func UnWrap

func UnWrap(err error) error

func WithCodeMessage

func WithCodeMessage(code int, err error, msg string, extras ...error) error

错误+信息+额外errs

func WithCodeMessageF

func WithCodeMessageF(code int, err error, format string, args ...interface{}) error

错误+格式+信息

func WithCodeStack

func WithCodeStack(code int, err error, extras ...error) error

错误堆栈设置+额外errs

func WithMessage

func WithMessage(err error, msg string, extras ...error) error

错误+信息+额外errs

func WithMessageF

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

错误+格式+信息

func WithStack

func WithStack(err error, extras ...error) error

错误堆栈设置+额外errs

Types

type StackError

type StackError struct {
	// contains filtered or unexported fields
}

错误自定义, 支持堆栈信息的错误结构体

func Convert

func Convert(e error) *StackError

转换成自定义 error 对象

func (*StackError) Code

func (c *StackError) Code() int

获取错误码. 只有设置了才会有.

func (*StackError) Err

func (c *StackError) Err() error

获取错误

func (*StackError) Error

func (c *StackError) Error() string

实现系统自带的 error 接口

func (*StackError) Errs

func (c *StackError) Errs() []error

获取扩展的 error 列表

func (*StackError) ExistExtra

func (c *StackError) ExistExtra() bool

判断是否存在扩展错误信息

func (*StackError) Msg

func (c *StackError) Msg() string

获取错误信息

func (*StackError) ToArr

func (c *StackError) ToArr() []string

打印堆栈信息 by array

func (*StackError) ToStr

func (c *StackError) ToStr() string

打印堆栈信息 by string, 带 \n 分隔

func (*StackError) ToStrByExtra

func (c *StackError) ToStrByExtra() string

获取扩展的 error 拼接的字符串

type StackSourceEntity

type StackSourceEntity struct {
	File     string `json:"file"` // 文件名称
	LineCode string `json:"line"` // 行号
	FuncName string `json:"func"` // 函数名称
}

定义输出的结构体

func MarshalStack

func MarshalStack(err error) []*StackSourceEntity

MarshalStack implements pkg/errors stack trace marshaling.

Jump to

Keyboard shortcuts

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