gitee.com/hongliu9527/go-tools@v0.0.8/errors/gerror/gerror_api_code.go (about) 1 /* 2 * @Author: hongliu 3 * @Date: 2022-12-29 10:51:12 4 * @LastEditors: hongliu 5 * @LastEditTime: 2022-12-29 11:27:00 6 * @FilePath: \go-tools\errors\gerror\gerror_api_code.go 7 * @Description: 带编码的api调用相关错误 8 * 9 * Copyright (c) 2022 by 洪流, All Rights Reserved. 10 */ 11 12 package gerror 13 14 import ( 15 "fmt" 16 "strings" 17 18 "gitee.com/hongliu9527/go-tools/errors/gcode" 19 ) 20 21 // NewCode creates and returns an error that has error code and given text. 22 func NewCode(code gcode.Code, text ...string) error { 23 return &Error{ 24 stack: callers(), 25 text: strings.Join(text, commaSeparatorSpace), 26 code: code, 27 } 28 } 29 30 // NewCodef returns an error that has error code and formats as the given format and args. 31 func NewCodef(code gcode.Code, format string, args ...interface{}) error { 32 return &Error{ 33 stack: callers(), 34 text: fmt.Sprintf(format, args...), 35 code: code, 36 } 37 } 38 39 // NewCodeSkip creates and returns an error which has error code and is formatted from given text. 40 // The parameter `skip` specifies the stack callers skipped amount. 41 func NewCodeSkip(code gcode.Code, skip int, text ...string) error { 42 return &Error{ 43 stack: callers(skip), 44 text: strings.Join(text, commaSeparatorSpace), 45 code: code, 46 } 47 } 48 49 // NewCodeSkipf returns an error that has error code and formats as the given format and args. 50 // The parameter `skip` specifies the stack callers skipped amount. 51 func NewCodeSkipf(code gcode.Code, skip int, format string, args ...interface{}) error { 52 return &Error{ 53 stack: callers(skip), 54 text: fmt.Sprintf(format, args...), 55 code: code, 56 } 57 } 58 59 // WrapCode wraps error with code and text. 60 // It returns nil if given err is nil. 61 func WrapCode(code gcode.Code, err error, text ...string) error { 62 if err == nil { 63 return nil 64 } 65 return &Error{ 66 error: err, 67 stack: callers(), 68 text: strings.Join(text, commaSeparatorSpace), 69 code: code, 70 } 71 } 72 73 // WrapCodef wraps error with code and format specifier. 74 // It returns nil if given `err` is nil. 75 func WrapCodef(code gcode.Code, err error, format string, args ...interface{}) error { 76 if err == nil { 77 return nil 78 } 79 return &Error{ 80 error: err, 81 stack: callers(), 82 text: fmt.Sprintf(format, args...), 83 code: code, 84 } 85 } 86 87 // WrapCodeSkip wraps error with code and text. 88 // It returns nil if given err is nil. 89 // The parameter `skip` specifies the stack callers skipped amount. 90 func WrapCodeSkip(code gcode.Code, skip int, err error, text ...string) error { 91 if err == nil { 92 return nil 93 } 94 return &Error{ 95 error: err, 96 stack: callers(skip), 97 text: strings.Join(text, commaSeparatorSpace), 98 code: code, 99 } 100 } 101 102 // WrapCodeSkipf wraps error with code and text that is formatted with given format and args. 103 // It returns nil if given err is nil. 104 // The parameter `skip` specifies the stack callers skipped amount. 105 func WrapCodeSkipf(code gcode.Code, skip int, err error, format string, args ...interface{}) error { 106 if err == nil { 107 return nil 108 } 109 return &Error{ 110 error: err, 111 stack: callers(skip), 112 text: fmt.Sprintf(format, args...), 113 code: code, 114 } 115 } 116 117 // Code returns the error code of current error. 118 // It returns `CodeNil` if it has no error code neither it does not implement interface Code. 119 func Code(err error) gcode.Code { 120 if err == nil { 121 return gcode.CodeNil 122 } 123 if e, ok := err.(ICode); ok { 124 return e.Code() 125 } 126 if e, ok := err.(IUnwrap); ok { 127 return Code(e.Unwrap()) 128 } 129 return gcode.CodeNil 130 } 131 132 // HasCode checks and reports whether `err` has `code` in its chaining errors. 133 func HasCode(err error, code gcode.Code) bool { 134 if err == nil { 135 return false 136 } 137 if e, ok := err.(ICode); ok { 138 return code == e.Code() 139 } 140 if e, ok := err.(IUnwrap); ok { 141 return HasCode(e.Unwrap(), code) 142 } 143 return false 144 }