Commit aff91f7b authored by Daniel Martí's avatar Daniel Martí

fluent/qp: don't panic on string panics

Since qp uses panics as an error mechanism, the top-level functions
recover those errors to return them normally.

However, this blows up if, internally, an ipld.Node implementation
panics with a value whose type is not error. For example, with:

	panic("some panic message")

One gets:

	panic: some panic message [recovered]
		panic: interface conversion: string is not error: missing method Error

Fix this by having a fallback for non-error panic values.
parent 949d1675
......@@ -2,6 +2,8 @@
package qp
import (
"fmt"
"github.com/ipld/go-ipld-prime"
)
......@@ -10,7 +12,12 @@ type Assemble = func(ipld.NodeAssembler)
func BuildMap(np ipld.NodePrototype, sizeHint int64, fn func(ipld.MapAssembler)) (_ ipld.Node, err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
if rerr, ok := r.(error); ok {
err = rerr
} else {
// A reasonable fallback, for e.g. strings.
err = fmt.Errorf("%v", r)
}
}
}()
nb := np.NewBuilder()
......@@ -49,7 +56,12 @@ func MapEntry(ma ipld.MapAssembler, k string, fn Assemble) {
func BuildList(np ipld.NodePrototype, sizeHint int64, fn func(ipld.ListAssembler)) (_ ipld.Node, err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
if rerr, ok := r.(error); ok {
err = rerr
} else {
// A reasonable fallback, for e.g. strings.
err = fmt.Errorf("%v", r)
}
}
}()
nb := np.NewBuilder()
......
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