atexit: implement OnError for cancelling multpart uploads

This commit is contained in:
Nick Craig-Wood 2020-06-04 12:02:48 +01:00
parent fcc2db8093
commit 54fda3422e

View file

@ -77,3 +77,23 @@ func Run() {
}
})
}
// OnError registers fn with atexit and returns a function which
// runs fn() if *perr != nil and deregisters fn
//
// It should be used in a defer statement normally so
//
// defer OnError(&err, cancelFunc)()
//
// So cancelFunc will be run if the function exits with an error or
// at exit.
func OnError(perr *error, fn func()) func() {
handle := Register(fn)
return func() {
defer Unregister(handle)
if *perr != nil {
fn()
}
}
}