1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-04 04:55:23 +00:00
v2fly/common/errors/errors.go

191 lines
3.4 KiB
Go
Raw Normal View History

2017-02-08 09:01:22 +00:00
// Package errors is a drop-in replacement for Golang lib 'errors'.
2016-12-04 08:10:47 +00:00
package errors
import (
2018-02-22 14:26:00 +00:00
"context"
2017-04-06 13:13:09 +00:00
"strings"
2016-12-15 10:05:32 +00:00
2017-12-10 22:33:23 +00:00
"v2ray.com/core/common/log"
2016-12-04 08:10:47 +00:00
"v2ray.com/core/common/serial"
2018-02-22 14:26:00 +00:00
"v2ray.com/core/common/session"
2016-12-04 08:10:47 +00:00
)
2017-02-08 09:01:22 +00:00
type hasInnerError interface {
// Inner returns the underlying error of this one.
2016-12-04 08:10:47 +00:00
Inner() error
}
2017-04-06 13:13:09 +00:00
type hasSeverity interface {
2017-12-10 22:33:23 +00:00
Severity() log.Severity
2017-02-21 22:14:07 +00:00
}
2018-02-22 14:26:00 +00:00
type hasContext interface {
Context() context.Context
}
2017-02-08 09:01:22 +00:00
// Error is an error object with underlying error.
2016-12-04 08:10:47 +00:00
type Error struct {
2017-04-08 19:18:13 +00:00
message []interface{}
2017-04-06 13:13:09 +00:00
inner error
2017-12-10 22:33:23 +00:00
severity log.Severity
2017-04-06 13:13:09 +00:00
path []string
2018-02-22 14:26:00 +00:00
ctx context.Context
2016-12-04 08:10:47 +00:00
}
2017-02-08 09:01:22 +00:00
// Error implements error.Error().
2017-04-06 13:13:09 +00:00
func (v *Error) Error() string {
2017-04-08 22:31:06 +00:00
msg := serial.Concat(v.message...)
2017-02-24 00:05:02 +00:00
if v.inner != nil {
msg += " > " + v.inner.Error()
}
2017-04-06 13:13:09 +00:00
if len(v.path) > 0 {
msg = strings.Join(v.path, "|") + ": " + msg
}
2017-02-24 00:05:02 +00:00
return msg
2016-12-04 08:10:47 +00:00
}
2017-02-08 09:01:22 +00:00
// Inner implements hasInnerError.Inner()
2017-04-06 13:13:09 +00:00
func (v *Error) Inner() error {
2016-12-04 08:10:47 +00:00
if v.inner == nil {
return nil
}
return v.inner
}
2017-04-06 13:13:09 +00:00
func (v *Error) Base(err error) *Error {
v.inner = err
return v
2017-02-21 22:14:07 +00:00
}
2018-02-22 14:26:00 +00:00
func (v *Error) WithContext(ctx context.Context) *Error {
v.ctx = ctx
return v
}
func (v *Error) Context() context.Context {
if v.ctx != nil {
return v.ctx
}
if v.inner == nil {
return nil
}
if c, ok := v.inner.(hasContext); ok {
return c.Context()
}
return nil
}
2017-12-10 22:33:23 +00:00
func (v *Error) atSeverity(s log.Severity) *Error {
2017-04-06 13:13:09 +00:00
v.severity = s
return v
}
2017-12-10 22:33:23 +00:00
func (v *Error) Severity() log.Severity {
2017-04-06 13:13:09 +00:00
if v.inner == nil {
return v.severity
}
2017-04-06 13:13:09 +00:00
if s, ok := v.inner.(hasSeverity); ok {
as := s.Severity()
2017-12-10 22:33:23 +00:00
if as < v.severity {
2017-04-06 13:13:09 +00:00
return as
}
}
return v.severity
}
2017-04-10 12:56:08 +00:00
// AtDebug sets the severity to debug.
2017-04-06 13:13:09 +00:00
func (v *Error) AtDebug() *Error {
2017-12-10 22:33:23 +00:00
return v.atSeverity(log.Severity_Debug)
}
2017-04-10 12:56:08 +00:00
// AtInfo sets the severity to info.
2017-04-06 13:13:09 +00:00
func (v *Error) AtInfo() *Error {
2017-12-10 22:33:23 +00:00
return v.atSeverity(log.Severity_Info)
2017-04-06 13:13:09 +00:00
}
2017-04-10 12:56:08 +00:00
// AtWarning sets the severity to warning.
2017-04-06 13:13:09 +00:00
func (v *Error) AtWarning() *Error {
2017-12-10 22:33:23 +00:00
return v.atSeverity(log.Severity_Warning)
2017-04-06 13:13:09 +00:00
}
2017-04-10 12:56:08 +00:00
// AtError sets the severity to error.
2017-04-06 13:13:09 +00:00
func (v *Error) AtError() *Error {
2017-12-10 22:33:23 +00:00
return v.atSeverity(log.Severity_Error)
2016-12-04 08:10:47 +00:00
}
2017-04-10 12:56:08 +00:00
// Path sets the path to the location where this error happens.
2017-04-06 13:13:09 +00:00
func (v *Error) Path(path ...string) *Error {
v.path = path
return v
}
2017-12-19 20:28:12 +00:00
// String returns the string representation of this error.
func (v *Error) String() string {
return v.Error()
}
// WriteToLog writes current error into log.
func (v *Error) WriteToLog() {
2018-02-22 14:26:00 +00:00
ctx := v.Context()
var sid session.ID
if ctx != nil {
sid = session.IDFromContext(ctx)
}
var c interface{} = v
if sid > 0 {
c = sessionLog{
id: sid,
content: v,
}
}
2017-12-19 20:28:12 +00:00
log.Record(&log.GeneralMessage{
Severity: GetSeverity(v),
2018-02-22 14:26:00 +00:00
Content: c,
2017-12-19 20:28:12 +00:00
})
}
2017-04-06 13:13:09 +00:00
// New returns a new error object with message formed from given arguments.
func New(msg ...interface{}) *Error {
return &Error{
2017-04-08 19:18:13 +00:00
message: msg,
2017-12-10 22:33:23 +00:00
severity: log.Severity_Info,
2016-12-04 08:10:47 +00:00
}
}
2016-12-15 10:05:32 +00:00
// Cause returns the root cause of this error.
2016-12-04 08:10:47 +00:00
func Cause(err error) error {
if err == nil {
return nil
}
for {
2017-02-08 09:01:22 +00:00
inner, ok := err.(hasInnerError)
2017-02-21 22:14:07 +00:00
if !ok || inner.Inner() == nil {
2016-12-04 08:10:47 +00:00
break
}
2017-02-21 22:14:07 +00:00
err = inner.Inner()
}
return err
}
2017-12-19 20:28:12 +00:00
// GetSeverity returns the actual severity of the error, including inner errors.
2017-12-10 22:33:23 +00:00
func GetSeverity(err error) log.Severity {
2017-04-06 13:13:09 +00:00
if s, ok := err.(hasSeverity); ok {
return s.Severity()
2016-12-04 08:10:47 +00:00
}
2017-12-10 22:33:23 +00:00
return log.Severity_Info
2016-12-04 08:10:47 +00:00
}
2018-02-22 14:26:00 +00:00
type sessionLog struct {
id session.ID
content interface{}
}
func (s sessionLog) String() string {
return serial.Concat("[", s.id, "] ", s.content)
}