1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -05:00

simplify error creation

This commit is contained in:
Darien Raymond 2018-09-30 18:39:53 +02:00
parent 00ea6e3cb2
commit d55fbd7f8d
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
48 changed files with 208 additions and 87 deletions

View File

@ -2,6 +2,8 @@ package commander
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("App", "Commander")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package dispatcher
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("App", "Dispatcher")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,4 +2,8 @@ package dns
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("App", "DNS") }
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package command
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("App", "Log", "Command")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,4 +2,8 @@ package log
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("App", "Log") }
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,4 +2,8 @@ package policy
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("App", "Policy") }
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package command
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("App", "Proxyman", "Command")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package inbound
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("App", "Proxyman", "Inbound")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package mux
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("App", "Proxyman", "Mux")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package outbound
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("App", "Proxyman", "Outbound")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,4 +2,8 @@ package router
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("App", "Router") }
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package command
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("App", "Stats", "Command")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,4 +2,8 @@ package stats
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("App", "Stats") }
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,4 +2,8 @@ package buf
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Buf") }
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,4 +2,8 @@ package crypto
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Crypto") }
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,4 +2,8 @@ package common
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Common") }
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -3,47 +3,39 @@
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"v2ray.com/core/common"
"path/filepath"
)
var (
pkg = flag.String("pkg", "", "Target package")
path = flag.String("path", "", "Path")
)
func getCurrentPkg() (string, error) {
path, err := os.Getwd()
if err != nil {
return "", err
}
return filepath.Base(path), nil
}
func main() {
flag.Parse()
if len(*pkg) == 0 {
panic("Package is not specified.")
pkg, err := getCurrentPkg()
if err != nil {
log.Fatal("Failed to get current package: ", err.Error())
return
}
if len(*path) == 0 {
panic("Path is not specified.")
}
paths := strings.Split(*path, ",")
for i := range paths {
paths[i] = "\"" + paths[i] + "\""
}
pathStr := strings.Join(paths, ", ")
file, err := os.OpenFile("errors.generated.go", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
log.Fatalf("Failed to generate errors.generated.go: %v", err)
return
}
common.Must2(fmt.Fprintln(file, "package", *pkg))
common.Must2(fmt.Fprintln(file, ""))
common.Must2(fmt.Fprintln(file, "import \"v2ray.com/core/common/errors\""))
common.Must2(fmt.Fprintln(file, ""))
common.Must2(fmt.Fprintln(file, "func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("+pathStr+") }"))
fmt.Fprintln(file, "package", pkg)
fmt.Fprintln(file, "")
fmt.Fprintln(file, "import \"v2ray.com/core/common/errors\"")
fmt.Fprintln(file, "")
fmt.Fprintln(file, "type errPathObjHolder struct {}")
fmt.Fprintln(file, "func newError(values ...interface{}) *errors.Error { return errors.New(values...).WithPathObj(errPathObjHolder{}) }")
common.Must(file.Close())
file.Close()
}

View File

@ -3,7 +3,7 @@ package errors // import "v2ray.com/core/common/errors"
import (
"os"
"strings"
"reflect"
"v2ray.com/core/common/log"
"v2ray.com/core/common/serial"
@ -20,21 +20,34 @@ type hasSeverity interface {
// Error is an error object with underlying error.
type Error struct {
pathObj interface{}
prefix []interface{}
path []string
message []interface{}
inner error
severity log.Severity
}
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()
}
// Error implements error.Error().
func (v *Error) Error() string {
msg := serial.Concat(v.message...)
if v.inner != nil {
msg += " > " + v.inner.Error()
}
if len(v.path) > 0 {
msg = strings.Join(v.path, "|") + ": " + msg
path := v.pkgPath()
if len(path) > 0 {
msg = path + ": " + msg
}
var prefix string
@ -98,12 +111,6 @@ func (v *Error) AtError() *Error {
return v.atSeverity(log.Severity_Error)
}
// Path sets the path to the location where this error happens.
func (v *Error) Path(path ...string) *Error {
v.path = path
return v
}
// String returns the string representation of this error.
func (v *Error) String() string {
return v.Error()

View File

@ -28,18 +28,20 @@ func TestError(t *testing.T) {
assert(err.Error(), HasSubstring, "EOF")
}
type e struct{}
func TestErrorMessage(t *testing.T) {
data := []struct {
err error
msg string
}{
{
err: New("a").Base(New("b")).Path("c", "d", "e"),
msg: "c|d|e: a > b",
err: New("a").Base(New("b")).WithPathObj(e{}),
msg: "v2ray.com/core/common/errors_test: a > b",
},
{
err: New("a").Base(New("b").Path("c")).Path("d", "e"),
msg: "d|e: a > c: b",
err: New("a").Base(New("b").WithPathObj(e{})),
msg: "a > v2ray.com/core/common/errors_test: b",
},
}

View File

@ -2,4 +2,8 @@ package net
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Net") }
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package ctlcmd
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Command", "Platform", "CtlCmd")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,4 +2,8 @@ package protocol
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Protocol") }
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package cert
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Protocol", "TLS", "Cert")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,4 +2,8 @@ package retry
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Retry") }
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,4 +2,8 @@ package core
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Core") }
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package external
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Main", "ConfLoader", "External")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,4 +2,8 @@ package main
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Main") }
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,4 +2,8 @@ package json
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Main", "Json") }
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package blackhole
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Proxy", "Blackhole")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package dokodemo
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Proxy", "Dokodemo")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package freedom
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Proxy", "Freedom")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,4 +2,8 @@ package http
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Proxy", "HTTP") }
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package mtproto
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Proxy", "MTProto")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package shadowsocks
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Proxy", "Shadowsocks")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package socks
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Proxy", "Socks")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package encoding
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Proxy", "VMess", "Encoding")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package vmess
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Proxy", "VMess")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package inbound
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Proxy", "VMess", "Inbound")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package outbound
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Proxy", "VMess", "Outbound")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package domainsocket
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Transport", "Internet", "DomainSocket")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package internet
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Transport", "Internet")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package http
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Transport", "Internet", "Headers", "HTTP")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package http
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Transport", "Internet", "HTTP")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package kcp
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Transport", "Internet", "mKCP")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package tcp
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Transport", "Internet", "TCP")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package tls
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Transport", "Internet", "TLS")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package udp
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Transport", "Internet", "UDP")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View File

@ -2,6 +2,8 @@ package websocket
import "v2ray.com/core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Transport", "Internet", "WebSocket")
return errors.New(values...).WithPathObj(errPathObjHolder{})
}