From c4a307e84d8693c37d3d33faeaf8f7fde1ca94d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Wed, 1 Jun 2022 11:03:06 +0800 Subject: [PATCH] Refactor to stdlib error unwrap method --- common/buf/copy.go | 4 ++-- common/errors/errors.go | 23 ++++++----------------- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/common/buf/copy.go b/common/buf/copy.go index dfacda36..601771be 100644 --- a/common/buf/copy.go +++ b/common/buf/copy.go @@ -48,7 +48,7 @@ func (e readError) Error() string { return e.error.Error() } -func (e readError) Inner() error { +func (e readError) Unwrap() error { return e.error } @@ -66,7 +66,7 @@ func (e writeError) Error() string { return e.error.Error() } -func (e writeError) Inner() error { +func (e writeError) Unwrap() error { return e.error } diff --git a/common/errors/errors.go b/common/errors/errors.go index 6dfb8a45..ef1dd6f9 100644 --- a/common/errors/errors.go +++ b/common/errors/errors.go @@ -2,7 +2,6 @@ package errors // import "github.com/xtls/xray-core/common/errors" import ( - "os" "reflect" "strings" @@ -13,8 +12,8 @@ import ( const trim = len("github.com/xtls/xray-core/") type hasInnerError interface { - // Inner returns the underlying error of this one. - Inner() error + // Unwrap returns the underlying error of this one. + Unwrap() error } type hasSeverity interface { @@ -72,8 +71,8 @@ func (err *Error) Error() string { return builder.String() } -// Inner implements hasInnerError.Inner() -func (err *Error) Inner() error { +// Unwrap implements hasInnerError.Unwrap() +func (err *Error) Unwrap() error { if err.inner == nil { return nil } @@ -171,20 +170,10 @@ L: for { switch inner := err.(type) { case hasInnerError: - if inner.Inner() == nil { + if inner.Unwrap() == nil { break L } - err = inner.Inner() - case *os.PathError: - if inner.Err == nil { - break L - } - err = inner.Err - case *os.SyscallError: - if inner.Err == nil { - break L - } - err = inner.Err + err = inner.Unwrap() default: break L }