github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/exceptions/error.go (about) 1 package exceptions 2 3 import ( 4 "context" 5 "errors" 6 "io" 7 "net" 8 "os" 9 "syscall" 10 _ "unsafe" 11 12 F "github.com/sagernet/sing/common/format" 13 ) 14 15 type Handler interface { 16 NewError(ctx context.Context, err error) 17 } 18 19 type MultiError interface { 20 Unwrap() []error 21 } 22 23 func New(message ...any) error { 24 return errors.New(F.ToString(message...)) 25 } 26 27 func Cause(cause error, message ...any) error { 28 if cause == nil { 29 panic("cause on an nil error") 30 } 31 return &causeError{F.ToString(message...), cause} 32 } 33 34 func Extend(cause error, message ...any) error { 35 if cause == nil { 36 panic("extend on an nil error") 37 } 38 return &extendedError{F.ToString(message...), cause} 39 } 40 41 func IsClosedOrCanceled(err error) bool { 42 return IsMulti(err, io.EOF, net.ErrClosed, io.ErrClosedPipe, os.ErrClosed, syscall.EPIPE, syscall.ECONNRESET, context.Canceled, context.DeadlineExceeded) 43 } 44 45 func IsClosed(err error) bool { 46 return IsMulti(err, io.EOF, net.ErrClosed, io.ErrClosedPipe, os.ErrClosed, syscall.EPIPE, syscall.ECONNRESET) 47 } 48 49 func IsCanceled(err error) bool { 50 return IsMulti(err, context.Canceled, context.DeadlineExceeded) 51 }