From 547e30f0255a7dc1a530bfa9c62ef3f5bea082a9 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Tue, 21 Jan 2025 06:44:28 +1100 Subject: [PATCH] Default panic handler should fatal. Update docs and example. --- docs/src/content/docs/guides/panic-handling.mdx | 15 ++++++++++----- v3/examples/panic-handling/main.go | 2 +- v3/pkg/application/application.go | 13 +++++-------- v3/pkg/application/panic_handler.go | 2 +- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/docs/src/content/docs/guides/panic-handling.mdx b/docs/src/content/docs/guides/panic-handling.mdx index 5a38c444e73..f252f0dc36f 100644 --- a/docs/src/content/docs/guides/panic-handling.mdx +++ b/docs/src/content/docs/guides/panic-handling.mdx @@ -69,16 +69,21 @@ If you want to see the full stack trace, you can use the `FullStackTrace` field. ## Default Panic Handler -If you don't specify a custom panic handler, Wails will use its default handler which outputs error information in a formatted log message. For example: +If you don't specify a custom panic handler, Wails will use its default handler which outputs error information in a formatted log message and then quits. +For example: ``` -Jan 16 21:18:05.649 ERR panic error: oh no! something went wrong deep in my service! :( +************************ FATAL ****************************** +* There has been a catastrophic failure in your application * +********************* Error Details ************************* +panic error: oh no! something went wrong deep in my service! :( main.(*WindowService).call2 - at E:/wails/v3/examples/panic-handling/main.go:24 + at E:/wails/v3/examples/panic-handling/main.go:23 main.(*WindowService).call1 - at E:/wails/v3/examples/panic-handling/main.go:20 + at E:/wails/v3/examples/panic-handling/main.go:19 main.(*WindowService).GeneratePanic - at E:/wails/v3/examples/panic-handling/main.go:16 + at E:/wails/v3/examples/panic-handling/main.go:15 +************************************************************* ``` ## Custom Panic Handler diff --git a/v3/examples/panic-handling/main.go b/v3/examples/panic-handling/main.go index 1efcb153ee0..1b702d64faa 100644 --- a/v3/examples/panic-handling/main.go +++ b/v3/examples/panic-handling/main.go @@ -44,6 +44,7 @@ func main() { fmt.Printf("Time: %s\n", panicDetails.Time) fmt.Printf("Error: %s\n", panicDetails.Error) fmt.Printf("Stacktrace: %s\n", panicDetails.StackTrace) + application.InfoDialog().SetMessage("There was a panic!").Show() }, }) @@ -56,5 +57,4 @@ func main() { if err != nil { log.Fatal(err) } - } diff --git a/v3/pkg/application/application.go b/v3/pkg/application/application.go index ec9e34b275c..3f35c37b8e0 100644 --- a/v3/pkg/application/application.go +++ b/v3/pkg/application/application.go @@ -12,7 +12,6 @@ import ( "net/http" "os" "runtime" - "runtime/debug" "strconv" "strings" "sync" @@ -419,13 +418,11 @@ func (a *App) ResetEvents() { func (a *App) handleFatalError(err error) { var buffer strings.Builder - buffer.WriteString("*********************** FATAL ***********************") - buffer.WriteString("There has been a catastrophic failure in your application.") - buffer.WriteString("Please report this error at https://github.com/wailsapp/wails/issues") - buffer.WriteString("******************** Error Details ******************") - buffer.WriteString(fmt.Sprintf("Message: " + err.Error())) - buffer.WriteString(fmt.Sprintf("Stack: " + string(debug.Stack()))) - buffer.WriteString("*********************** FATAL ***********************") + buffer.WriteString("\n\n************************ FATAL ******************************\n") + buffer.WriteString("* There has been a catastrophic failure in your application *\n") + buffer.WriteString("********************* Error Details *************************\n") + buffer.WriteString(err.Error()) + buffer.WriteString("*************************************************************\n") a.handleError(fmt.Errorf(buffer.String())) os.Exit(1) } diff --git a/v3/pkg/application/panic_handler.go b/v3/pkg/application/panic_handler.go index de2d9ab3df9..f1c520b5a5c 100644 --- a/v3/pkg/application/panic_handler.go +++ b/v3/pkg/application/panic_handler.go @@ -103,5 +103,5 @@ func processPanic(panicDetails *PanicDetails) { func defaultPanicHandler(panicDetails *PanicDetails) { errorMessage := fmt.Sprintf("panic error: %s\n%s", panicDetails.Error.Error(), panicDetails.StackTrace) - globalApplication.error(errorMessage) + globalApplication.fatal(errorMessage) }