Files
v2ray_simple/utils/error.go
2022-03-29 21:58:23 +08:00

88 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utils
import (
"bytes"
"fmt"
"strconv"
)
//没啥特殊的
type NumErr struct {
N int
Prefix string
}
func (ne NumErr) Error() string {
return ne.Prefix + strconv.Itoa(ne.N)
}
//就是带个buffer的普通ErrInErr没啥特殊的
type ErrFirstBuffer struct {
Err error
First *bytes.Buffer
}
func (ef ErrFirstBuffer) Unwarp() error {
return ef.Err
}
func (ef ErrFirstBuffer) Error() string {
return ef.Err.Error()
}
// 返回结构体,而不是指针, 这样可以避免内存逃逸到堆
func NewErr(desc string, e error) ErrInErr {
return ErrInErr{
ErrDesc: desc,
ErrDetail: e,
}
}
// 返回结构体,而不是指针, 这样可以避免内存逃逸到堆
func NewDataErr(desc string, e error, data interface{}) ErrInErr {
return ErrInErr{
ErrDesc: desc,
ErrDetail: e,
Data: data,
}
}
// ErrInErr 很适合一个err包含另一个err并且提供附带数据的情况.
type ErrInErr struct {
ErrDesc string
ErrDetail error
Data any
}
func (e ErrInErr) Error() string {
return e.String()
}
func (e ErrInErr) Unwarp() error {
return e.ErrDetail
}
func (e ErrInErr) String() string {
if e.Data != nil {
if e.ErrDetail != nil {
return fmt.Sprintf("%s : %s, Data: %v", e.ErrDesc, e.ErrDetail.Error(), e.Data)
}
return fmt.Sprintf("%s , Data: %v", e.ErrDesc, e.Data)
}
if e.ErrDetail != nil {
return fmt.Sprintf("%s : %s", e.ErrDesc, e.ErrDetail.Error())
}
return e.ErrDesc
}