mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 23:51:44 +08:00
feat: add option to disable panic recovery in message processing
This commit is contained in:
parent
6a3ba3d613
commit
e9100152e7
@ -212,7 +212,7 @@ func CreateApp(appoptions *options.App) (*App, error) {
|
|||||||
|
|
||||||
eventHandler := runtime.NewEvents(myLogger)
|
eventHandler := runtime.NewEvents(myLogger)
|
||||||
ctx = context.WithValue(ctx, "events", eventHandler)
|
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
|
// Create the frontends and register to event handler
|
||||||
desktopFrontend := desktop.NewFrontend(ctx, appoptions, myLogger, appBindings, messageDispatcher)
|
desktopFrontend := desktop.NewFrontend(ctx, appoptions, myLogger, appBindings, messageDispatcher)
|
||||||
|
@ -82,7 +82,7 @@ func CreateApp(appoptions *options.App) (*App, error) {
|
|||||||
ctx = context.WithValue(ctx, "buildtype", "production")
|
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)
|
appFrontend := desktop.NewFrontend(ctx, appoptions, myLogger, appBindings, messageDispatcher)
|
||||||
eventHandler.AddFrontend(appFrontend)
|
eventHandler.AddFrontend(appFrontend)
|
||||||
|
|
||||||
|
@ -17,9 +17,10 @@ type Dispatcher struct {
|
|||||||
bindingsDB *binding.DB
|
bindingsDB *binding.DB
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
errfmt options.ErrorFormatter
|
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{
|
return &Dispatcher{
|
||||||
log: log,
|
log: log,
|
||||||
bindings: bindings,
|
bindings: bindings,
|
||||||
@ -27,10 +28,12 @@ func NewDispatcher(ctx context.Context, log *logger.Logger, bindings *binding.Bi
|
|||||||
bindingsDB: bindings.DB(),
|
bindingsDB: bindings.DB(),
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
errfmt: errfmt,
|
errfmt: errfmt,
|
||||||
|
disablePanicRecovery: disablePanicRecovery,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Dispatcher) ProcessMessage(message string, sender frontend.Frontend) (_ string, err error) {
|
func (d *Dispatcher) ProcessMessage(message string, sender frontend.Frontend) (_ string, err error) {
|
||||||
|
if !d.disablePanicRecovery {
|
||||||
defer func() {
|
defer func() {
|
||||||
if e := recover(); e != nil {
|
if e := recover(); e != nil {
|
||||||
if errPanic, ok := e.(error); ok {
|
if errPanic, ok := e.(error); ok {
|
||||||
@ -43,6 +46,7 @@ func (d *Dispatcher) ProcessMessage(message string, sender frontend.Frontend) (_
|
|||||||
d.log.Error("process message error: %s -> %s", message, err)
|
d.log.Error("process message error: %s -> %s", message, err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
if message == "" {
|
if message == "" {
|
||||||
return "", errors.New("No message to process")
|
return "", errors.New("No message to process")
|
||||||
|
@ -98,6 +98,9 @@ type App struct {
|
|||||||
|
|
||||||
// DragAndDrop options for drag and drop behavior
|
// DragAndDrop options for drag and drop behavior
|
||||||
DragAndDrop *DragAndDrop
|
DragAndDrop *DragAndDrop
|
||||||
|
|
||||||
|
// DisablePanicRecovery disables the panic recovery system in messages processing
|
||||||
|
DisablePanicRecovery bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type ErrorFormatter func(error) any
|
type ErrorFormatter func(error) any
|
||||||
|
@ -487,6 +487,13 @@ services of Apple and Microsoft.
|
|||||||
Name: EnableFraudulentWebsiteDetection<br/>
|
Name: EnableFraudulentWebsiteDetection<br/>
|
||||||
Type: `bool`
|
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
|
### Bind
|
||||||
|
|
||||||
A slice of struct instances defining methods that need to be bound to the frontend.
|
A slice of struct instances defining methods that need to be bound to the frontend.
|
||||||
|
Loading…
Reference in New Issue
Block a user