1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-28 10:15:23 +00:00

Remove use of panic

This commit is contained in:
V2Ray 2015-10-07 10:05:11 +02:00
parent 4f166ccb2c
commit a743f7b46b
3 changed files with 32 additions and 7 deletions

View File

@ -117,3 +117,17 @@ func NewInvalidOperationError(operation string) InvalidOperationError {
func (r InvalidOperationError) Error() string { func (r InvalidOperationError) Error() string {
return r.Prefix() + "Invalid operation: " + r.Operation return r.Prefix() + "Invalid operation: " + r.Operation
} }
type BadConfigurationError struct {
ErrorCode
}
var badConfigurationErrorInstance = BadConfigurationError{ErrorCode: 6}
func NewBadConfigurationError() BadConfigurationError {
return badConfigurationErrorInstance
}
func (r BadConfigurationError) Error() string {
return r.Prefix() + "Bad configuration."
}

View File

@ -1,6 +1,7 @@
package core package core
import ( import (
"github.com/v2ray/v2ray-core/common/errors"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/config" "github.com/v2ray/v2ray-core/config"
@ -38,7 +39,8 @@ func NewPoint(pConfig config.PointConfig) (*Point, error) {
ichFactory, ok := inboundFactories[pConfig.InboundConfig().Protocol()] ichFactory, ok := inboundFactories[pConfig.InboundConfig().Protocol()]
if !ok { if !ok {
panic(log.Error("Unknown inbound connection handler factory %s", pConfig.InboundConfig().Protocol())) log.Error("Unknown inbound connection handler factory %s", pConfig.InboundConfig().Protocol())
return nil, errors.NewBadConfigurationError()
} }
ichConfig := pConfig.InboundConfig().Settings(config.TypeInbound) ichConfig := pConfig.InboundConfig().Settings(config.TypeInbound)
ich, err := ichFactory.Create(vpoint, ichConfig) ich, err := ichFactory.Create(vpoint, ichConfig)
@ -50,7 +52,8 @@ func NewPoint(pConfig config.PointConfig) (*Point, error) {
ochFactory, ok := outboundFactories[pConfig.OutboundConfig().Protocol()] ochFactory, ok := outboundFactories[pConfig.OutboundConfig().Protocol()]
if !ok { if !ok {
panic(log.Error("Unknown outbound connection handler factory %s", pConfig.OutboundConfig().Protocol)) log.Error("Unknown outbound connection handler factory %s", pConfig.OutboundConfig().Protocol())
return nil, errors.NewBadConfigurationError()
} }
ochConfig := pConfig.OutboundConfig().Settings(config.TypeOutbound) ochConfig := pConfig.OutboundConfig().Settings(config.TypeOutbound)
och, err := ochFactory.Create(vpoint, ochConfig) och, err := ochFactory.Create(vpoint, ochConfig)
@ -83,7 +86,8 @@ type OutboundConnectionHandler interface {
// In the case of any errors, the state of the server is unpredicatable. // In the case of any errors, the state of the server is unpredicatable.
func (vp *Point) Start() error { func (vp *Point) Start() error {
if vp.port <= 0 { if vp.port <= 0 {
return log.Error("Invalid port %d", vp.port) log.Error("Invalid port %d", vp.port)
return errors.NewBadConfigurationError()
} }
err := vp.ich.Listen(vp.port) err := vp.ich.Listen(vp.port)

View File

@ -17,7 +17,7 @@ import (
var ( var (
configFile = flag.String("config", "", "Config file for this Point server.") configFile = flag.String("config", "", "Config file for this Point server.")
logLevel = flag.String("loglevel", "", "Level of log info to be printed to console, available value: debug, info, warning, error") logLevel = flag.String("loglevel", "warning", "Level of log info to be printed to console, available value: debug, info, warning, error")
version = flag.Bool("version", false, "Show current version of V2Ray.") version = flag.Bool("version", false, "Show current version of V2Ray.")
) )
@ -40,24 +40,31 @@ func main() {
log.SetLogLevel(log.WarningLevel) log.SetLogLevel(log.WarningLevel)
case "error": case "error":
log.SetLogLevel(log.ErrorLevel) log.SetLogLevel(log.ErrorLevel)
default:
fmt.Println("Unknown log level: " + *logLevel)
return
} }
if configFile == nil || len(*configFile) == 0 { if configFile == nil || len(*configFile) == 0 {
panic(log.Error("Config file is not set.")) log.Error("Config file is not set.")
return
} }
config, err := jsonconf.LoadConfig(*configFile) config, err := jsonconf.LoadConfig(*configFile)
if err != nil { if err != nil {
panic(log.Error("Failed to read config file (%s): %v", *configFile, err)) log.Error("Failed to read config file (%s): %v", *configFile, err)
return
} }
vPoint, err := core.NewPoint(config) vPoint, err := core.NewPoint(config)
if err != nil { if err != nil {
panic(log.Error("Failed to create Point server: %v", err)) log.Error("Failed to create Point server: %v", err)
return
} }
err = vPoint.Start() err = vPoint.Start()
if err != nil { if err != nil {
log.Error("Error starting Point server: %v", err) log.Error("Error starting Point server: %v", err)
return
} }
finish := make(chan bool) finish := make(chan bool)