simplify error creation

This commit is contained in:
Darien Raymond
2018-09-30 18:39:53 +02:00
parent 00ea6e3cb2
commit d55fbd7f8d
48 changed files with 208 additions and 87 deletions
+5 -1
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{})
}
+5 -1
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{})
}
+5 -1
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{})
}
+20 -28
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()
}
+17 -10
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()
+6 -4
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",
},
}
+5 -1
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{})
}
+3 -1
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{})
}
+5 -1
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{})
}
+3 -1
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{})
}
+5 -1
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{})
}