Refactor to stdlib error unwrap method

This commit is contained in:
世界 2022-06-01 11:03:06 +08:00
parent f1d753f069
commit c4a307e84d
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 8 additions and 19 deletions

View file

@ -48,7 +48,7 @@ func (e readError) Error() string {
return e.error.Error() return e.error.Error()
} }
func (e readError) Inner() error { func (e readError) Unwrap() error {
return e.error return e.error
} }
@ -66,7 +66,7 @@ func (e writeError) Error() string {
return e.error.Error() return e.error.Error()
} }
func (e writeError) Inner() error { func (e writeError) Unwrap() error {
return e.error return e.error
} }

View file

@ -2,7 +2,6 @@
package errors // import "github.com/xtls/xray-core/common/errors" package errors // import "github.com/xtls/xray-core/common/errors"
import ( import (
"os"
"reflect" "reflect"
"strings" "strings"
@ -13,8 +12,8 @@ import (
const trim = len("github.com/xtls/xray-core/") const trim = len("github.com/xtls/xray-core/")
type hasInnerError interface { type hasInnerError interface {
// Inner returns the underlying error of this one. // Unwrap returns the underlying error of this one.
Inner() error Unwrap() error
} }
type hasSeverity interface { type hasSeverity interface {
@ -72,8 +71,8 @@ func (err *Error) Error() string {
return builder.String() return builder.String()
} }
// Inner implements hasInnerError.Inner() // Unwrap implements hasInnerError.Unwrap()
func (err *Error) Inner() error { func (err *Error) Unwrap() error {
if err.inner == nil { if err.inner == nil {
return nil return nil
} }
@ -171,20 +170,10 @@ L:
for { for {
switch inner := err.(type) { switch inner := err.(type) {
case hasInnerError: case hasInnerError:
if inner.Inner() == nil { if inner.Unwrap() == nil {
break L break L
} }
err = inner.Inner() err = inner.Unwrap()
case *os.PathError:
if inner.Err == nil {
break L
}
err = inner.Err
case *os.SyscallError:
if inner.Err == nil {
break L
}
err = inner.Err
default: default:
break L break L
} }