errors

package module
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2019 License: MIT Imports: 14 Imported by: 14

README

errors

error handle for go

package errors_test

import (
	es "errors"
	"fmt"
	"github.com/pubgo/errors"
	"reflect"
	"testing"
	"time"
)

func TestT(t *testing.T) {
	defer errors.Debug()

	errors.T(true, "test t")
}

func TestRetry(t *testing.T) {
	defer errors.Debug()

	errors.Retry(3, func() {
		errors.T(true, "test t")
	})
}

func TestIf(t *testing.T) {
	defer errors.Log()

	errors.T(errors.If(true, "test true", "test false").(string) != "test true", "")
}

func TestTT(t *testing.T) {
	defer errors.Debug()

	errors.TT(true, "test tt").
		M("k", "v").
		SetTag("ss", 12).
		Done()
}

func TestWrap(t *testing.T) {
	defer errors.Debug()

	errors.Wrap(es.New("test"), "test")
}

func TestWrapM(t *testing.T) {
	defer errors.Debug()

	errors.WrapM(es.New("dd"), "test").
		Done()
}

func testFunc_2() {
	defer errors.Handle()()

	errors.WrapM(es.New("testFunc_1"), "test shhh").
		M("ss", 1).
		M("input", 2).
		Done()
}

func testFunc_1() {
	defer errors.Handle()()

	testFunc_2()
}

func testFunc() {
	defer errors.Handle()()

	errors.Wrap(errors.Try(testFunc_1), "errors.Wrap")
}

func TestErrLog(t *testing.T) {
	defer errors.Debug()

	errors.Wrap(errors.Try(testFunc), "errors.Wrap11111")
}

func init11() {
	defer errors.Handle()()

	//T(true, "sss")
	errors.T(true, "test tt")
}

func TestT2(t *testing.T) {
	defer errors.Debug()

	init11()
}

func TestTry(t *testing.T) {
	defer errors.Log()

	errors.Panic(errors.Try(errors.T, true, "sss"))
}

func TestTask(t *testing.T) {
	defer errors.Log()

	errors.Wrap(errors.Try(func() {
		errors.Wrap(es.New("dd"), "err ")
	}), "test wrap")
}

func TestHandle(t *testing.T) {
	defer errors.Log()

	func() {
		defer errors.Handle()()

		errors.Wrap(es.New("hello error"), "sss")
	}()

}

func TestErrHandle(t *testing.T) {
	defer errors.Log()

	errors.ErrHandle(errors.Try(errors.T, true, "test 12345"), func(err *errors.Err) {
		err.P()
	})

	errors.ErrHandle(errors.Try(errors.T, true, "test 12345"))

	errors.ErrHandle("ttt")
	errors.ErrHandle(es.New("eee"))
	errors.ErrHandle([]string{"dd"})
}

func TestIsZero(t *testing.T) {
	defer errors.Log()

	var ss = func() map[string]interface{} {
		return make(map[string]interface{})
	}

	var ss1 = func() map[string]interface{} {
		return nil
	}

	var s = 1
	var ss2 map[string]interface{}
	errors.T(errors.IsZero(reflect.ValueOf(1)), "")
	errors.T(errors.IsZero(reflect.ValueOf(1.2)), "")
	errors.T(!errors.IsZero(reflect.ValueOf(nil)), "")
	errors.T(errors.IsZero(reflect.ValueOf("ss")), "")
	errors.T(errors.IsZero(reflect.ValueOf(map[string]interface{}{})), "")
	errors.T(errors.IsZero(reflect.ValueOf(ss())), "")
	errors.T(!errors.IsZero(reflect.ValueOf(ss1())), "")
	errors.T(errors.IsZero(reflect.ValueOf(&s)), "")
	errors.T(!errors.IsZero(reflect.ValueOf(ss2)), "")
}

func TestResp(t *testing.T) {
	defer errors.Resp(func(err *errors.Err) {
		err.Log()
	})

	errors.T(true, "data handle")
}

func TestTicker(t *testing.T) {
	defer errors.Handle()()

	errors.Ticker(func(dur time.Time) time.Duration {
		fmt.Println(dur)
		return time.Second
	})
}

func TestRetryAt(t *testing.T) {
	errors.RetryAt(time.Second*2, func(dur time.Duration) {
		fmt.Println(dur.String())

		errors.T(true, "test RetryAt")
	})
}

func TestErr(t *testing.T) {
	s := "hello 世界"
	fmt.Println(len(s), len([]rune(s)))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cfg = struct {
	Debug       bool
	MaxObj      uint8
	MaxRetryDur time.Duration
}{
	Debug:       true,
	MaxObj:      15,
	MaxRetryDur: time.Hour * 24,
}
View Source
var ErrTags = struct {
	UnknownTypeCode uint16
}{
	1000,
}

Functions

func Debug

func Debug()

func Default added in v0.2.6

func Default(t, f interface{}) interface{}

func ErrCodeRegistry added in v0.2.2

func ErrCodeRegistry(code uint16, err string)

func ErrHandle

func ErrHandle(err interface{}, fn ...func(err *Err))

func ErrLog added in v0.1.8

func ErrLog(err interface{})

func FnOf

func FnOf(fn interface{}) (reflect.Value, bool, reflect.Value)

func GetErrTag added in v0.2.2

func GetErrTag(code uint16) string

func GetErrTags added in v0.2.2

func GetErrTags() map[uint16]string

func Handle

func Handle() func()

func If

func If(b bool, t, f interface{}) interface{}

func IsZero added in v0.1.0

func IsZero(val reflect.Value) bool

func Log added in v0.1.10

func Log()

func P

func P(d ...interface{})

func Panic

func Panic(err interface{})

func Resp

func Resp(fn func(err *Err))

func Retry added in v0.1.4

func Retry(num int, fn func()) (err error)

func RetryAt added in v0.1.10

func RetryAt(t time.Duration, fn func(at time.Duration))

func T

func T(b bool, msg string, args ...interface{})

func Ticker added in v0.1.7

func Ticker(fn func(dur time.Time) time.Duration)

func Try

func Try(fn interface{}) func(args ...interface{}) func(...interface{}) (err error)

func TryRaw added in v0.2.6

func TryRaw(fn reflect.Value) func(...reflect.Value) func(...reflect.Value) (err error)

func Wrap

func Wrap(err interface{}, msg string, args ...interface{})

Types

type Err

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

func TT

func TT(b bool, msg string, args ...interface{}) *Err

func WrapM

func WrapM(err interface{}, msg string, args ...interface{}) *Err

func (*Err) Caller added in v0.2.0

func (t *Err) Caller(depth int) *Err

func (*Err) Done added in v0.2.0

func (t *Err) Done()

func (*Err) Err

func (t *Err) Err() error

func (*Err) Error

func (t *Err) Error() string

func (*Err) Log added in v0.1.10

func (t *Err) Log()

func (*Err) M added in v0.2.0

func (t *Err) M(k string, v interface{}) *Err

func (*Err) P

func (t *Err) P()

func (*Err) SetTag added in v0.2.2

func (t *Err) SetTag(k string, v uint16) *Err

func (*Err) StackTrace

func (t *Err) StackTrace() *_Err

func (*Err) Tag

func (t *Err) Tag() uint16

Jump to

Keyboard shortcuts

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