gf/errors/gerror/gerror_z_example_test.go

58 lines
1.3 KiB
Go
Raw Normal View History

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() {
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() {
err := gerror.NewCodef(gcode.New(10000, "", nil), "It's %s", "My Error")
2020-12-28 14:59:49 +08:00
fmt.Println(err.Error())
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")
err2 := gerror.WrapCode(gcode.New(10000, "", nil), err1, "Custom Error")
2020-12-28 14:59:49 +08:00
fmt.Println(err2.Error())
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")
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())
fmt.Println(gerror.Code(err2).Code())
2020-12-28 14:59:49 +08:00
// Output:
// It's Custom Error: permission denied
// 10000
}