Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Playground

gnomod.toml

module = "gno.land/p/aeddi/panictoerr/v0"
gno = "0.9"

[addpkg]
  creator = "g1edq4dugw0sgat4zxcw9xardvuydqf6cgleuc8p"

panictoerr.gno

package panictoerr

import (
	"errors"

	"gno.land/p/nt/ufmt/v0"
)

// PanicToError executes a function that might panic and, if it does,
// recovers the panic and converts it to an error.
func PanicToError(mightPanic func()) (err error) {
	// Catch any panic that might occur and convert it to an error.
	defer func() {
		if r := recover(); r != nil {
			err = anyToError(r)
		}
	}()

	// Execute the function that might panic.
	mightPanic()

	return nil
}

// AbortToError executes a function that might abort, and if it does,
// revives the abort and converts it to an error.
func AbortToError(mightAbort func()) error {
	// Catch any abort that might occur and convert it to an error.
	if r := revive(mightAbort); r != nil {
		return anyToError(r)
	}

	return nil
}

// PanicAbortToError executes a function that might either panic or abort,
// and if it does, it recovers the panic or revives the abort and converts
// it to an error.
func PanicAbortToError(mightPanicOrAbort func()) error {
	var panicErr error

	// Catch any panic or abort that might occur and convert it to an error.
	if abortErr := AbortToError(func() {
		panicErr = PanicToError(mightPanicOrAbort)
	}); abortErr != nil {
		return abortErr
	}

	return panicErr
}

// anyToError converts any value to an error.
func anyToError(v any) error {
	switch v := v.(type) {
	case string:
		return errors.New(v)
	case error:
		return v
	default:
		return errors.New(ufmt.Sprint(v))
	}
}

panictoerr_test.gno

package panictoerr_test

import (
	"errors"
	"testing"

	pte "gno.land/p/aeddi/panictoerr/v0"
	"gno.land/p/nt/uassert/v0"
	grc20 "gno.land/r/demo/defi/grc20factory"
)

// cur is a zero-value realm used as a placeholder when forwarding to
// uassert/urequire dispatch helpers that gained an `rlm realm` param.
// These tests pass `func()` callbacks (no crossing inside the callback),
// so rlm is ignored — a nil realm here is safe.
var cur realm

// Test PanicToError with different types as panic value.
func TestSimplePanicToError(t *testing.T) {
	err := pte.PanicToError(func() {
		panic("string")
	})
	uassert.Equal(t, err.Error(), "string")

	err = pte.PanicToError(func() {
		panic(errors.New("error"))
	})
	uassert.Equal(t, err.Error(), "error")

	err = pte.PanicToError(func() {
		panic(42)
	})
	uassert.Equal(t, err.Error(), "42")
}

func TestRealmPanicToError(cur realm, t *testing.T) {
	// Set a test realm to be able to call a realm.
	testRealm := testing.NewCodeRealm("gno.land/r/aeddi/panictoerr/test")
	testing.SetRealm(testRealm)

	const message = "token instance does not exist"
	var err error

	// Define a panicking function (local panic; does not cross any
	// realm boundary). Under the unified declaring-realm borrow,
	// calling grc20.Bank() would borrow into /r/demo/defi/grc20factory
	// and any panic there would cross a realm boundary, becoming an
	// abort catchable only by revive() — see the `aborting` case
	// below. To exercise the pure recover() path, panic locally.
	panicking := func() {
		panic(message)
	}

	// panicking function should panic.
	uassert.PanicsWithMessage(t, cur, message, panicking)

	// panicking function should panic when wrapped in AbortToError.
	uassert.PanicsWithMessage(t, cur, message, func() { pte.AbortToError(panicking) })

	// panicking function should not panic when wrapped in PanicToError.
	uassert.NotPanics(
		t, cur,
		func() { err = pte.PanicToError(panicking) },
		"panicking function should not panic when wrapped in PanicToError",
	)
	uassert.Equal(t, err.Error(), message)

	// panicking function should not panic when wrapped in PanicAbortToError.
	uassert.NotPanics(
		t, cur,
		func() { err = pte.PanicAbortToError(panicking) },
		"panicking function should not panic when wrapped in PanicAbortToError",
	)
	uassert.Equal(t, err.Error(), message)

	// Define an aborting function (crossing).
	aborting := func() {
		grc20.Faucet(cross(cur), "unknown")
	}

	// aborting function should abort.
	uassert.AbortsWithMessage(t, cur, message, aborting)

	// aborting function should abort when wrapped in PanicToError.
	uassert.AbortsWithMessage(t, cur, message, func() { pte.PanicToError(aborting) })

	// aborting function should not abort when wrapped in AbortToError.
	uassert.NotAborts(
		t, cur,
		func() { err = pte.AbortToError(aborting) },
		"aborting function should not abort when wrapped in AbortToError",
	)
	uassert.Equal(t, err.Error(), message)

	// aborting function should not abort when wrapped in PanicAbortToError.
	uassert.NotAborts(
		t, cur,
		func() { err = pte.PanicAbortToError(aborting) },
		"aborting function should not abort when wrapped in PanicAbortToError",
	)
	uassert.Equal(t, err.Error(), message)
}