github.com/ipld/go-ipld-prime@v0.21.0/fluent/fluentRecover.go (about)

     1  package fluent
     2  
     3  type Error struct {
     4  	Err error
     5  }
     6  
     7  func (e Error) Error() string {
     8  	return e.Err.Error()
     9  }
    10  
    11  // Recover invokes a function within a panic-recovering context, and returns
    12  // any raised fluent.Error values; any other values are re-panicked.
    13  //
    14  // This can be useful for writing large blocks of code using fluent nodes,
    15  // and handling any errors at once at the end.
    16  func Recover(fn func()) (err error) {
    17  	defer func() {
    18  		ei := recover()
    19  		switch e2 := ei.(type) {
    20  		case nil:
    21  			return
    22  		case Error:
    23  			err = e2
    24  		default:
    25  			panic(ei)
    26  		}
    27  	}()
    28  	fn()
    29  	return
    30  }