Add panic recover to ffi

This commit is contained in:
problematicconsumer
2023-10-14 17:22:26 +03:30
parent ac0eddc583
commit 758eca1984
2 changed files with 44 additions and 7 deletions

View File

@@ -3,8 +3,10 @@ package shared
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime/debug"
"github.com/sagernet/sing-box/option"
)
@@ -20,3 +22,10 @@ func SaveCurrentConfig(path string, options option.Options) error {
}
return os.WriteFile(filepath.Join(path, "current-config.json"), buffer.Bytes(), 0777)
}
func DeferPanicToError(name string, err func(error)) {
if r := recover(); r != nil {
s := fmt.Errorf("%s panic: %s\n%s", name, r, string(debug.Stack()))
err(s)
}
}