Change to Stop State in case of an error occure. fix connection bug

This commit is contained in:
Hiddify
2024-02-06 11:27:57 +01:00
parent 2c1731cbd0
commit ab8430e683
4 changed files with 16 additions and 5 deletions

View File

@@ -122,6 +122,7 @@ func generateConfigFromFile(path string, configOpt config.ConfigOptions) (string
func start(configPath *C.char, disableMemoryLimit bool) (CErr *C.char) {
defer config.DeferPanicToError("start", func(err error) {
CErr = C.CString(err.Error())
stopAndAlert("Unexpected Error!", err)
})
if status != Stopped {
@@ -153,7 +154,7 @@ func startService(delayStart bool) error {
var patchedOptions *option.Options
patchedOptions, err = config.BuildConfig(*configOptions, options)
if err != nil {
return fmt.Errorf("error building config: %w", err)
return stopAndAlert("Error Building Config", err)
}
config.SaveCurrentConfig(filepath.Join(sWorkingPath, "current-config.json"), *patchedOptions)
@@ -219,6 +220,7 @@ func stop() (CErr *C.char) {
func restart(configPath *C.char, disableMemoryLimit bool) (CErr *C.char) {
defer config.DeferPanicToError("restart", func(err error) {
CErr = C.CString(err.Error())
stopAndAlert("Unexpected Error!", err)
})
log.Debug("[Service] Restarting")

View File

@@ -24,7 +24,13 @@ func propagateStatus(newStatus string) {
bridge.SendStringToPort(statusPropagationPort, string(msg))
}
func stopAndAlert(alert string, err error) error {
func stopAndAlert(alert string, err error) (resultErr error) {
defer func() {
if r := recover(); r != nil {
resultErr = fmt.Errorf("panic recovered: %v", r)
}
}()
status = Stopped
message := err.Error()
fmt.Printf("Error: %s: %v\n", alert, err)