github.com/gogf/gf/v2@v2.7.4/errors/gerror/gerror_api_code.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 8 9 import ( 10 "fmt" 11 "strings" 12 13 "github.com/gogf/gf/v2/errors/gcode" 14 ) 15 16 // NewCode creates and returns an error that has error code and given text. 17 func NewCode(code gcode.Code, text ...string) error { 18 return &Error{ 19 stack: callers(), 20 text: strings.Join(text, commaSeparatorSpace), 21 code: code, 22 } 23 } 24 25 // NewCodef returns an error that has error code and formats as the given format and args. 26 func NewCodef(code gcode.Code, format string, args ...interface{}) error { 27 return &Error{ 28 stack: callers(), 29 text: fmt.Sprintf(format, args...), 30 code: code, 31 } 32 } 33 34 // NewCodeSkip creates and returns an error which has error code and is formatted from given text. 35 // The parameter `skip` specifies the stack callers skipped amount. 36 func NewCodeSkip(code gcode.Code, skip int, text ...string) error { 37 return &Error{ 38 stack: callers(skip), 39 text: strings.Join(text, commaSeparatorSpace), 40 code: code, 41 } 42 } 43 44 // NewCodeSkipf returns an error that has error code and formats as the given format and args. 45 // The parameter `skip` specifies the stack callers skipped amount. 46 func NewCodeSkipf(code gcode.Code, skip int, format string, args ...interface{}) error { 47 return &Error{ 48 stack: callers(skip), 49 text: fmt.Sprintf(format, args...), 50 code: code, 51 } 52 } 53 54 // WrapCode wraps error with code and text. 55 // It returns nil if given err is nil. 56 func WrapCode(code gcode.Code, err error, text ...string) error { 57 if err == nil { 58 return nil 59 } 60 return &Error{ 61 error: err, 62 stack: callers(), 63 text: strings.Join(text, commaSeparatorSpace), 64 code: code, 65 } 66 } 67 68 // WrapCodef wraps error with code and format specifier. 69 // It returns nil if given `err` is nil. 70 func WrapCodef(code gcode.Code, err error, format string, args ...interface{}) error { 71 if err == nil { 72 return nil 73 } 74 return &Error{ 75 error: err, 76 stack: callers(), 77 text: fmt.Sprintf(format, args...), 78 code: code, 79 } 80 } 81 82 // WrapCodeSkip wraps error with code and text. 83 // It returns nil if given err is nil. 84 // The parameter `skip` specifies the stack callers skipped amount. 85 func WrapCodeSkip(code gcode.Code, skip int, err error, text ...string) error { 86 if err == nil { 87 return nil 88 } 89 return &Error{ 90 error: err, 91 stack: callers(skip), 92 text: strings.Join(text, commaSeparatorSpace), 93 code: code, 94 } 95 } 96 97 // WrapCodeSkipf wraps error with code and text that is formatted with given format and args. 98 // It returns nil if given err is nil. 99 // The parameter `skip` specifies the stack callers skipped amount. 100 func WrapCodeSkipf(code gcode.Code, skip int, err error, format string, args ...interface{}) error { 101 if err == nil { 102 return nil 103 } 104 return &Error{ 105 error: err, 106 stack: callers(skip), 107 text: fmt.Sprintf(format, args...), 108 code: code, 109 } 110 } 111 112 // Code returns the error code of `current error`. 113 // It returns `CodeNil` if it has no error code neither it does not implement interface Code. 114 func Code(err error) gcode.Code { 115 if err == nil { 116 return gcode.CodeNil 117 } 118 if e, ok := err.(ICode); ok { 119 return e.Code() 120 } 121 if e, ok := err.(IUnwrap); ok { 122 return Code(e.Unwrap()) 123 } 124 return gcode.CodeNil 125 } 126 127 // HasCode checks and reports whether `err` has `code` in its chaining errors. 128 func HasCode(err error, code gcode.Code) bool { 129 if err == nil { 130 return false 131 } 132 if e, ok := err.(ICode); ok && code == e.Code() { 133 return true 134 } 135 if e, ok := err.(IUnwrap); ok { 136 return HasCode(e.Unwrap(), code) 137 } 138 return false 139 }