-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
54 lines (46 loc) · 910 Bytes
/
error.go
File metadata and controls
54 lines (46 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package funnelfox
import (
"fmt"
)
// Error SDK 错误类型
type Error struct {
Message string
Err error
}
func (e *Error) Error() string {
if e.Err != nil {
return fmt.Sprintf("%s: %v", e.Message, e.Err)
}
return e.Message
}
func (e *Error) Unwrap() error {
return e.Err
}
// NewError 创建新错误
func NewError(message string) *Error {
return &Error{Message: message}
}
// WrapError 包装错误
func WrapError(err error, message string) *Error {
return &Error{
Message: message,
Err: err,
}
}
// WrapErrorf 格式化包装错误
func WrapErrorf(err error, format string, args ...any) *Error {
return &Error{
Message: fmt.Sprintf(format, args...),
Err: err,
}
}
// AsError 将标准 error 转换为 *Error
func AsError(err error) *Error {
if err == nil {
return nil
}
if e, ok := err.(*Error); ok {
return e
}
return WrapError(err, "funnelfox error")
}