Sfoglia il codice sorgente

merge similar error definitions

v2ray 9 anni fa
parent
commit
ad1353ac2f

+ 2 - 4
app/router/config_cache.go

@@ -1,15 +1,13 @@
 package router
 
 import (
-	"errors"
+	"github.com/v2ray/v2ray-core/common"
 )
 
 type ConfigObjectCreator func([]byte) (interface{}, error)
 
 var (
 	configCache map[string]ConfigObjectCreator
-
-	ErrRouterNotFound = errors.New("Router not found.")
 )
 
 func RegisterRouterConfig(strategy string, creator ConfigObjectCreator) error {
@@ -21,7 +19,7 @@ func RegisterRouterConfig(strategy string, creator ConfigObjectCreator) error {
 func CreateRouterConfig(strategy string, data []byte) (interface{}, error) {
 	creator, found := configCache[strategy]
 	if !found {
-		return nil, ErrRouterNotFound
+		return nil, common.ErrObjectNotFound
 	}
 	return creator(data)
 }

+ 6 - 2
app/router/router.go

@@ -3,6 +3,7 @@ package router
 import (
 	"github.com/v2ray/v2ray-core/app"
 	"github.com/v2ray/v2ray-core/common"
+	"github.com/v2ray/v2ray-core/common/log"
 	v2net "github.com/v2ray/v2ray-core/common/net"
 )
 
@@ -24,7 +25,9 @@ var (
 )
 
 func RegisterRouter(name string, factory RouterFactory) error {
-	// TODO: check name
+	if _, found := routerCache[name]; found {
+		return common.ErrDuplicatedName
+	}
 	routerCache[name] = factory
 	return nil
 }
@@ -33,5 +36,6 @@ func CreateRouter(name string, rawConfig interface{}, space app.Space) (Router,
 	if factory, found := routerCache[name]; found {
 		return factory.Create(rawConfig, space)
 	}
-	return nil, ErrRouterNotFound
+	log.Error("Router: not found: ", name)
+	return nil, common.ErrObjectNotFound
 }

+ 2 - 0
common/common.go

@@ -9,6 +9,8 @@ import (
 var (
 	ErrObjectReleased   = errors.New("Object already released.")
 	ErrBadConfiguration = errors.New("Bad configuration.")
+	ErrObjectNotFound   = errors.New("Object not found.")
+	ErrDuplicatedName   = errors.New("Duplicated name.")
 )
 
 // Releasable interface is for those types that can release its members.

+ 3 - 2
common/loader/json_conf.go

@@ -5,6 +5,7 @@ package loader
 import (
 	"encoding/json"
 
+	"github.com/v2ray/v2ray-core/common"
 	"github.com/v2ray/v2ray-core/common/log"
 )
 
@@ -42,7 +43,7 @@ func (this *JSONConfigLoader) Load(raw []byte) (interface{}, string, error) {
 	rawID, found := obj[this.idKey]
 	if !found {
 		log.Error(this.idKey, " not found in JSON content.")
-		return nil, "", ErrConfigIDKeyNotFound
+		return nil, "", common.ErrObjectNotFound
 	}
 	var id string
 	if err := json.Unmarshal(rawID, &id); err != nil {
@@ -53,7 +54,7 @@ func (this *JSONConfigLoader) Load(raw []byte) (interface{}, string, error) {
 		configValue, found := obj[this.configKey]
 		if !found {
 			log.Error(this.configKey, " not found in JSON content.")
-			return nil, "", ErrConfigIDKeyNotFound
+			return nil, "", common.ErrObjectNotFound
 		}
 		rawConfig = configValue
 	}

+ 3 - 4
common/loader/loader.go

@@ -2,12 +2,11 @@ package loader
 
 import (
 	"errors"
+	"github.com/v2ray/v2ray-core/common"
 )
 
 var (
-	ErrConfigIDKeyNotFound = errors.New("Config ID key is not found.")
-	ErrConfigIDExists      = errors.New("Config ID already exists.")
-	ErrUnknownConfigID     = errors.New("Unknown config ID.")
+	ErrUnknownConfigID = errors.New("Unknown config ID.")
 )
 
 type ConfigCreator func() interface{}
@@ -31,7 +30,7 @@ func NewBaseConfigLoader() *BaseConfigLoader {
 
 func (this *BaseConfigLoader) RegisterCreator(id string, creator ConfigCreator) error {
 	if _, found := this.creators[id]; found {
-		return ErrConfigIDExists
+		return common.ErrDuplicatedName
 	}
 
 	this.creators[id] = creator

+ 5 - 9
proxy/registry/handler_cache.go

@@ -1,9 +1,8 @@
 package registry
 
 import (
-	"errors"
-
 	"github.com/v2ray/v2ray-core/app"
+	"github.com/v2ray/v2ray-core/common"
 	"github.com/v2ray/v2ray-core/proxy"
 	"github.com/v2ray/v2ray-core/transport/internet"
 )
@@ -11,14 +10,11 @@ import (
 var (
 	inboundFactories  = make(map[string]InboundHandlerFactory)
 	outboundFactories = make(map[string]OutboundHandlerFactory)
-
-	ErrProxyNotFound = errors.New("Proxy not found.")
-	ErrNameExists    = errors.New("Proxy with the same name already exists.")
 )
 
 func RegisterInboundHandlerCreator(name string, creator InboundHandlerFactory) error {
 	if _, found := inboundFactories[name]; found {
-		return ErrNameExists
+		return common.ErrDuplicatedName
 	}
 	inboundFactories[name] = creator
 	return nil
@@ -32,7 +28,7 @@ func MustRegisterInboundHandlerCreator(name string, creator InboundHandlerFactor
 
 func RegisterOutboundHandlerCreator(name string, creator OutboundHandlerFactory) error {
 	if _, found := outboundFactories[name]; found {
-		return ErrNameExists
+		return common.ErrDuplicatedName
 	}
 	outboundFactories[name] = creator
 	return nil
@@ -47,7 +43,7 @@ func MustRegisterOutboundHandlerCreator(name string, creator OutboundHandlerFact
 func CreateInboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
 	creator, found := inboundFactories[name]
 	if !found {
-		return nil, ErrProxyNotFound
+		return nil, common.ErrObjectNotFound
 	}
 	if meta.StreamSettings == nil {
 		meta.StreamSettings = &internet.StreamSettings{
@@ -70,7 +66,7 @@ func CreateInboundHandler(name string, space app.Space, rawConfig []byte, meta *
 func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
 	creator, found := outboundFactories[name]
 	if !found {
-		return nil, ErrProxyNotFound
+		return nil, common.ErrObjectNotFound
 	}
 	if meta.StreamSettings == nil {
 		meta.StreamSettings = &internet.StreamSettings{

+ 3 - 2
proxy/testing/proxy.go

@@ -3,6 +3,7 @@ package testing
 import (
 	"fmt"
 
+	"github.com/v2ray/v2ray-core/common"
 	"github.com/v2ray/v2ray-core/proxy/registry"
 )
 
@@ -17,7 +18,7 @@ func RegisterInboundConnectionHandlerCreator(prefix string, creator registry.Inb
 	for {
 		name := prefix + randomString()
 		err := registry.RegisterInboundHandlerCreator(name, creator)
-		if err != registry.ErrNameExists {
+		if err != common.ErrDuplicatedName {
 			return name, err
 		}
 	}
@@ -27,7 +28,7 @@ func RegisterOutboundConnectionHandlerCreator(prefix string, creator registry.Ou
 	for {
 		name := prefix + randomString()
 		err := registry.RegisterOutboundHandlerCreator(name, creator)
-		if err != registry.ErrNameExists {
+		if err != common.ErrDuplicatedName {
 			return name, err
 		}
 	}

+ 3 - 7
transport/internet/authenticator.go

@@ -1,8 +1,7 @@
 package internet
 
 import (
-	"errors"
-
+	"github.com/v2ray/v2ray-core/common"
 	"github.com/v2ray/v2ray-core/common/alloc"
 	"github.com/v2ray/v2ray-core/common/loader"
 )
@@ -21,16 +20,13 @@ type AuthenticatorConfig interface {
 }
 
 var (
-	ErrDuplicatedAuthenticator = errors.New("Authenticator already registered.")
-	ErrAuthenticatorNotFound   = errors.New("Authenticator not found.")
-
 	authenticatorCache = make(map[string]AuthenticatorFactory)
 	configCache        loader.ConfigLoader
 )
 
 func RegisterAuthenticator(name string, factory AuthenticatorFactory, configCreator loader.ConfigCreator) error {
 	if _, found := authenticatorCache[name]; found {
-		return ErrDuplicatedAuthenticator
+		return common.ErrDuplicatedName
 	}
 	authenticatorCache[name] = factory
 	return configCache.RegisterCreator(name, configCreator)
@@ -39,7 +35,7 @@ func RegisterAuthenticator(name string, factory AuthenticatorFactory, configCrea
 func CreateAuthenticator(name string, config AuthenticatorConfig) (Authenticator, error) {
 	factory, found := authenticatorCache[name]
 	if !found {
-		return nil, ErrAuthenticatorNotFound
+		return nil, common.ErrObjectNotFound
 	}
 	return factory.Create(config), nil
 }