2017-02-08 04:01:22 -05:00
|
|
|
// Package errors is a drop-in replacement for Golang lib 'errors'.
|
2021-02-16 15:31:50 -05:00
|
|
|
package errors
|
2016-12-04 03:10:47 -05:00
|
|
|
|
|
|
|
import (
|
2018-04-13 05:25:41 -04:00
|
|
|
"os"
|
2018-09-30 12:39:53 -04:00
|
|
|
"reflect"
|
2018-10-01 06:42:14 -04:00
|
|
|
"strings"
|
2016-12-15 05:05:32 -05:00
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/log"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/serial"
|
2016-12-04 03:10:47 -05:00
|
|
|
)
|
|
|
|
|
2017-02-08 04:01:22 -05:00
|
|
|
type hasInnerError interface {
|
|
|
|
// Inner returns the underlying error of this one.
|
2016-12-04 03:10:47 -05:00
|
|
|
Inner() error
|
|
|
|
}
|
|
|
|
|
2017-04-06 09:13:09 -04:00
|
|
|
type hasSeverity interface {
|
2017-12-10 17:33:23 -05:00
|
|
|
Severity() log.Severity
|
2017-02-21 17:14:07 -05:00
|
|
|
}
|
|
|
|
|
2017-02-08 04:01:22 -05:00
|
|
|
// Error is an error object with underlying error.
|
2016-12-04 03:10:47 -05:00
|
|
|
type Error struct {
|
2018-09-30 12:39:53 -04:00
|
|
|
pathObj interface{}
|
2018-06-24 19:09:02 -04:00
|
|
|
prefix []interface{}
|
2017-04-08 15:18:13 -04:00
|
|
|
message []interface{}
|
2017-04-06 09:13:09 -04:00
|
|
|
inner error
|
2017-12-10 17:33:23 -05:00
|
|
|
severity log.Severity
|
2016-12-04 03:10:47 -05:00
|
|
|
}
|
|
|
|
|
2018-09-30 12:39:53 -04:00
|
|
|
func (err *Error) WithPathObj(obj interface{}) *Error {
|
|
|
|
err.pathObj = obj
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err *Error) pkgPath() string {
|
|
|
|
if err.pathObj == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return reflect.TypeOf(err.pathObj).PkgPath()
|
|
|
|
}
|
|
|
|
|
2017-02-08 04:01:22 -05:00
|
|
|
// Error implements error.Error().
|
2018-10-01 06:49:36 -04:00
|
|
|
func (err *Error) Error() string {
|
2018-10-01 06:42:14 -04:00
|
|
|
builder := strings.Builder{}
|
2018-10-01 06:49:36 -04:00
|
|
|
for _, prefix := range err.prefix {
|
2018-10-01 06:42:14 -04:00
|
|
|
builder.WriteByte('[')
|
|
|
|
builder.WriteString(serial.ToString(prefix))
|
|
|
|
builder.WriteString("] ")
|
2017-02-23 19:05:02 -05:00
|
|
|
}
|
2018-10-01 06:42:14 -04:00
|
|
|
|
2018-10-01 06:49:36 -04:00
|
|
|
path := err.pkgPath()
|
2018-09-30 12:39:53 -04:00
|
|
|
if len(path) > 0 {
|
2018-10-01 06:42:14 -04:00
|
|
|
builder.WriteString(path)
|
|
|
|
builder.WriteString(": ")
|
2017-04-06 09:13:09 -04:00
|
|
|
}
|
2018-06-24 19:09:02 -04:00
|
|
|
|
2018-10-01 06:49:36 -04:00
|
|
|
msg := serial.Concat(err.message...)
|
2018-10-01 06:42:14 -04:00
|
|
|
builder.WriteString(msg)
|
|
|
|
|
2018-10-01 06:49:36 -04:00
|
|
|
if err.inner != nil {
|
2018-10-01 06:42:14 -04:00
|
|
|
builder.WriteString(" > ")
|
2018-10-01 06:49:36 -04:00
|
|
|
builder.WriteString(err.inner.Error())
|
2018-06-24 19:09:02 -04:00
|
|
|
}
|
|
|
|
|
2018-10-01 06:42:14 -04:00
|
|
|
return builder.String()
|
2016-12-04 03:10:47 -05:00
|
|
|
}
|
|
|
|
|
2017-02-08 04:01:22 -05:00
|
|
|
// Inner implements hasInnerError.Inner()
|
2018-10-01 06:49:36 -04:00
|
|
|
func (err *Error) Inner() error {
|
|
|
|
if err.inner == nil {
|
2016-12-04 03:10:47 -05:00
|
|
|
return nil
|
|
|
|
}
|
2018-10-01 06:49:36 -04:00
|
|
|
return err.inner
|
2016-12-04 03:10:47 -05:00
|
|
|
}
|
|
|
|
|
2018-10-01 06:49:36 -04:00
|
|
|
func (err *Error) Base(e error) *Error {
|
|
|
|
err.inner = e
|
|
|
|
return err
|
2017-02-21 17:14:07 -05:00
|
|
|
}
|
|
|
|
|
2018-10-01 06:49:36 -04:00
|
|
|
func (err *Error) atSeverity(s log.Severity) *Error {
|
|
|
|
err.severity = s
|
|
|
|
return err
|
2017-02-22 04:24:04 -05:00
|
|
|
}
|
|
|
|
|
2018-10-01 06:49:36 -04:00
|
|
|
func (err *Error) Severity() log.Severity {
|
|
|
|
if err.inner == nil {
|
|
|
|
return err.severity
|
2017-02-22 04:24:04 -05:00
|
|
|
}
|
2017-04-06 09:13:09 -04:00
|
|
|
|
2018-10-01 06:49:36 -04:00
|
|
|
if s, ok := err.inner.(hasSeverity); ok {
|
2017-04-06 09:13:09 -04:00
|
|
|
as := s.Severity()
|
2018-10-01 06:49:36 -04:00
|
|
|
if as < err.severity {
|
2017-04-06 09:13:09 -04:00
|
|
|
return as
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 06:49:36 -04:00
|
|
|
return err.severity
|
2017-02-22 04:24:04 -05:00
|
|
|
}
|
|
|
|
|
2017-04-10 08:56:08 -04:00
|
|
|
// AtDebug sets the severity to debug.
|
2018-10-01 06:49:36 -04:00
|
|
|
func (err *Error) AtDebug() *Error {
|
|
|
|
return err.atSeverity(log.Severity_Debug)
|
2017-02-22 04:24:04 -05:00
|
|
|
}
|
|
|
|
|
2017-04-10 08:56:08 -04:00
|
|
|
// AtInfo sets the severity to info.
|
2018-10-01 06:49:36 -04:00
|
|
|
func (err *Error) AtInfo() *Error {
|
|
|
|
return err.atSeverity(log.Severity_Info)
|
2017-04-06 09:13:09 -04:00
|
|
|
}
|
|
|
|
|
2017-04-10 08:56:08 -04:00
|
|
|
// AtWarning sets the severity to warning.
|
2018-10-01 06:49:36 -04:00
|
|
|
func (err *Error) AtWarning() *Error {
|
|
|
|
return err.atSeverity(log.Severity_Warning)
|
2017-04-06 09:13:09 -04:00
|
|
|
}
|
|
|
|
|
2017-04-10 08:56:08 -04:00
|
|
|
// AtError sets the severity to error.
|
2018-10-01 06:49:36 -04:00
|
|
|
func (err *Error) AtError() *Error {
|
|
|
|
return err.atSeverity(log.Severity_Error)
|
2016-12-04 03:10:47 -05:00
|
|
|
}
|
|
|
|
|
2017-12-19 15:28:12 -05:00
|
|
|
// String returns the string representation of this error.
|
2018-10-01 06:49:36 -04:00
|
|
|
func (err *Error) String() string {
|
|
|
|
return err.Error()
|
2017-12-19 15:28:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteToLog writes current error into log.
|
2018-10-01 06:49:36 -04:00
|
|
|
func (err *Error) WriteToLog(opts ...ExportOption) {
|
2018-06-24 19:09:02 -04:00
|
|
|
var holder ExportOptionHolder
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&holder)
|
2018-02-22 09:26:00 -05:00
|
|
|
}
|
2018-06-24 19:09:02 -04:00
|
|
|
|
|
|
|
if holder.SessionID > 0 {
|
2018-10-01 06:49:36 -04:00
|
|
|
err.prefix = append(err.prefix, holder.SessionID)
|
2018-02-22 09:26:00 -05:00
|
|
|
}
|
2018-06-24 19:09:02 -04:00
|
|
|
|
2017-12-19 15:28:12 -05:00
|
|
|
log.Record(&log.GeneralMessage{
|
2018-10-01 06:49:36 -04:00
|
|
|
Severity: GetSeverity(err),
|
|
|
|
Content: err,
|
2017-12-19 15:28:12 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-24 19:09:02 -04:00
|
|
|
type ExportOptionHolder struct {
|
|
|
|
SessionID uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExportOption func(*ExportOptionHolder)
|
|
|
|
|
2017-04-06 09:13:09 -04:00
|
|
|
// New returns a new error object with message formed from given arguments.
|
|
|
|
func New(msg ...interface{}) *Error {
|
|
|
|
return &Error{
|
2017-04-08 15:18:13 -04:00
|
|
|
message: msg,
|
2017-12-10 17:33:23 -05:00
|
|
|
severity: log.Severity_Info,
|
2016-12-04 03:10:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 05:05:32 -05:00
|
|
|
// Cause returns the root cause of this error.
|
2016-12-04 03:10:47 -05:00
|
|
|
func Cause(err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-04-13 05:25:41 -04:00
|
|
|
L:
|
2016-12-04 03:10:47 -05:00
|
|
|
for {
|
2018-04-13 05:25:41 -04:00
|
|
|
switch inner := err.(type) {
|
|
|
|
case hasInnerError:
|
|
|
|
if inner.Inner() == 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
|
|
|
|
default:
|
|
|
|
break L
|
2016-12-04 03:10:47 -05:00
|
|
|
}
|
2017-02-21 17:14:07 -05:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-19 15:28:12 -05:00
|
|
|
// GetSeverity returns the actual severity of the error, including inner errors.
|
2017-12-10 17:33:23 -05:00
|
|
|
func GetSeverity(err error) log.Severity {
|
2017-04-06 09:13:09 -04:00
|
|
|
if s, ok := err.(hasSeverity); ok {
|
|
|
|
return s.Severity()
|
2016-12-04 03:10:47 -05:00
|
|
|
}
|
2017-12-10 17:33:23 -05:00
|
|
|
return log.Severity_Info
|
2016-12-04 03:10:47 -05:00
|
|
|
}
|