Commit caaf28f2 authored by Eric Myhre's avatar Eric Myhre

Impl and tests for fluent.Recover.

Used the ipldfree.Node impl concretely to make the tests work.
In theory any impl should of course work, but it's not useful or
interesting to do a compatibility matrix here -- particularly given
that we use the fluent system in the compataibility tests for all
the other Node impl packages.
Signed-off-by: default avatarEric Myhre <hash@exultant.us>
parent 0d8c9fc4
package fluent
import "fmt"
func Recover(fn func()) error {
return fmt.Errorf("TODO")
// recover, but only handle fluent.Error
// re-raise anything else
// Recover invokes a function within a panic-recovering context, and returns
// any raised fluent.Error values; any other values are re-panicked.
//
// This can be useful for writing large blocks of code using fluent nodes,
// and handling any errors at once at the end.
func Recover(fn func()) (err error) {
defer func() {
ei := recover()
switch e2 := ei.(type) {
case nil:
return
case Error:
err = e2
default:
panic(ei)
}
}()
fn()
return
}
package fluent
import (
"fmt"
"testing"
. "github.com/warpfork/go-wish"
"github.com/ipld/go-ipld-prime/impl/free"
)
func TestRecover(t *testing.T) {
t.Run("simple traversal error should capture", func(t *testing.T) {
Wish(t,
Recover(func() {
WrapNode(&ipldfree.Node{}).TraverseIndex(0).AsString()
t.Fatal("should not be reached")
}),
ShouldEqual,
Error{fmt.Errorf("cannot traverse a node that is undefined")},
)
})
t.Run("correct traversal should return nil", func(t *testing.T) {
Wish(t,
Recover(func() {
n := &ipldfree.Node{}
n.SetString("foo")
WrapNode(n).AsString()
}),
ShouldEqual,
nil,
)
})
t.Run("other panics should continue to rise", func(t *testing.T) {
Wish(t,
func() (r interface{}) {
defer func() { r = recover() }()
Recover(func() {
panic("fuqawds")
})
return
}(),
ShouldEqual,
"fuqawds",
)
})
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment