Skip to content

Commit

Permalink
Default panic handler should fatal. Update docs and example.
Browse files Browse the repository at this point in the history
  • Loading branch information
leaanthony committed Jan 20, 2025
1 parent b7ab650 commit 547e30f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
15 changes: 10 additions & 5 deletions docs/src/content/docs/guides/panic-handling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion v3/examples/panic-handling/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
})

Expand All @@ -56,5 +57,4 @@ func main() {
if err != nil {
log.Fatal(err)
}

}
13 changes: 5 additions & 8 deletions v3/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"net/http"
"os"
"runtime"
"runtime/debug"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion v3/pkg/application/panic_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 547e30f

Please sign in to comment.