Skip to content

Commit

Permalink
add stack trace message for rod.Try
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Mar 30, 2022
1 parent 169f186 commit af9d869
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
6 changes: 5 additions & 1 deletion browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,12 @@ func TestTestTry(t *testing.T) {
var errVal *rod.ErrTry
g.True(errors.As(err, &errVal))
g.Is(err, &rod.ErrTry{})
g.Eq(errVal.Unwrap().Error(), "1")
g.Eq(1, errVal.Value)
g.Eq(errVal.Error(), "error value: 1")
g.Has(errVal.Error(), "error value: 1\ngoroutine")

errVal = rod.Try(func() { panic(errors.New("t")) }).(*rod.ErrTry)
g.Eq(errVal.Unwrap().Error(), "t")
}

func TestBrowserOthers(t *testing.T) {
Expand Down
11 changes: 10 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,26 @@ import (
// ErrTry error
type ErrTry struct {
Value interface{}
Stack string
}

func (e *ErrTry) Error() string {
return fmt.Sprintf("error value: %#v", e.Value)
return fmt.Sprintf("error value: %#v\n%s", e.Value, e.Stack)
}

// Is interface
func (e *ErrTry) Is(err error) bool {
return reflect.TypeOf(e) == reflect.TypeOf(err)
}

// Unwrap stdlib interface
func (e *ErrTry) Unwrap() error {
if err, ok := e.Value.(error); ok {
return err
}
return fmt.Errorf("%v", e.Value)
}

// ErrExpectElement error
type ErrExpectElement struct {
*proto.RuntimeRemoteObject
Expand Down
9 changes: 3 additions & 6 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"path/filepath"
"reflect"
"regexp"
"runtime/debug"
"sync"
"time"

Expand Down Expand Up @@ -204,15 +205,11 @@ func (sr *StreamReader) Close() error {
return proto.IOClose{Handle: sr.handle}.Call(sr.c)
}

// Try try fn with recover, return the panic as value
// Try try fn with recover, return the panic as rod.ErrTry
func Try(fn func()) (err error) {
defer func() {
if val := recover(); val != nil {
var ok bool
err, ok = val.(error)
if !ok {
err = &ErrTry{val}
}
err = &ErrTry{val, string(debug.Stack())}
}
}()

Expand Down

0 comments on commit af9d869

Please sign in to comment.