mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-10 15:52:32 +08:00
[v3] Add PanicHandler application option.
This commit is contained in:
parent
6b59216b32
commit
90e66a7ad4
@ -157,6 +157,25 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func processPanic(value any) {
|
||||||
|
if value == nil {
|
||||||
|
value = fmt.Errorf("unknown error")
|
||||||
|
}
|
||||||
|
if globalApplication.options.PanicHandler != nil {
|
||||||
|
globalApplication.options.PanicHandler(value)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Print the panic details
|
||||||
|
fmt.Printf("Panic occurred: %v", value)
|
||||||
|
|
||||||
|
// Print the stack trace
|
||||||
|
buf := make([]byte, 1<<16)
|
||||||
|
runtime.Stack(buf, true)
|
||||||
|
fmt.Println("Stack trace:")
|
||||||
|
fmt.Println(string(buf))
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
// Messages sent from javascript get routed here
|
// Messages sent from javascript get routed here
|
||||||
type windowMessage struct {
|
type windowMessage struct {
|
||||||
windowId uint
|
windowId uint
|
||||||
@ -365,6 +384,14 @@ func (a *App) NewSystemTray() *SystemTray {
|
|||||||
|
|
||||||
func (a *App) Run() error {
|
func (a *App) Run() error {
|
||||||
a.info("Starting application")
|
a.info("Starting application")
|
||||||
|
|
||||||
|
// Setup panic handler
|
||||||
|
defer func() {
|
||||||
|
if err := recover(); err != nil {
|
||||||
|
processPanic(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
a.impl = newPlatformApp(a)
|
a.impl = newPlatformApp(a)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
@ -683,14 +710,7 @@ func invokeSync(fn func()) {
|
|||||||
globalApplication.dispatchOnMainThread(func() {
|
globalApplication.dispatchOnMainThread(func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
// Print the panic details
|
processPanic(err)
|
||||||
fmt.Println("Panic occurred:", err)
|
|
||||||
|
|
||||||
// Print the stack trace
|
|
||||||
buf := make([]byte, 1<<16)
|
|
||||||
runtime.Stack(buf, true)
|
|
||||||
fmt.Println("Stack trace:")
|
|
||||||
fmt.Println(string(buf))
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
fn()
|
fn()
|
||||||
@ -705,14 +725,7 @@ func invokeSyncWithResult[T any](fn func() T) (res T) {
|
|||||||
globalApplication.dispatchOnMainThread(func() {
|
globalApplication.dispatchOnMainThread(func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
// Print the panic details
|
processPanic(err)
|
||||||
fmt.Println("Panic occurred:", err)
|
|
||||||
|
|
||||||
// Print the stack trace
|
|
||||||
buf := make([]byte, 1<<16)
|
|
||||||
runtime.Stack(buf, true)
|
|
||||||
fmt.Println("Stack trace:")
|
|
||||||
fmt.Println(string(buf))
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
res = fn()
|
res = fn()
|
||||||
@ -728,14 +741,7 @@ func invokeSyncWithError(fn func() error) (err error) {
|
|||||||
globalApplication.dispatchOnMainThread(func() {
|
globalApplication.dispatchOnMainThread(func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
// Print the panic details
|
processPanic(err)
|
||||||
fmt.Println("Panic occurred:", err)
|
|
||||||
|
|
||||||
// Print the stack trace
|
|
||||||
buf := make([]byte, 1<<16)
|
|
||||||
runtime.Stack(buf, true)
|
|
||||||
fmt.Println("Stack trace:")
|
|
||||||
fmt.Println(string(buf))
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
err = fn()
|
err = fn()
|
||||||
@ -751,14 +757,7 @@ func invokeSyncWithResultAndError[T any](fn func() (T, error)) (res T, err error
|
|||||||
globalApplication.dispatchOnMainThread(func() {
|
globalApplication.dispatchOnMainThread(func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
// Print the panic details
|
processPanic(err)
|
||||||
fmt.Println("Panic occurred:", err)
|
|
||||||
|
|
||||||
// Print the stack trace
|
|
||||||
buf := make([]byte, 1<<16)
|
|
||||||
runtime.Stack(buf, true)
|
|
||||||
fmt.Println("Stack trace:")
|
|
||||||
fmt.Println(string(buf))
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
res, err = fn()
|
res, err = fn()
|
||||||
|
@ -172,20 +172,12 @@ func (m *windowsApp) setApplicationMenu(menu *Menu) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *windowsApp) run() error {
|
func (m *windowsApp) run() error {
|
||||||
// Add a hook to the ApplicationDidFinishLaunching event
|
|
||||||
//m.parent.On(events.Mac.ApplicationDidFinishLaunching, func() {
|
|
||||||
// C.setApplicationShouldTerminateAfterLastWindowClosed(C.bool(m.parent.options.Mac.ApplicationShouldTerminateAfterLastWindowClosed))
|
|
||||||
// C.setActivationPolicy(C.int(m.parent.options.Mac.ActivationPolicy))
|
|
||||||
// C.activateIgnoringOtherApps()
|
|
||||||
//})
|
|
||||||
// setup event listeners
|
|
||||||
for eventID := range m.parent.applicationEventListeners {
|
for eventID := range m.parent.applicationEventListeners {
|
||||||
m.on(eventID)
|
m.on(eventID)
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = m.runMainLoop()
|
_ = m.runMainLoop()
|
||||||
|
|
||||||
//C.run()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,6 +21,9 @@ type Options struct {
|
|||||||
Assets AssetOptions
|
Assets AssetOptions
|
||||||
Plugins map[string]Plugin
|
Plugins map[string]Plugin
|
||||||
Flags map[string]any
|
Flags map[string]any
|
||||||
|
|
||||||
|
// PanicHandler is a way to register a custom panic handler
|
||||||
|
PanicHandler func(any)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AssetOptions defines the configuration of the AssetServer.
|
// AssetOptions defines the configuration of the AssetServer.
|
||||||
|
Loading…
Reference in New Issue
Block a user