5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 03:01:45 +08:00

feat: add option to disable panic recovery in message processing

This commit is contained in:
Andrey Pshenkin 2025-03-13 00:06:07 +00:00
parent 6a3ba3d613
commit e9100152e7
No known key found for this signature in database
5 changed files with 40 additions and 26 deletions

View File

@ -212,7 +212,7 @@ func CreateApp(appoptions *options.App) (*App, error) {
eventHandler := runtime.NewEvents(myLogger)
ctx = context.WithValue(ctx, "events", eventHandler)
messageDispatcher := dispatcher.NewDispatcher(ctx, myLogger, appBindings, eventHandler, appoptions.ErrorFormatter)
messageDispatcher := dispatcher.NewDispatcher(ctx, myLogger, appBindings, eventHandler, appoptions.ErrorFormatter, appoptions.DisablePanicRecovery)
// Create the frontends and register to event handler
desktopFrontend := desktop.NewFrontend(ctx, appoptions, myLogger, appBindings, messageDispatcher)

View File

@ -82,7 +82,7 @@ func CreateApp(appoptions *options.App) (*App, error) {
ctx = context.WithValue(ctx, "buildtype", "production")
}
messageDispatcher := dispatcher.NewDispatcher(ctx, myLogger, appBindings, eventHandler, appoptions.ErrorFormatter)
messageDispatcher := dispatcher.NewDispatcher(ctx, myLogger, appBindings, eventHandler, appoptions.ErrorFormatter, appoptions.DisablePanicRecovery)
appFrontend := desktop.NewFrontend(ctx, appoptions, myLogger, appBindings, messageDispatcher)
eventHandler.AddFrontend(appFrontend)

View File

@ -11,38 +11,42 @@ import (
)
type Dispatcher struct {
log *logger.Logger
bindings *binding.Bindings
events frontend.Events
bindingsDB *binding.DB
ctx context.Context
errfmt options.ErrorFormatter
log *logger.Logger
bindings *binding.Bindings
events frontend.Events
bindingsDB *binding.DB
ctx context.Context
errfmt options.ErrorFormatter
disablePanicRecovery bool
}
func NewDispatcher(ctx context.Context, log *logger.Logger, bindings *binding.Bindings, events frontend.Events, errfmt options.ErrorFormatter) *Dispatcher {
func NewDispatcher(ctx context.Context, log *logger.Logger, bindings *binding.Bindings, events frontend.Events, errfmt options.ErrorFormatter, disablePanicRecovery bool) *Dispatcher {
return &Dispatcher{
log: log,
bindings: bindings,
events: events,
bindingsDB: bindings.DB(),
ctx: ctx,
errfmt: errfmt,
log: log,
bindings: bindings,
events: events,
bindingsDB: bindings.DB(),
ctx: ctx,
errfmt: errfmt,
disablePanicRecovery: disablePanicRecovery,
}
}
func (d *Dispatcher) ProcessMessage(message string, sender frontend.Frontend) (_ string, err error) {
defer func() {
if e := recover(); e != nil {
if errPanic, ok := e.(error); ok {
err = errPanic
} else {
err = fmt.Errorf("%v", e)
if !d.disablePanicRecovery {
defer func() {
if e := recover(); e != nil {
if errPanic, ok := e.(error); ok {
err = errPanic
} else {
err = fmt.Errorf("%v", e)
}
}
}
if err != nil {
d.log.Error("process message error: %s -> %s", message, err)
}
}()
if err != nil {
d.log.Error("process message error: %s -> %s", message, err)
}
}()
}
if message == "" {
return "", errors.New("No message to process")

View File

@ -98,6 +98,9 @@ type App struct {
// DragAndDrop options for drag and drop behavior
DragAndDrop *DragAndDrop
// DisablePanicRecovery disables the panic recovery system in messages processing
DisablePanicRecovery bool
}
type ErrorFormatter func(error) any

View File

@ -487,6 +487,13 @@ services of Apple and Microsoft.
Name: EnableFraudulentWebsiteDetection<br/>
Type: `bool`
### DisablePanicRecovery
DisablePanicRecovery disables the automatic recovery from panics in message processing. By default, Wails will recover from panics in message processing and log the error. If you want to handle panics yourself, set this to `true`.
Name: DisablePanicRecovery<br/>
Type: `bool`
### Bind
A slice of struct instances defining methods that need to be bound to the frontend.