1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-02 09:17:55 -04:00
v2fly/common/errors/errors.go

132 lines
2.2 KiB
Go
Raw Normal View History

2017-02-08 04:01:22 -05:00
// Package errors is a drop-in replacement for Golang lib 'errors'.
2016-12-04 03:10:47 -05:00
package errors
import (
2017-04-06 09:13:09 -04:00
"strings"
2016-12-15 05:05:32 -05:00
2016-12-04 03:10:47 -05:00
"v2ray.com/core/common/serial"
)
2017-04-10 08:44:42 -04:00
// Severity describes how severe the error is.
2017-04-06 09:13:09 -04:00
type Severity int
const (
SeverityDebug Severity = iota
SeverityInfo
SeverityWarning
SeverityError
)
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 {
Severity() 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 {
2017-04-08 15:18:13 -04:00
message []interface{}
2017-04-06 09:13:09 -04:00
inner error
severity Severity
path []string
2016-12-04 03:10:47 -05:00
}
2017-02-08 04:01:22 -05:00
// Error implements error.Error().
2017-04-06 09:13:09 -04:00
func (v *Error) Error() string {
2017-04-08 18:31:06 -04:00
msg := serial.Concat(v.message...)
2017-02-23 19:05:02 -05:00
if v.inner != nil {
msg += " > " + v.inner.Error()
}
2017-04-06 09:13:09 -04:00
if len(v.path) > 0 {
msg = strings.Join(v.path, "|") + ": " + msg
}
2017-02-23 19:05:02 -05:00
return msg
2016-12-04 03:10:47 -05:00
}
2017-02-08 04:01:22 -05:00
// Inner implements hasInnerError.Inner()
2017-04-06 09:13:09 -04:00
func (v *Error) Inner() error {
2016-12-04 03:10:47 -05:00
if v.inner == nil {
return nil
}
return v.inner
}
2017-04-06 09:13:09 -04:00
func (v *Error) Base(err error) *Error {
v.inner = err
return v
2017-02-21 17:14:07 -05:00
}
2017-04-06 09:13:09 -04:00
func (v *Error) atSeverity(s Severity) *Error {
v.severity = s
return v
}
2017-04-06 09:13:09 -04:00
func (v *Error) Severity() Severity {
if v.inner == nil {
return v.severity
}
2017-04-06 09:13:09 -04:00
if s, ok := v.inner.(hasSeverity); ok {
as := s.Severity()
if as > v.severity {
return as
}
}
return v.severity
}
2017-04-06 09:13:09 -04:00
func (v *Error) AtDebug() *Error {
return v.atSeverity(SeverityDebug)
}
2017-04-06 09:13:09 -04:00
func (v *Error) AtInfo() *Error {
return v.atSeverity(SeverityInfo)
}
func (v *Error) AtWarning() *Error {
return v.atSeverity(SeverityWarning)
}
func (v *Error) AtError() *Error {
return v.atSeverity(SeverityError)
2016-12-04 03:10:47 -05:00
}
2017-04-06 09:13:09 -04:00
func (v *Error) Path(path ...string) *Error {
v.path = path
return v
}
// 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-04-06 09:13:09 -04:00
severity: SeverityInfo,
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
}
for {
2017-02-08 04:01:22 -05:00
inner, ok := err.(hasInnerError)
2017-02-21 17:14:07 -05:00
if !ok || inner.Inner() == nil {
2016-12-04 03:10:47 -05:00
break
}
2017-02-21 17:14:07 -05:00
err = inner.Inner()
}
return err
}
2017-04-06 09:13:09 -04:00
func GetSeverity(err error) Severity {
if s, ok := err.(hasSeverity); ok {
return s.Severity()
2016-12-04 03:10:47 -05:00
}
2017-04-06 09:13:09 -04:00
return SeverityInfo
2016-12-04 03:10:47 -05:00
}