1package panictoerr23import (4 "errors"56 "gno.land/p/nt/ufmt/v0"7)89// PanicToError executes a function that might panic and, if it does,10// recovers the panic and converts it to an error.11func PanicToError(mightPanic func()) (err error) {12 // Catch any panic that might occur and convert it to an error.13 defer func() {14 if r := recover(); r != nil {15 err = anyToError(r)16 }17 }()1819 // Execute the function that might panic.20 mightPanic()2122 return nil23}2425// AbortToError executes a function that might abort, and if it does,26// revives the abort and converts it to an error.27func AbortToError(mightAbort func()) error {28 // Catch any abort that might occur and convert it to an error.29 if r := revive(mightAbort); r != nil {30 return anyToError(r)31 }3233 return nil34}3536// PanicAbortToError executes a function that might either panic or abort,37// and if it does, it recovers the panic or revives the abort and converts38// it to an error.39func PanicAbortToError(mightPanicOrAbort func()) error {40 var panicErr error4142 // Catch any panic or abort that might occur and convert it to an error.43 if abortErr := AbortToError(func() {44 panicErr = PanicToError(mightPanicOrAbort)45 }); abortErr != nil {46 return abortErr47 }4849 return panicErr50}5152// anyToError converts any value to an error.53func anyToError(v any) error {54 switch v := v.(type) {55 case string:56 return errors.New(v)57 case error:58 return v59 default:60 return errors.New(ufmt.Sprint(v))61 }62}63Signatures reconstructed verbatim from vm/qfuncs — interface params keep their inline definitions.