5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 22:49:31 +08:00

Merge pull request #4136 from APshenkin/feature/allow-disable-panic-recovery

Add option to disable panic recovery in message processing
This commit is contained in:
Lea Anthony 2025-04-18 15:44:02 +10:00 committed by GitHub
commit 467bbfc4e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 41 additions and 26 deletions

View File

@ -221,7 +221,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)

View File

@ -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)

View File

@ -11,38 +11,42 @@ import (
) )
type Dispatcher struct { type Dispatcher struct {
log *logger.Logger log *logger.Logger
bindings *binding.Bindings bindings *binding.Bindings
events frontend.Events events frontend.Events
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,
events: events, events: events,
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) {
defer func() { if !d.disablePanicRecovery {
if e := recover(); e != nil { defer func() {
if errPanic, ok := e.(error); ok { if e := recover(); e != nil {
err = errPanic if errPanic, ok := e.(error); ok {
} else { err = errPanic
err = fmt.Errorf("%v", e) } else {
err = fmt.Errorf("%v", e)
}
} }
} if err != nil {
if err != nil { 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")

View File

@ -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

View File

@ -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.

View File

@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- Added "Branding" section to `wails doctor` to correctly identify Windows 11 [#3891](https://github.com/wailsapp/wails/pull/3891) by [@ronen25](https://github.com/ronen25) - Added "Branding" section to `wails doctor` to correctly identify Windows 11 [#3891](https://github.com/wailsapp/wails/pull/3891) by [@ronen25](https://github.com/ronen25)
- Added `-skipembedcreate` flag to build and dev command to improve compile and recompile speed [#4143](https://github.com/wailsapp/wails/pull/4143) by @josStorer - Added `-skipembedcreate` flag to build and dev command to improve compile and recompile speed [#4143](https://github.com/wailsapp/wails/pull/4143) by @josStorer
- Added `DisablePanicRecovery` option to allow handle panics manually [#4136](https://github.com/wailsapp/wails/pull/4136) by [@APshenkin](https://github.com/APshenkin)
### Fixed ### Fixed
- Fixed typescript generation of maps with key of array of structs by @joshuapare in [#4209](https://github.com/wailsapp/wails/pull/4209) - Fixed typescript generation of maps with key of array of structs by @joshuapare in [#4209](https://github.com/wailsapp/wails/pull/4209)