Files
v2ray_simple/utils/error.go
2022-04-29 18:23:50 +08:00

100 lines
1.8 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"
"errors"
"fmt"
"strconv"
)
var (
ErrNotImplemented = errors.New("not implemented")
ErrNilParameter = errors.New("nil parameter")
ErrNilOrWrongParameter = errors.New("nil or wrong parameter")
ErrWrongParameter = errors.New("wrong parameter")
ErrInvalidData = errors.New("invalid data")
ErrShortRead = errors.New("short read")
ErrHandled = errors.New("handled")
ErrFailed = errors.New("failed") //最无脑的Err, 在能描述清楚错误时不要使用 ErrFailed
)
//nothing special
type NumErr struct {
N int
Prefix string
}
func (ne NumErr) Error() string {
return ne.Prefix + strconv.Itoa(ne.N)
}
//a err with buffer, nothing special
type ErrBuffer struct {
Err error
Buf *bytes.Buffer
}
func (ef ErrBuffer) Unwarp() error {
return ef.Err
}
func (ef ErrBuffer) Error() string {
return ef.Err.Error()
}
// ErrInErr 很适合一个err包含另一个err并且提供附带数据的情况.
type ErrInErr struct {
ErrDesc string
ErrDetail error
Data any
ExtraIs []error
}
func (e ErrInErr) Error() string {
return e.String()
}
func (e ErrInErr) Unwarp() error {
return e.ErrDetail
}
func (e ErrInErr) Is(err error) bool {
if e.ErrDetail == err {
return true
} else if errors.Is(e.ErrDetail, err) {
return true
} else if len(e.ExtraIs) > 0 {
for _, v := range e.ExtraIs {
if errors.Is(v, err) {
return true
}
}
}
return false
}
func (e ErrInErr) String() string {
if e.Data != nil {
if e.ErrDetail != nil {
return fmt.Sprintf(" [ %s , Detail: %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 , Detail: %s ] ", e.ErrDesc, e.ErrDetail.Error())
}
return e.ErrDesc
}