Skip to content

Commit e910015

Browse files
committed
feat: add option to disable panic recovery in message processing
1 parent 6a3ba3d commit e910015

File tree

5 files changed

+40
-26
lines changed

5 files changed

+40
-26
lines changed

v2/internal/app/app_dev.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func CreateApp(appoptions *options.App) (*App, error) {
212212

213213
eventHandler := runtime.NewEvents(myLogger)
214214
ctx = context.WithValue(ctx, "events", eventHandler)
215-
messageDispatcher := dispatcher.NewDispatcher(ctx, myLogger, appBindings, eventHandler, appoptions.ErrorFormatter)
215+
messageDispatcher := dispatcher.NewDispatcher(ctx, myLogger, appBindings, eventHandler, appoptions.ErrorFormatter, appoptions.DisablePanicRecovery)
216216

217217
// Create the frontends and register to event handler
218218
desktopFrontend := desktop.NewFrontend(ctx, appoptions, myLogger, appBindings, messageDispatcher)

v2/internal/app/app_production.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func CreateApp(appoptions *options.App) (*App, error) {
8282
ctx = context.WithValue(ctx, "buildtype", "production")
8383
}
8484

85-
messageDispatcher := dispatcher.NewDispatcher(ctx, myLogger, appBindings, eventHandler, appoptions.ErrorFormatter)
85+
messageDispatcher := dispatcher.NewDispatcher(ctx, myLogger, appBindings, eventHandler, appoptions.ErrorFormatter, appoptions.DisablePanicRecovery)
8686
appFrontend := desktop.NewFrontend(ctx, appoptions, myLogger, appBindings, messageDispatcher)
8787
eventHandler.AddFrontend(appFrontend)
8888

v2/internal/frontend/dispatcher/dispatcher.go

+28-24
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,42 @@ import (
1111
)
1212

1313
type Dispatcher struct {
14-
log *logger.Logger
15-
bindings *binding.Bindings
16-
events frontend.Events
17-
bindingsDB *binding.DB
18-
ctx context.Context
19-
errfmt options.ErrorFormatter
14+
log *logger.Logger
15+
bindings *binding.Bindings
16+
events frontend.Events
17+
bindingsDB *binding.DB
18+
ctx context.Context
19+
errfmt options.ErrorFormatter
20+
disablePanicRecovery bool
2021
}
2122

22-
func NewDispatcher(ctx context.Context, log *logger.Logger, bindings *binding.Bindings, events frontend.Events, errfmt options.ErrorFormatter) *Dispatcher {
23+
func NewDispatcher(ctx context.Context, log *logger.Logger, bindings *binding.Bindings, events frontend.Events, errfmt options.ErrorFormatter, disablePanicRecovery bool) *Dispatcher {
2324
return &Dispatcher{
24-
log: log,
25-
bindings: bindings,
26-
events: events,
27-
bindingsDB: bindings.DB(),
28-
ctx: ctx,
29-
errfmt: errfmt,
25+
log: log,
26+
bindings: bindings,
27+
events: events,
28+
bindingsDB: bindings.DB(),
29+
ctx: ctx,
30+
errfmt: errfmt,
31+
disablePanicRecovery: disablePanicRecovery,
3032
}
3133
}
3234

3335
func (d *Dispatcher) ProcessMessage(message string, sender frontend.Frontend) (_ string, err error) {
34-
defer func() {
35-
if e := recover(); e != nil {
36-
if errPanic, ok := e.(error); ok {
37-
err = errPanic
38-
} else {
39-
err = fmt.Errorf("%v", e)
36+
if !d.disablePanicRecovery {
37+
defer func() {
38+
if e := recover(); e != nil {
39+
if errPanic, ok := e.(error); ok {
40+
err = errPanic
41+
} else {
42+
err = fmt.Errorf("%v", e)
43+
}
4044
}
41-
}
42-
if err != nil {
43-
d.log.Error("process message error: %s -> %s", message, err)
44-
}
45-
}()
45+
if err != nil {
46+
d.log.Error("process message error: %s -> %s", message, err)
47+
}
48+
}()
49+
}
4650

4751
if message == "" {
4852
return "", errors.New("No message to process")

v2/pkg/options/options.go

+3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ type App struct {
9898

9999
// DragAndDrop options for drag and drop behavior
100100
DragAndDrop *DragAndDrop
101+
102+
// DisablePanicRecovery disables the panic recovery system in messages processing
103+
DisablePanicRecovery bool
101104
}
102105

103106
type ErrorFormatter func(error) any

website/docs/reference/options.mdx

+7
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,13 @@ services of Apple and Microsoft.
487487
Name: EnableFraudulentWebsiteDetection<br/>
488488
Type: `bool`
489489

490+
### DisablePanicRecovery
491+
492+
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`.
493+
494+
Name: DisablePanicRecovery<br/>
495+
Type: `bool`
496+
490497
### Bind
491498

492499
A slice of struct instances defining methods that need to be bound to the frontend.

0 commit comments

Comments
 (0)