gitee.com/hongliu9527/go-tools@v0.0.8/errors/gerror/gerror_api.go (about) 1 /* 2 * @Author: hongliu 3 * @Date: 2022-12-29 10:51:12 4 * @LastEditors: hongliu 5 * @LastEditTime: 2022-12-29 11:27:28 6 * @FilePath: \go-tools\errors\gerror\gerror_api.go 7 * @Description:api调用相关错误 8 * 9 * Copyright (c) 2022 by 洪流, All Rights Reserved. 10 */ 11 12 package gerror 13 14 import ( 15 "fmt" 16 17 "gitee.com/hongliu9527/go-tools/errors/gcode" 18 ) 19 20 // New creates and returns an error which is formatted from given text. 21 func New(text string) error { 22 return &Error{ 23 stack: callers(), 24 text: text, 25 code: gcode.CodeNil, 26 } 27 } 28 29 // Newf returns an error that formats as the given format and args. 30 func Newf(format string, args ...interface{}) error { 31 return &Error{ 32 stack: callers(), 33 text: fmt.Sprintf(format, args...), 34 code: gcode.CodeNil, 35 } 36 } 37 38 // NewSkip creates and returns an error which is formatted from given text. 39 // The parameter `skip` specifies the stack callers skipped amount. 40 func NewSkip(skip int, text string) error { 41 return &Error{ 42 stack: callers(skip), 43 text: text, 44 code: gcode.CodeNil, 45 } 46 } 47 48 // NewSkipf returns an error that formats as the given format and args. 49 // The parameter `skip` specifies the stack callers skipped amount. 50 func NewSkipf(skip int, format string, args ...interface{}) error { 51 return &Error{ 52 stack: callers(skip), 53 text: fmt.Sprintf(format, args...), 54 code: gcode.CodeNil, 55 } 56 } 57 58 // Wrap wraps error with text. It returns nil if given err is nil. 59 // Note that it does not lose the error code of wrapped error, as it inherits the error code from it. 60 func Wrap(err error, text string) error { 61 if err == nil { 62 return nil 63 } 64 return &Error{ 65 error: err, 66 stack: callers(), 67 text: text, 68 code: Code(err), 69 } 70 } 71 72 // Wrapf returns an error annotating err with a stack trace at the point Wrapf is called, and the format specifier. 73 // It returns nil if given `err` is nil. 74 // Note that it does not lose the error code of wrapped error, as it inherits the error code from it. 75 func Wrapf(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(err), 84 } 85 } 86 87 // WrapSkip wraps error with text. It returns nil if given err is nil. 88 // The parameter `skip` specifies the stack callers skipped amount. 89 // Note that it does not lose the error code of wrapped error, as it inherits the error code from it. 90 func WrapSkip(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: text, 98 code: Code(err), 99 } 100 } 101 102 // WrapSkipf wraps error with text that is formatted with given format and args. It returns nil if given err is nil. 103 // The parameter `skip` specifies the stack callers skipped amount. 104 // Note that it does not lose the error code of wrapped error, as it inherits the error code from it. 105 func WrapSkipf(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(err), 114 } 115 }