2020-12-30 13:18:43 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-06-29 18:17:33 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package gerror_test
|
|
|
|
|
|
|
|
import (
|
2019-06-30 22:21:08 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-01-14 00:05:15 +08:00
|
|
|
"github.com/gogf/gf/internal/json"
|
2019-06-29 18:17:33 +08:00
|
|
|
"testing"
|
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/errors/gerror"
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
2019-06-29 18:17:33 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func nilError() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Nil(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
t.Assert(gerror.New(""), nil)
|
|
|
|
t.Assert(gerror.Wrap(nilError(), "test"), nil)
|
2019-06-29 18:17:33 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-30 13:18:43 +08:00
|
|
|
func Test_New(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := gerror.New("1")
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := gerror.Newf("%d", 1)
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := gerror.NewSkip(1, "1")
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := gerror.NewSkipf(1, "%d", 1)
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "1")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-29 18:17:33 +08:00
|
|
|
func Test_Wrap(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-30 22:21:08 +08:00
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
2019-06-30 22:21:08 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-30 22:21:08 +08:00
|
|
|
err := gerror.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
2019-06-30 22:21:08 +08:00
|
|
|
})
|
2020-12-11 00:45:15 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := gerror.New("1")
|
|
|
|
err = gerror.Wrap(err, "")
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "1")
|
|
|
|
})
|
2019-06-30 22:21:08 +08:00
|
|
|
}
|
|
|
|
|
2020-12-30 13:18:43 +08:00
|
|
|
func Test_Wrapf(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.Wrapf(err, "%d", 2)
|
|
|
|
err = gerror.Wrapf(err, "%d", 3)
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := gerror.New("1")
|
|
|
|
err = gerror.Wrapf(err, "%d", 2)
|
|
|
|
err = gerror.Wrapf(err, "%d", 3)
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := gerror.New("1")
|
|
|
|
err = gerror.Wrapf(err, "")
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "1")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_WrapSkip(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.WrapSkip(1, err, "2")
|
|
|
|
err = gerror.WrapSkip(1, err, "3")
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := gerror.New("1")
|
|
|
|
err = gerror.WrapSkip(1, err, "2")
|
|
|
|
err = gerror.WrapSkip(1, err, "3")
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := gerror.New("1")
|
|
|
|
err = gerror.WrapSkip(1, err, "")
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "1")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_WrapSkipf(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.WrapSkipf(1, err, "2")
|
|
|
|
err = gerror.WrapSkipf(1, err, "3")
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := gerror.New("1")
|
|
|
|
err = gerror.WrapSkipf(1, err, "2")
|
|
|
|
err = gerror.WrapSkipf(1, err, "3")
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := gerror.New("1")
|
|
|
|
err = gerror.WrapSkipf(1, err, "")
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(err.Error(), "1")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-09 10:40:26 +08:00
|
|
|
func Test_Cause(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-09 10:40:26 +08:00
|
|
|
err := errors.New("1")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gerror.Cause(err), err)
|
2019-07-09 10:40:26 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-09 10:40:26 +08:00
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gerror.Cause(err), "1")
|
2019-07-09 10:40:26 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-09 10:40:26 +08:00
|
|
|
err := gerror.New("1")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gerror.Cause(err), "1")
|
2019-07-09 10:40:26 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-09 10:40:26 +08:00
|
|
|
err := gerror.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gerror.Cause(err), "1")
|
2019-07-09 10:40:26 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-30 22:21:08 +08:00
|
|
|
func Test_Format(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-30 22:21:08 +08:00
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(fmt.Sprintf("%s", err), "3: 2: 1")
|
|
|
|
t.Assert(fmt.Sprintf("%v", err), "3: 2: 1")
|
2019-06-30 22:21:08 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-30 22:21:08 +08:00
|
|
|
err := gerror.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(fmt.Sprintf("%s", err), "3: 2: 1")
|
|
|
|
t.Assert(fmt.Sprintf("%v", err), "3: 2: 1")
|
2019-06-30 22:21:08 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-30 22:21:08 +08:00
|
|
|
err := gerror.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
|
|
|
t.Assert(fmt.Sprintf("%-s", err), "3")
|
|
|
|
t.Assert(fmt.Sprintf("%-v", err), "3")
|
2019-06-30 22:21:08 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Stack(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-30 22:21:08 +08:00
|
|
|
err := errors.New("1")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(fmt.Sprintf("%+v", err), "1")
|
2019-06-30 22:21:08 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-30 22:21:08 +08:00
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-06-30 22:21:08 +08:00
|
|
|
//fmt.Printf("%+v", err)
|
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-30 22:21:08 +08:00
|
|
|
err := gerror.New("1")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(fmt.Sprintf("%+v", err), "1")
|
2019-06-30 22:21:08 +08:00
|
|
|
//fmt.Printf("%+v", err)
|
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-29 23:35:32 +08:00
|
|
|
err := gerror.New("1")
|
2019-06-30 22:21:08 +08:00
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-06-30 22:21:08 +08:00
|
|
|
//fmt.Printf("%+v", err)
|
2019-06-29 18:17:33 +08:00
|
|
|
})
|
|
|
|
}
|
2020-10-17 17:17:10 +08:00
|
|
|
|
|
|
|
func Test_Current(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
|
|
|
t.Assert(gerror.Current(err).Error(), "3")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Next(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.Wrap(err, "3")
|
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
|
|
|
|
|
|
|
err = gerror.Next(err)
|
|
|
|
t.Assert(err.Error(), "2: 1")
|
|
|
|
|
|
|
|
err = gerror.Next(err)
|
|
|
|
t.Assert(err.Error(), "1")
|
|
|
|
|
|
|
|
err = gerror.Next(err)
|
|
|
|
t.Assert(err, nil)
|
|
|
|
})
|
|
|
|
}
|
2020-12-10 23:33:24 +08:00
|
|
|
|
|
|
|
func Test_Code(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := errors.New("123")
|
|
|
|
t.Assert(gerror.Code(err), -1)
|
|
|
|
t.Assert(err.Error(), "123")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := gerror.NewCode(1, "123")
|
|
|
|
t.Assert(gerror.Code(err), 1)
|
|
|
|
t.Assert(err.Error(), "123")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := gerror.NewCodef(1, "%s", "123")
|
|
|
|
t.Assert(gerror.Code(err), 1)
|
|
|
|
t.Assert(err.Error(), "123")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := gerror.NewCodeSkip(1, 0, "123")
|
|
|
|
t.Assert(gerror.Code(err), 1)
|
|
|
|
t.Assert(err.Error(), "123")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := gerror.NewCodeSkipf(1, 0, "%s", "123")
|
|
|
|
t.Assert(gerror.Code(err), 1)
|
|
|
|
t.Assert(err.Error(), "123")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.WrapCode(1, err, "3")
|
|
|
|
t.Assert(gerror.Code(err), 1)
|
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.WrapCodef(1, err, "%s", "3")
|
|
|
|
t.Assert(gerror.Code(err), 1)
|
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
|
|
|
})
|
2020-12-30 13:18:43 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.WrapCodeSkip(1, 100, err, "3")
|
|
|
|
t.Assert(gerror.Code(err), 1)
|
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
|
|
|
err = gerror.WrapCodeSkipf(1, 100, err, "%s", "3")
|
|
|
|
t.Assert(gerror.Code(err), 1)
|
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
|
|
|
})
|
2020-12-10 23:33:24 +08:00
|
|
|
}
|
2021-01-14 00:05:15 +08:00
|
|
|
|
|
|
|
func Test_Json(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := gerror.Wrap(gerror.New("1"), "2")
|
|
|
|
b, e := json.Marshal(err)
|
|
|
|
t.Assert(e, nil)
|
|
|
|
t.Assert(string(b), `"2: 1"`)
|
|
|
|
})
|
|
|
|
}
|