From 5bbbdc05de801dbbc5e53b110e87d1e9e73da8a1 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Thu, 15 Dec 2016 11:05:32 +0100 Subject: [PATCH] errors.Format --- common/errors/errors.go | 8 +++++++- proxy/socks/protocol/socks.go | 3 +-- proxy/socks/protocol/udp.go | 3 +-- proxy/vmess/encoding/client.go | 4 ++-- tools/conf/transport_internet.go | 5 ++--- tools/geoip/geoip_gen.go | 3 ++- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/common/errors/errors.go b/common/errors/errors.go index 890bd88a2..36a03d39a 100644 --- a/common/errors/errors.go +++ b/common/errors/errors.go @@ -2,6 +2,7 @@ package errors import ( "fmt" + "v2ray.com/core/common/serial" ) @@ -27,7 +28,7 @@ func (v *Error) Inner() error { func New(msg ...interface{}) error { return &Error{ - message: serial.ToString(msg), + message: serial.Concat(msg), } } @@ -37,6 +38,11 @@ func Base(err error) ErrorBuilder { } } +func Format(format string, values ...interface{}) error { + return New(fmt.Sprintf(format, values...)) +} + +// Cause returns the root cause of this error. func Cause(err error) error { if err == nil { return nil diff --git a/proxy/socks/protocol/socks.go b/proxy/socks/protocol/socks.go index 0a17d3819..3e5ec250d 100644 --- a/proxy/socks/protocol/socks.go +++ b/proxy/socks/protocol/socks.go @@ -1,7 +1,6 @@ package protocol import ( - "fmt" "io" "v2ray.com/core/common/buf" @@ -228,7 +227,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) { return } default: - err = fmt.Errorf("Socks: Unexpected address type %d", request.AddrType) + err = errors.Format("Socks: Unexpected address type %d", request.AddrType) return } diff --git a/proxy/socks/protocol/udp.go b/proxy/socks/protocol/udp.go index 89974b6dc..f4e142a6d 100644 --- a/proxy/socks/protocol/udp.go +++ b/proxy/socks/protocol/udp.go @@ -1,7 +1,6 @@ package protocol import ( - "fmt" "v2ray.com/core/common/buf" "v2ray.com/core/common/errors" v2net "v2ray.com/core/common/net" @@ -79,7 +78,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) { request.Address = v2net.ParseAddress(domain) dataBegin = 5 + domainLength + 2 default: - return nil, fmt.Errorf("Socks|UDP: Unknown address type %d", addrType) + return nil, errors.Format("Socks|UDP: Unknown address type %d", addrType) } if len(packet) > dataBegin { diff --git a/proxy/vmess/encoding/client.go b/proxy/vmess/encoding/client.go index e260d8f9d..d75fdf48a 100644 --- a/proxy/vmess/encoding/client.go +++ b/proxy/vmess/encoding/client.go @@ -5,7 +5,6 @@ import ( "crypto/cipher" "crypto/md5" "crypto/rand" - "fmt" "hash/fnv" "io" @@ -14,6 +13,7 @@ import ( "v2ray.com/core/common/buf" "v2ray.com/core/common/crypto" "v2ray.com/core/common/dice" + "v2ray.com/core/common/errors" "v2ray.com/core/common/log" v2net "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" @@ -187,7 +187,7 @@ func (v *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.Respon } if buffer[0] != v.responseHeader { - return nil, fmt.Errorf("VMess|Client: Unexpected response header. Expecting %d but actually %d", v.responseHeader, buffer[0]) + return nil, errors.Format("VMess|Client: Unexpected response header. Expecting %d but actually %d", v.responseHeader, buffer[0]) } header := &protocol.ResponseHeader{ diff --git a/tools/conf/transport_internet.go b/tools/conf/transport_internet.go index 38a00d134..36f72b1ba 100644 --- a/tools/conf/transport_internet.go +++ b/tools/conf/transport_internet.go @@ -2,7 +2,6 @@ package conf import ( "encoding/json" - "fmt" "io/ioutil" "strings" @@ -46,14 +45,14 @@ func (v *KCPConfig) Build() (*loader.TypedSettings, error) { if v.Mtu != nil { mtu := *v.Mtu if mtu < 576 || mtu > 1460 { - return nil, fmt.Errorf("KCP|Config: Invalid MTU size: %d", mtu) + return nil, errors.Format("KCP|Config: Invalid MTU size: %d", mtu) } config.Mtu = &kcp.MTU{Value: mtu} } if v.Tti != nil { tti := *v.Tti if tti < 10 || tti > 100 { - return nil, fmt.Errorf("KCP|Config: Invalid TTI: %d", tti) + return nil, errors.Format("KCP|Config: Invalid TTI: %d", tti) } config.Tti = &kcp.TTI{Value: tti} } diff --git a/tools/geoip/geoip_gen.go b/tools/geoip/geoip_gen.go index cd992c975..5a7db83e5 100644 --- a/tools/geoip/geoip_gen.go +++ b/tools/geoip/geoip_gen.go @@ -14,6 +14,7 @@ import ( "strings" "v2ray.com/core/app/router" + "v2ray.com/core/common/errors" "v2ray.com/core/tools/geoip" "github.com/golang/protobuf/proto" @@ -29,7 +30,7 @@ func main() { panic(err) } if resp.StatusCode != 200 { - panic(fmt.Errorf("Unexpected status %d", resp.StatusCode)) + panic(errors.Format("Unexpected status %d", resp.StatusCode)) } defer resp.Body.Close() scanner := bufio.NewScanner(resp.Body)