Przeglądaj źródła

Remove use of panic

V2Ray 10 lat temu
rodzic
commit
a743f7b46b
3 zmienionych plików z 32 dodań i 7 usunięć
  1. 14 0
      common/errors/errors.go
  2. 7 3
      point.go
  3. 11 4
      release/server/main.go

+ 14 - 0
common/errors/errors.go

@@ -117,3 +117,17 @@ func NewInvalidOperationError(operation string) InvalidOperationError {
 func (r InvalidOperationError) Error() string {
 	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."
+}

+ 7 - 3
point.go

@@ -1,6 +1,7 @@
 package core
 
 import (
+	"github.com/v2ray/v2ray-core/common/errors"
 	"github.com/v2ray/v2ray-core/common/log"
 	v2net "github.com/v2ray/v2ray-core/common/net"
 	"github.com/v2ray/v2ray-core/config"
@@ -38,7 +39,8 @@ func NewPoint(pConfig config.PointConfig) (*Point, error) {
 
 	ichFactory, ok := inboundFactories[pConfig.InboundConfig().Protocol()]
 	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)
 	ich, err := ichFactory.Create(vpoint, ichConfig)
@@ -50,7 +52,8 @@ func NewPoint(pConfig config.PointConfig) (*Point, error) {
 
 	ochFactory, ok := outboundFactories[pConfig.OutboundConfig().Protocol()]
 	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)
 	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.
 func (vp *Point) Start() error {
 	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)

+ 11 - 4
release/server/main.go

@@ -17,7 +17,7 @@ import (
 
 var (
 	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.")
 )
 
@@ -40,24 +40,31 @@ func main() {
 		log.SetLogLevel(log.WarningLevel)
 	case "error":
 		log.SetLogLevel(log.ErrorLevel)
+	default:
+		fmt.Println("Unknown log level: " + *logLevel)
+		return
 	}
 
 	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)
 	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)
 	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()
 	if err != nil {
 		log.Error("Error starting Point server: %v", err)
+		return
 	}
 
 	finish := make(chan bool)