5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 07:10:40 +08:00

Override error formatting (#2717)

This commit is contained in:
Ethan Reesor 2023-07-13 05:40:22 -05:00 committed by GitHub
parent 61f7b10f36
commit 22cfcbd5a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 6 deletions

View File

@ -213,7 +213,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) messageDispatcher := dispatcher.NewDispatcher(ctx, myLogger, appBindings, eventHandler, appoptions.ErrorFormatter)
// 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) messageDispatcher := dispatcher.NewDispatcher(ctx, myLogger, appBindings, eventHandler, appoptions.ErrorFormatter)
appFrontend := desktop.NewFrontend(ctx, appoptions, myLogger, appBindings, messageDispatcher) appFrontend := desktop.NewFrontend(ctx, appoptions, myLogger, appBindings, messageDispatcher)
eventHandler.AddFrontend(appFrontend) eventHandler.AddFrontend(appFrontend)

View File

@ -3,8 +3,9 @@ package dispatcher
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/wailsapp/wails/v2/internal/frontend"
"strings" "strings"
"github.com/wailsapp/wails/v2/internal/frontend"
) )
type callMessage struct { type callMessage struct {
@ -49,7 +50,12 @@ func (d *Dispatcher) processCallMessage(message string, sender frontend.Frontend
CallbackID: payload.CallbackID, CallbackID: payload.CallbackID,
} }
if err != nil { if err != nil {
// Use the error formatter if one was provided
if d.errfmt != nil {
callbackMessage.Err = d.errfmt(err)
} else {
callbackMessage.Err = err.Error() callbackMessage.Err = err.Error()
}
} else { } else {
callbackMessage.Result = result callbackMessage.Result = result
} }
@ -66,7 +72,7 @@ func (d *Dispatcher) processCallMessage(message string, sender frontend.Frontend
// CallbackMessage defines a message that contains the result of a call // CallbackMessage defines a message that contains the result of a call
type CallbackMessage struct { type CallbackMessage struct {
Result interface{} `json:"result"` Result interface{} `json:"result"`
Err string `json:"error"` Err any `json:"error"`
CallbackID string `json:"callbackid"` CallbackID string `json:"callbackid"`
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/wailsapp/wails/v2/internal/binding" "github.com/wailsapp/wails/v2/internal/binding"
"github.com/wailsapp/wails/v2/internal/frontend" "github.com/wailsapp/wails/v2/internal/frontend"
"github.com/wailsapp/wails/v2/internal/logger" "github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/pkg/options"
) )
type Dispatcher struct { type Dispatcher struct {
@ -15,15 +16,17 @@ type Dispatcher struct {
events frontend.Events events frontend.Events
bindingsDB *binding.DB bindingsDB *binding.DB
ctx context.Context ctx context.Context
errfmt options.ErrorFormatter
} }
func NewDispatcher(ctx context.Context, log *logger.Logger, bindings *binding.Bindings, events frontend.Events) *Dispatcher { func NewDispatcher(ctx context.Context, log *logger.Logger, bindings *binding.Bindings, events frontend.Events, errfmt options.ErrorFormatter) *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,
} }
} }

View File

@ -64,6 +64,9 @@ type App struct {
Bind []interface{} Bind []interface{}
WindowStartState WindowStartState WindowStartState WindowStartState
// ErrorFormatter overrides the formatting of errors returned by backend methods
ErrorFormatter ErrorFormatter
// CSS property to test for draggable elements. Default "--wails-draggable" // CSS property to test for draggable elements. Default "--wails-draggable"
CSSDragProperty string CSSDragProperty string
@ -90,6 +93,8 @@ type App struct {
Debug Debug Debug Debug
} }
type ErrorFormatter func(error) any
type RGBA struct { type RGBA struct {
R uint8 `json:"r"` R uint8 `json:"r"`
G uint8 `json:"g"` G uint8 `json:"g"`

View File

@ -58,6 +58,7 @@ func main() {
Bind: []interface{}{ Bind: []interface{}{
app, app,
}, },
ErrorFormatter: func(err error) any { return err.Error() },
Windows: &windows.Options{ Windows: &windows.Options{
WebviewIsTransparent: false, WebviewIsTransparent: false,
WindowIsTranslucent: false, WindowIsTranslucent: false,
@ -477,6 +478,14 @@ A slice of struct instances defining methods that need to be bound to the fronte
Name: Bind<br/> Name: Bind<br/>
Type: `[]interface{}` Type: `[]interface{}`
### ErrorFormatter
A function that determines how errors are formatted when returned by a JS-to-Go
method call. The returned value will be marshalled as JSON.
Name: ErrorFormatter<br/>
Type: `func (error) any`
### Windows ### Windows
This defines [Windows specific options](#windows). This defines [Windows specific options](#windows).

View File

@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `-devtools` production build flag. Added by @mmghv in [PR](https://github.com/wailsapp/wails/pull/2725) - Added `-devtools` production build flag. Added by @mmghv in [PR](https://github.com/wailsapp/wails/pull/2725)
- Added `EnableDefaultContextMenu` option to allow enabling the browser's default context-menu in production . Added by @mmghv in [PR](https://github.com/wailsapp/wails/pull/2733) - Added `EnableDefaultContextMenu` option to allow enabling the browser's default context-menu in production . Added by @mmghv in [PR](https://github.com/wailsapp/wails/pull/2733)
- Added smart functionality for the default context-menu in production with CSS styles to control it. Added by @mmghv in [PR](https://github.com/wailsapp/wails/pull/2748) - Added smart functionality for the default context-menu in production with CSS styles to control it. Added by @mmghv in [PR](https://github.com/wailsapp/wails/pull/2748)
- Added custom error formatting to allow passing structured errors back to the frontend.
### Changed ### Changed