errors

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 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"
	"testing"
	"time"
)

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

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

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

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

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

	t.Log(errors.If(true, "", "").(string))
}

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

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

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

	errors.TT(true, func(m *errors.M) {
		m.Msg("test tt").
			Tag("tag").
			M("k", "v")

	})
}

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

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

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

	errors.WrapM(es.New("dd"), func(m *errors.M) {
		m.Msg("test")
	})
}

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

	errors.WrapM(es.New("sbhbhbh"), func(m *errors.M) {
		m.Msg("test shhh").
			M("ss", 1).
			M("input", 2)
	})
}

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

	for i := 0; i < 10000; i++ {
		errors.Try(testFunc)()
		t.Log("ok")
	}

}

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

	//T(true, "sss")
	errors.TT(true, func(m *errors.M) {
		m.Msg("test tt")
	})
}

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

	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(func() {})

		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(1), "")
	errors.T(errors.IsZero(1.2), "")
	errors.T(!errors.IsZero(nil), "")
	errors.T(errors.IsZero("ss"), "")
	errors.T(errors.IsZero(map[string]interface{}{}), "")
	errors.T(errors.IsZero(ss()), "")
	errors.T(!errors.IsZero(ss1()), "")
	errors.T(errors.IsZero(&s), "")
	errors.T(!errors.IsZero(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(func() {})

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cfg = struct {
	Debug bool
}{
	Debug: true,
}
View Source
var ErrTag = struct {
	UnknownErr string
}{
	UnknownErr: "unknown",
}

Functions

func Debug

func Debug()

func ErrHandle

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

func ErrLog added in v0.1.8

func ErrLog(err interface{})

func FnCost

func FnCost(f func()) time.Duration

func FnOf

func FnOf(fn interface{}, args ...interface{}) func() []reflect.Value

func Handle

func Handle(fn func())

func If

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

func IsZero added in v0.1.0

func IsZero(v interface{}) 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())

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{}, args ...interface{}) func(...interface{}) (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) StackTrace

func (t *Err) StackTrace() *_Err

func (*Err) Tag

func (t *Err) Tag(tag string) *Err

Jump to

Keyboard shortcuts

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