merge similar error definitions

pull/2034/head
v2ray 2016-08-18 08:34:21 +02:00
parent 786775ea80
commit ad1353ac2f
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
8 changed files with 27 additions and 30 deletions

View File

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

View File

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

View File

@ -9,6 +9,8 @@ import (
var ( var (
ErrObjectReleased = errors.New("Object already released.") ErrObjectReleased = errors.New("Object already released.")
ErrBadConfiguration = errors.New("Bad configuration.") 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. // Releasable interface is for those types that can release its members.

View File

@ -5,6 +5,7 @@ package loader
import ( import (
"encoding/json" "encoding/json"
"github.com/v2ray/v2ray-core/common"
"github.com/v2ray/v2ray-core/common/log" "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] rawID, found := obj[this.idKey]
if !found { if !found {
log.Error(this.idKey, " not found in JSON content.") log.Error(this.idKey, " not found in JSON content.")
return nil, "", ErrConfigIDKeyNotFound return nil, "", common.ErrObjectNotFound
} }
var id string var id string
if err := json.Unmarshal(rawID, &id); err != nil { 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] configValue, found := obj[this.configKey]
if !found { if !found {
log.Error(this.configKey, " not found in JSON content.") log.Error(this.configKey, " not found in JSON content.")
return nil, "", ErrConfigIDKeyNotFound return nil, "", common.ErrObjectNotFound
} }
rawConfig = configValue rawConfig = configValue
} }

View File

@ -2,11 +2,10 @@ package loader
import ( import (
"errors" "errors"
"github.com/v2ray/v2ray-core/common"
) )
var ( 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.")
) )
@ -31,7 +30,7 @@ func NewBaseConfigLoader() *BaseConfigLoader {
func (this *BaseConfigLoader) RegisterCreator(id string, creator ConfigCreator) error { func (this *BaseConfigLoader) RegisterCreator(id string, creator ConfigCreator) error {
if _, found := this.creators[id]; found { if _, found := this.creators[id]; found {
return ErrConfigIDExists return common.ErrDuplicatedName
} }
this.creators[id] = creator this.creators[id] = creator

View File

@ -1,9 +1,8 @@
package registry package registry
import ( import (
"errors"
"github.com/v2ray/v2ray-core/app" "github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/common"
"github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/transport/internet" "github.com/v2ray/v2ray-core/transport/internet"
) )
@ -11,14 +10,11 @@ import (
var ( var (
inboundFactories = make(map[string]InboundHandlerFactory) inboundFactories = make(map[string]InboundHandlerFactory)
outboundFactories = make(map[string]OutboundHandlerFactory) 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 { func RegisterInboundHandlerCreator(name string, creator InboundHandlerFactory) error {
if _, found := inboundFactories[name]; found { if _, found := inboundFactories[name]; found {
return ErrNameExists return common.ErrDuplicatedName
} }
inboundFactories[name] = creator inboundFactories[name] = creator
return nil return nil
@ -32,7 +28,7 @@ func MustRegisterInboundHandlerCreator(name string, creator InboundHandlerFactor
func RegisterOutboundHandlerCreator(name string, creator OutboundHandlerFactory) error { func RegisterOutboundHandlerCreator(name string, creator OutboundHandlerFactory) error {
if _, found := outboundFactories[name]; found { if _, found := outboundFactories[name]; found {
return ErrNameExists return common.ErrDuplicatedName
} }
outboundFactories[name] = creator outboundFactories[name] = creator
return nil 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) { func CreateInboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
creator, found := inboundFactories[name] creator, found := inboundFactories[name]
if !found { if !found {
return nil, ErrProxyNotFound return nil, common.ErrObjectNotFound
} }
if meta.StreamSettings == nil { if meta.StreamSettings == nil {
meta.StreamSettings = &internet.StreamSettings{ 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) { func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
creator, found := outboundFactories[name] creator, found := outboundFactories[name]
if !found { if !found {
return nil, ErrProxyNotFound return nil, common.ErrObjectNotFound
} }
if meta.StreamSettings == nil { if meta.StreamSettings == nil {
meta.StreamSettings = &internet.StreamSettings{ meta.StreamSettings = &internet.StreamSettings{

View File

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

View File

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