Browse Source

simplify error creation

pull/1291/head
Darien Raymond 6 years ago
parent
commit
d55fbd7f8d
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
  1. 4
      app/commander/errors.generated.go
  2. 4
      app/dispatcher/errors.generated.go
  3. 6
      app/dns/errors.generated.go
  4. 4
      app/log/command/errors.generated.go
  5. 6
      app/log/errors.generated.go
  6. 6
      app/policy/errors.generated.go
  7. 4
      app/proxyman/command/errors.generated.go
  8. 4
      app/proxyman/inbound/errors.generated.go
  9. 4
      app/proxyman/mux/errors.generated.go
  10. 4
      app/proxyman/outbound/errors.generated.go
  11. 6
      app/router/errors.generated.go
  12. 4
      app/stats/command/errors.generated.go
  13. 6
      app/stats/errors.generated.go
  14. 6
      common/buf/errors.generated.go
  15. 6
      common/crypto/errors.generated.go
  16. 6
      common/errors.generated.go
  17. 48
      common/errors/errorgen/main.go
  18. 27
      common/errors/errors.go
  19. 10
      common/errors/errors_test.go
  20. 6
      common/net/errors.generated.go
  21. 4
      common/platform/ctlcmd/errors.generated.go
  22. 6
      common/protocol/errors.generated.go
  23. 4
      common/protocol/tls/cert/errors.generated.go
  24. 6
      common/retry/errors.generated.go
  25. 6
      errors.generated.go
  26. 4
      main/confloader/external/errors.generated.go
  27. 6
      main/errors.generated.go
  28. 6
      main/json/errors.generated.go
  29. 4
      proxy/blackhole/errors.generated.go
  30. 4
      proxy/dokodemo/errors.generated.go
  31. 4
      proxy/freedom/errors.generated.go
  32. 6
      proxy/http/errors.generated.go
  33. 4
      proxy/mtproto/errors.generated.go
  34. 4
      proxy/shadowsocks/errors.generated.go
  35. 4
      proxy/socks/errors.generated.go
  36. 4
      proxy/vmess/encoding/errors.generated.go
  37. 4
      proxy/vmess/errors.generated.go
  38. 4
      proxy/vmess/inbound/errors.generated.go
  39. 4
      proxy/vmess/outbound/errors.generated.go
  40. 4
      transport/internet/domainsocket/errors.generated.go
  41. 4
      transport/internet/errors.generated.go
  42. 4
      transport/internet/headers/http/errors.generated.go
  43. 4
      transport/internet/http/errors.generated.go
  44. 4
      transport/internet/kcp/errors.generated.go
  45. 4
      transport/internet/tcp/errors.generated.go
  46. 4
      transport/internet/tls/errors.generated.go
  47. 4
      transport/internet/udp/errors.generated.go
  48. 4
      transport/internet/websocket/errors.generated.go

4
app/commander/errors.generated.go

@ -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{})
}

4
app/dispatcher/errors.generated.go

@ -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{})
}

6
app/dns/errors.generated.go

@ -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{})
}

4
app/log/command/errors.generated.go

@ -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{})
}

6
app/log/errors.generated.go

@ -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{})
}

6
app/policy/errors.generated.go

@ -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{})
}

4
app/proxyman/command/errors.generated.go

@ -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{})
}

4
app/proxyman/inbound/errors.generated.go

@ -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{})
}

4
app/proxyman/mux/errors.generated.go

@ -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{})
}

4
app/proxyman/outbound/errors.generated.go

@ -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{})
}

6
app/router/errors.generated.go

@ -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{})
}

4
app/stats/command/errors.generated.go

@ -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{})
}

6
app/stats/errors.generated.go

@ -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{})
}

6
common/buf/errors.generated.go

@ -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{})
}

6
common/crypto/errors.generated.go

@ -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{})
}

6
common/errors.generated.go

@ -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{})
}

48
common/errors/errorgen/main.go

@ -3,47 +3,39 @@
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"v2ray.com/core/common"
)
var (
pkg = flag.String("pkg", "", "Target package")
path = flag.String("path", "", "Path")
"path/filepath"
)
func main() {
flag.Parse()
if len(*pkg) == 0 {
panic("Package is not specified.")
}
if len(*path) == 0 {
panic("Path is not specified.")
func getCurrentPkg() (string, error) {
path, err := os.Getwd()
if err != nil {
return "", err
}
return filepath.Base(path), nil
}
paths := strings.Split(*path, ",")
for i := range paths {
paths[i] = "\"" + paths[i] + "\""
func main() {
pkg, err := getCurrentPkg()
if err != nil {
log.Fatal("Failed to get current package: ", err.Error())
return
}
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()
}

27
common/errors/errors.go

@ -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()

10
common/errors/errors_test.go

@ -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",
},
}

6
common/net/errors.generated.go

@ -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{})
}

4
common/platform/ctlcmd/errors.generated.go

@ -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{})
}

6
common/protocol/errors.generated.go

@ -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{})
}

4
common/protocol/tls/cert/errors.generated.go

@ -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{})
}

6
common/retry/errors.generated.go

@ -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{})
}

6
errors.generated.go

@ -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{})
}

4
main/confloader/external/errors.generated.go vendored

@ -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{})
}

6
main/errors.generated.go

@ -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{})
}

6
main/json/errors.generated.go

@ -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{})
}

4
proxy/blackhole/errors.generated.go

@ -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{})
}

4
proxy/dokodemo/errors.generated.go

@ -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{})
}

4
proxy/freedom/errors.generated.go

@ -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{})
}

6
proxy/http/errors.generated.go

@ -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{})
}

4
proxy/mtproto/errors.generated.go

@ -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{})
}

4
proxy/shadowsocks/errors.generated.go

@ -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{})
}

4
proxy/socks/errors.generated.go

@ -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{})
}

4
proxy/vmess/encoding/errors.generated.go

@ -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{})
}

4
proxy/vmess/errors.generated.go

@ -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{})
}

4
proxy/vmess/inbound/errors.generated.go

@ -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{})
}

4
proxy/vmess/outbound/errors.generated.go

@ -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{})
}

4
transport/internet/domainsocket/errors.generated.go

@ -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{})
}

4
transport/internet/errors.generated.go

@ -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{})
}

4
transport/internet/headers/http/errors.generated.go

@ -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{})
}

4
transport/internet/http/errors.generated.go

@ -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{})
}

4
transport/internet/kcp/errors.generated.go

@ -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{})
}

4
transport/internet/tcp/errors.generated.go

@ -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{})
}

4
transport/internet/tls/errors.generated.go

@ -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{})
}

4
transport/internet/udp/errors.generated.go

@ -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{})
}

4
transport/internet/websocket/errors.generated.go

@ -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{})
}

Loading…
Cancel
Save