gitee.com/hongliu9527/go-tools@v0.0.8/errors/gerror/gerror_z_example_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gerror_test 8 9 import ( 10 "errors" 11 "fmt" 12 13 "gitee.com/hongliu9527/go-tools/errors/gcode" 14 "gitee.com/hongliu9527/go-tools/errors/gerror" 15 ) 16 17 func ExampleNewCode() { 18 err := gerror.NewCode(gcode.New(10000, "", nil), "My Error") 19 fmt.Println(err.Error()) 20 fmt.Println(gerror.Code(err)) 21 22 // Output: 23 // My Error 24 // 10000 25 } 26 27 func ExampleNewCodef() { 28 err := gerror.NewCodef(gcode.New(10000, "", nil), "It's %s", "My Error") 29 fmt.Println(err.Error()) 30 fmt.Println(gerror.Code(err).Code()) 31 32 // Output: 33 // It's My Error 34 // 10000 35 } 36 37 func ExampleWrapCode() { 38 err1 := errors.New("permission denied") 39 err2 := gerror.WrapCode(gcode.New(10000, "", nil), err1, "Custom Error") 40 fmt.Println(err2.Error()) 41 fmt.Println(gerror.Code(err2).Code()) 42 43 // Output: 44 // Custom Error: permission denied 45 // 10000 46 } 47 48 func ExampleWrapCodef() { 49 err1 := errors.New("permission denied") 50 err2 := gerror.WrapCodef(gcode.New(10000, "", nil), err1, "It's %s", "Custom Error") 51 fmt.Println(err2.Error()) 52 fmt.Println(gerror.Code(err2).Code()) 53 54 // Output: 55 // It's Custom Error: permission denied 56 // 10000 57 } 58 59 func ExampleEqual() { 60 err1 := errors.New("permission denied") 61 err2 := gerror.New("permission denied") 62 err3 := gerror.NewCode(gcode.CodeNotAuthorized, "permission denied") 63 fmt.Println(gerror.Equal(err1, err2)) 64 fmt.Println(gerror.Equal(err2, err3)) 65 66 // Output: 67 // true 68 // false 69 } 70 71 func ExampleIs() { 72 err1 := errors.New("permission denied") 73 err2 := gerror.Wrap(err1, "operation failed") 74 fmt.Println(gerror.Is(err1, err1)) 75 fmt.Println(gerror.Is(err2, err2)) 76 fmt.Println(gerror.Is(err2, err1)) 77 fmt.Println(gerror.Is(err1, err2)) 78 79 // Output: 80 // false 81 // true 82 // true 83 // false 84 }