2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-12-28 14:59:49 +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 (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-11-13 23:23:55 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gcode"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2020-12-28 14:59:49 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleNewCode() {
|
2021-08-24 21:18:59 +08:00
|
|
|
err := gerror.NewCode(gcode.New(10000, "", nil), "My Error")
|
2020-12-28 14:59:49 +08:00
|
|
|
fmt.Println(err.Error())
|
|
|
|
fmt.Println(gerror.Code(err))
|
|
|
|
|
|
|
|
// Output:
|
|
|
|
// My Error
|
|
|
|
// 10000
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleNewCodef() {
|
2021-08-24 21:18:59 +08:00
|
|
|
err := gerror.NewCodef(gcode.New(10000, "", nil), "It's %s", "My Error")
|
2020-12-28 14:59:49 +08:00
|
|
|
fmt.Println(err.Error())
|
2021-08-24 21:18:59 +08:00
|
|
|
fmt.Println(gerror.Code(err).Code())
|
2020-12-28 14:59:49 +08:00
|
|
|
|
|
|
|
// Output:
|
|
|
|
// It's My Error
|
|
|
|
// 10000
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleWrapCode() {
|
|
|
|
err1 := errors.New("permission denied")
|
2021-08-24 21:18:59 +08:00
|
|
|
err2 := gerror.WrapCode(gcode.New(10000, "", nil), err1, "Custom Error")
|
2020-12-28 14:59:49 +08:00
|
|
|
fmt.Println(err2.Error())
|
2021-08-24 21:18:59 +08:00
|
|
|
fmt.Println(gerror.Code(err2).Code())
|
2020-12-28 14:59:49 +08:00
|
|
|
|
|
|
|
// Output:
|
|
|
|
// Custom Error: permission denied
|
|
|
|
// 10000
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleWrapCodef() {
|
|
|
|
err1 := errors.New("permission denied")
|
2021-08-24 21:18:59 +08:00
|
|
|
err2 := gerror.WrapCodef(gcode.New(10000, "", nil), err1, "It's %s", "Custom Error")
|
2020-12-28 14:59:49 +08:00
|
|
|
fmt.Println(err2.Error())
|
2021-08-24 21:18:59 +08:00
|
|
|
fmt.Println(gerror.Code(err2).Code())
|
2020-12-28 14:59:49 +08:00
|
|
|
|
|
|
|
// Output:
|
|
|
|
// It's Custom Error: permission denied
|
|
|
|
// 10000
|
|
|
|
}
|