Skip to content

Commit

Permalink
add ErrSilent wrapper when marshalling arguments and results
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Apr 13, 2024
1 parent b950769 commit d624de4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import "fmt"

const VERSION = "1.0.22"
const REVISION = "b41281e9fca79eb3bbde5e4347ab8f37763bc545+1"
const NUMBER = 176
const REVISION = "b95076921b299cab29e79a1642abf005462fb732+1"
const NUMBER = 177

func getRevision() string {
revSuffix := ""
Expand Down
4 changes: 2 additions & 2 deletions runtime/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

const VERSION = "1.0.22"
const REVISION = "b41281e9fca79eb3bbde5e4347ab8f37763bc545+1"
const NUMBER = 176
const REVISION = "b95076921b299cab29e79a1642abf005462fb732+1"
const NUMBER = 177

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down
6 changes: 3 additions & 3 deletions runtime/trace/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func MarshalAnyJSON(v interface{}) ([]byte, error) {
var err error
// mock the encoding json
trap.WithFuncOverride(newTypeEncoder, &trap.Interceptor{
Pre: func(ctx context.Context, f *core.FuncInfo, args, result core.Object) (interface{}, error) {
return nil, nil
},
// Pre: func(ctx context.Context, f *core.FuncInfo, args, result core.Object) (interface{}, error) {
// return nil, nil
// },
Post: func(ctx context.Context, f *core.FuncInfo, args, result core.Object, data interface{}) error {
if f != newTypeEncoder {
return nil
Expand Down
42 changes: 39 additions & 3 deletions runtime/trace/stack.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package trace

import (
"encoding/json"
"fmt"
"time"

"github.com/xhd2015/xgo/runtime/core"
Expand Down Expand Up @@ -30,9 +32,14 @@ type Stack struct {
//
// for example: google.golang.org/protobuf/internal/order
type ExportOptions struct {
// supress error when marshalling

Check warning on line 35 in runtime/trace/stack.go

View workflow job for this annotation

GitHub Actions / build

"supress" should be "suppress".
// arguments and results
DisableErrSilent bool

FilterStack func(stack *StackExport) *StackExport

FilterRoot func(root *RootExport) *RootExport
MarshalJSON func(root *RootExport) ([]byte, error)
MarshalRoot func(root *RootExport) ([]byte, error)
}

func (c *Root) Export(opts *ExportOptions) *RootExport {
Expand Down Expand Up @@ -71,12 +78,18 @@ func (c *Stack) Export(opts *ExportOptions) *StackExport {
if c.Error != nil {
errMsg = c.Error.Error()
}
var args interface{} = c.Args
var results interface{} = c.Results
if opts == nil || !opts.DisableErrSilent {
args = &ErrSilent{args}
results = &ErrSilent{results}
}
stack := &StackExport{
FuncInfo: ExportFuncInfo(c.FuncInfo, opts),
Begin: c.Begin,
End: c.End,
Args: c.Args,
Results: c.Results,
Args: args,
Results: results,
Panic: c.Panic,
Error: errMsg,
Children: ((stacks)(c.Children)).Export(opts),
Expand Down Expand Up @@ -111,3 +124,26 @@ func ExportFuncInfo(c *core.FuncInfo, opts *ExportOptions) *FuncInfoExport {
Line: c.Line,
}
}

// make json err silent
type ErrSilent struct {
Data interface{}
}

func (c *ErrSilent) MarshalJSON() (data []byte, err error) {
defer func() {
if e := recover(); e != nil {
if pe, ok := e.(error); ok {
err = pe
} else {
err = fmt.Errorf("panic: %v", e)
}
}
if err != nil {
data = []byte(fmt.Sprintf(`{"error":%q}`, err.Error()))
err = nil
}
}()
data, err = json.Marshal(c.Data)
return
}
4 changes: 2 additions & 2 deletions runtime/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ func fmtStack(root *Root, opts *ExportOptions) (data []byte, err error) {
if opts.FilterRoot != nil {
exportRoot = opts.FilterRoot(exportRoot)
}
if opts.MarshalJSON != nil {
return opts.MarshalJSON(exportRoot)
if opts.MarshalRoot != nil {
return opts.MarshalRoot(exportRoot)
}
}
return MarshalAnyJSON(exportRoot)
Expand Down

0 comments on commit d624de4

Please sign in to comment.