1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-05-25 21:49:09 -04:00

refactor error messages

This commit is contained in:
Darien Raymond
2017-04-09 01:43:25 +02:00
parent 8175a751db
commit 35248497d2
141 changed files with 710 additions and 481 deletions

3
common/buf/buf.go Normal file
View File

@@ -0,0 +1,3 @@
package buf
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg buf -path Buf

View File

@@ -0,0 +1,5 @@
package buf
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Buf") }

View File

@@ -14,7 +14,7 @@ type Reader interface {
Read() (*Buffer, error)
}
var ErrReadTimeout = errors.New("Buf: IO timeout.")
var ErrReadTimeout = newError("Buf: IO timeout.")
type TimeoutReader interface {
ReadTimeout(time.Duration) (*Buffer, error)

View File

@@ -2,6 +2,8 @@
// See each sub-package for detail.
package common
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg common -path Common
// Must panics if err is not nil.
func Must(err error) {
if err != nil {

View File

@@ -8,12 +8,11 @@ import (
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/serial"
)
var (
errInsufficientBuffer = errors.New("insufficient buffer")
errInsufficientBuffer = newError("insufficient buffer")
)
type BytesGenerator interface {
@@ -52,7 +51,7 @@ type AEADAuthenticator struct {
func (v *AEADAuthenticator) Open(dst, cipherText []byte) ([]byte, error) {
iv := v.NonceGenerator.Next()
if len(iv) != v.AEAD.NonceSize() {
return nil, errors.New("Crypto:AEADAuthenticator: Invalid nonce size: ", len(iv))
return nil, newError("Crypto:AEADAuthenticator: Invalid nonce size: ", len(iv))
}
additionalData := v.AdditionalDataGenerator.Next()
@@ -62,7 +61,7 @@ func (v *AEADAuthenticator) Open(dst, cipherText []byte) ([]byte, error) {
func (v *AEADAuthenticator) Seal(dst, plainText []byte) ([]byte, error) {
iv := v.NonceGenerator.Next()
if len(iv) != v.AEAD.NonceSize() {
return nil, errors.New("Crypto:AEADAuthenticator: Invalid nonce size: ", len(iv))
return nil, newError("Crypto:AEADAuthenticator: Invalid nonce size: ", len(iv))
}
additionalData := v.AdditionalDataGenerator.Next()
@@ -128,13 +127,13 @@ func (v *AuthenticationReader) nextChunk(mask uint16) error {
return errInsufficientBuffer
}
if size > readerBufferSize-2 {
return errors.New("size too large: ", size).Path("Common", "Crypto", "AuthenticationReader")
return newError("size too large: ", size)
}
if size == v.auth.Overhead() {
return io.EOF
}
if size < v.auth.Overhead() {
return errors.New("invalid packet size: ", size).Path("Common", "Crypto", "AuthenticationReader")
return newError("invalid packet size: ", size)
}
cipherChunk := v.buffer.BytesRange(2, size+2)
plainChunk, err := v.auth.Open(cipherChunk[:0], cipherChunk)

View File

@@ -1,2 +1,4 @@
// Package crypto provides common crypto libraries for V2Ray.
package crypto
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg crypto -path Crypto

View File

@@ -0,0 +1,5 @@
package crypto
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Crypto") }

View File

@@ -1,4 +1,3 @@
// GENERATED CODE. DO NOT MODIFY!
package internal
import "encoding/binary"

View File

@@ -0,0 +1,5 @@
package common
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Common") }

View File

@@ -4,7 +4,6 @@ import (
"net"
"v2ray.com/core/app/log"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/predicate"
)
@@ -95,7 +94,7 @@ func IPAddress(ip []byte) Address {
}
return addr
default:
log.Trace(errors.New("Net: Invalid IP format: ", ip).AtError())
log.Trace(newError("Net: Invalid IP format: ", ip).AtError())
return nil
}
}

View File

@@ -0,0 +1,5 @@
package net
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Net") }

View File

@@ -1,2 +1,4 @@
// Package net contains common network utilities.
package net
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg net -path Net

View File

@@ -3,7 +3,6 @@ package net
import (
"strconv"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/serial"
)
@@ -20,7 +19,7 @@ func PortFromBytes(port []byte) Port {
// @error when the integer is not positive or larger then 65535
func PortFromInt(val uint32) (Port, error) {
if val > 65535 {
return Port(0), errors.New("Net: Invalid port range: ", val)
return Port(0), newError("Net: Invalid port range: ", val)
}
return Port(val), nil
}
@@ -30,7 +29,7 @@ func PortFromInt(val uint32) (Port, error) {
func PortFromString(s string) (Port, error) {
val, err := strconv.ParseUint(s, 10, 32)
if err != nil {
return Port(0), errors.New("Net: Invalid port range: ", s)
return Port(0), newError("Net: Invalid port range: ", s)
}
return PortFromInt(uint32(val))
}

View File

@@ -0,0 +1,5 @@
package protocol
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Protocol") }

View File

@@ -0,0 +1,3 @@
package protocol
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg protocol -path Protocol

View File

@@ -1,15 +1,11 @@
package protocol
import (
"time"
"v2ray.com/core/common/errors"
)
import "time"
var (
ErrAccountMissing = errors.New("Account is not specified.")
ErrNonMessageType = errors.New("Not a protobuf message.")
ErrUnknownAccountType = errors.New("Unknown account type.")
ErrAccountMissing = newError("Account is not specified.")
ErrNonMessageType = newError("Not a protobuf message.")
ErrUnknownAccountType = newError("Unknown account type.")
)
func (v *User) GetTypedAccount() (Account, error) {
@@ -27,7 +23,7 @@ func (v *User) GetTypedAccount() (Account, error) {
if account, ok := rawAccount.(Account); ok {
return account, nil
}
return nil, errors.New("Unknown account type: ", v.Account.Type)
return nil, newError("Unknown account type: ", v.Account.Type)
}
func (v *User) GetSettings() UserSettings {

View File

@@ -0,0 +1,5 @@
package retry
import "v2ray.com/core/common/errors"
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Retry") }

View File

@@ -1,13 +1,13 @@
package retry
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg retry -path Retry
import (
"time"
"v2ray.com/core/common/errors"
)
var (
ErrRetryFailed = errors.New("Retry: All retry attempts failed.")
ErrRetryFailed = newError("Retry: All retry attempts failed.")
)
// Strategy is a way to retry on a specific function.
@@ -38,7 +38,7 @@ func (r *retryer) On(method func() error) error {
<-time.After(time.Duration(delay) * time.Millisecond)
attempt++
}
return errors.New(accumulatedError).Base(ErrRetryFailed)
return newError(accumulatedError).Base(ErrRetryFailed)
}
// Timed returns a retry strategy with fixed interval.

View File

@@ -2,7 +2,6 @@ package common
import (
"context"
"errors"
"reflect"
)
@@ -17,7 +16,7 @@ var (
func RegisterConfig(config interface{}, configCreator ConfigCreator) error {
configType := reflect.TypeOf(config)
if _, found := typeCreatorRegistry[configType]; found {
return errors.New("Common: " + configType.Name() + " is already registered.")
return newError("Common: " + configType.Name() + " is already registered.")
}
typeCreatorRegistry[configType] = configCreator
return nil
@@ -28,7 +27,7 @@ func CreateObject(ctx context.Context, config interface{}) (interface{}, error)
configType := reflect.TypeOf(config)
creator, found := typeCreatorRegistry[configType]
if !found {
return nil, errors.New("Common: " + configType.String() + " is not registered.")
return nil, newError("Common: " + configType.String() + " is not registered.")
}
return creator(ctx, config)
}

View File

@@ -70,7 +70,7 @@ func New() *UUID {
// ParseBytes converts an UUID in byte form to object.
func ParseBytes(b []byte) (*UUID, error) {
if len(b) != 16 {
return nil, errors.New("Invalid UUID: ", b)
return nil, errors.New("invalid UUID: ", b)
}
uuid := new(UUID)
copy(uuid[:], b)
@@ -81,7 +81,7 @@ func ParseBytes(b []byte) (*UUID, error) {
func ParseString(str string) (*UUID, error) {
text := []byte(str)
if len(text) < 32 {
return nil, errors.New("Invalid UUID: ", str)
return nil, errors.New("invalid UUID: ", str)
}
uuid := new(UUID)