Skip to content

Commit

Permalink
simplify error creation
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Sep 30, 2018
1 parent 00ea6e3 commit d55fbd7
Show file tree
Hide file tree
Showing 48 changed files with 208 additions and 87 deletions.
4 changes: 3 additions & 1 deletion app/commander/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 3 additions & 1 deletion app/dispatcher/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 5 additions & 1 deletion app/dns/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 3 additions & 1 deletion app/log/command/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 5 additions & 1 deletion app/log/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 5 additions & 1 deletion app/policy/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 3 additions & 1 deletion app/proxyman/command/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 3 additions & 1 deletion app/proxyman/inbound/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 3 additions & 1 deletion app/proxyman/mux/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 3 additions & 1 deletion app/proxyman/outbound/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 5 additions & 1 deletion app/router/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 3 additions & 1 deletion app/stats/command/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 5 additions & 1 deletion app/stats/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 5 additions & 1 deletion common/buf/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 5 additions & 1 deletion common/crypto/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 5 additions & 1 deletion common/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 20 additions & 28 deletions common/errors/errorgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 17 additions & 10 deletions common/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 6 additions & 4 deletions common/errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}

Expand Down
6 changes: 5 additions & 1 deletion common/net/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 3 additions & 1 deletion common/platform/ctlcmd/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 5 additions & 1 deletion common/protocol/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 3 additions & 1 deletion common/protocol/tls/cert/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 5 additions & 1 deletion common/retry/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 5 additions & 1 deletion errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 3 additions & 1 deletion main/confloader/external/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 5 additions & 1 deletion main/errors.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
Loading

0 comments on commit d55fbd7

Please sign in to comment.