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"
|
2019-06-29 18:17:33 +08:00
|
|
|
"testing"
|
|
|
|
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gcode"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/json"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/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)
|
2021-11-13 23:23:55 +08:00
|
|
|
// fmt.Printf("%+v", err)
|
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")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(fmt.Sprintf("%+v", err), "1")
|
2021-11-13 23:23:55 +08:00
|
|
|
// fmt.Printf("%+v", err)
|
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-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)
|
2021-11-13 23:23:55 +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)
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2020-10-17 17:17:10 +08:00
|
|
|
})
|
|
|
|
}
|
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) {
|
2021-08-24 21:18:59 +08:00
|
|
|
err := gerror.NewCode(gcode.CodeUnknown, "123")
|
|
|
|
t.Assert(gerror.Code(err), gcode.CodeUnknown)
|
2020-12-10 23:33:24 +08:00
|
|
|
t.Assert(err.Error(), "123")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-08-24 21:18:59 +08:00
|
|
|
err := gerror.NewCodef(gcode.New(1, "", nil), "%s", "123")
|
|
|
|
t.Assert(gerror.Code(err).Code(), 1)
|
2020-12-10 23:33:24 +08:00
|
|
|
t.Assert(err.Error(), "123")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-08-24 21:18:59 +08:00
|
|
|
err := gerror.NewCodeSkip(gcode.New(1, "", nil), 0, "123")
|
|
|
|
t.Assert(gerror.Code(err).Code(), 1)
|
2020-12-10 23:33:24 +08:00
|
|
|
t.Assert(err.Error(), "123")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-08-24 21:18:59 +08:00
|
|
|
err := gerror.NewCodeSkipf(gcode.New(1, "", nil), 0, "%s", "123")
|
|
|
|
t.Assert(gerror.Code(err).Code(), 1)
|
2020-12-10 23:33:24 +08:00
|
|
|
t.Assert(err.Error(), "123")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
2021-08-24 21:18:59 +08:00
|
|
|
err = gerror.WrapCode(gcode.New(1, "", nil), err, "3")
|
|
|
|
t.Assert(gerror.Code(err).Code(), 1)
|
2020-12-10 23:33:24 +08:00
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
2021-08-24 21:18:59 +08:00
|
|
|
err = gerror.WrapCodef(gcode.New(1, "", nil), err, "%s", "3")
|
|
|
|
t.Assert(gerror.Code(err).Code(), 1)
|
2020-12-10 23:33:24 +08:00
|
|
|
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")
|
2021-08-24 21:18:59 +08:00
|
|
|
err = gerror.WrapCodeSkip(gcode.New(1, "", nil), 100, err, "3")
|
|
|
|
t.Assert(gerror.Code(err).Code(), 1)
|
2020-12-30 13:18:43 +08:00
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := errors.New("1")
|
|
|
|
err = gerror.Wrap(err, "2")
|
2021-08-24 21:18:59 +08:00
|
|
|
err = gerror.WrapCodeSkipf(gcode.New(1, "", nil), 100, err, "%s", "3")
|
|
|
|
t.Assert(gerror.Code(err).Code(), 1)
|
2020-12-30 13:18:43 +08:00
|
|
|
t.Assert(err.Error(), "3: 2: 1")
|
|
|
|
})
|
2020-12-10 23:33:24 +08:00
|
|
|
}
|
2021-01-14 00:05:15 +08:00
|
|
|
|
2021-11-14 17:47:21 +08:00
|
|
|
func Test_SetCode(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err := gerror.New("123")
|
|
|
|
t.Assert(gerror.Code(err), -1)
|
|
|
|
t.Assert(err.Error(), "123")
|
|
|
|
|
|
|
|
err.(*gerror.Error).SetCode(gcode.CodeValidationFailed)
|
|
|
|
t.Assert(gerror.Code(err), gcode.CodeValidationFailed)
|
|
|
|
t.Assert(err.Error(), "123")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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"`)
|
|
|
|
})
|
|
|
|
}
|
2021-09-17 19:26:56 +08:00
|
|
|
|
|
|
|
func Test_HasStack(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
err1 := errors.New("1")
|
|
|
|
err2 := gerror.New("1")
|
|
|
|
t.Assert(gerror.HasStack(err1), false)
|
|
|
|
t.Assert(gerror.HasStack(err2), true)
|
|
|
|
})
|
|
|
|
}
|