Paul Lecuq
36c5d6f2ed
- web service with json support - web page - cron service to update database infos
34 lines
587 B
Go
34 lines
587 B
Go
package safe
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
// Run the function safely knowing that if it panics
|
|
// the panic will be caught and returned as an error
|
|
func Run(fn func()) (err error) {
|
|
return RunE(func() error {
|
|
fn()
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// Run the function safely knowing that if it panics
|
|
// the panic will be caught and returned as an error
|
|
func RunE(fn func() error) (err error) {
|
|
defer func() {
|
|
if err != nil {
|
|
return
|
|
}
|
|
if ex := recover(); ex != nil {
|
|
if e, ok := ex.(error); ok {
|
|
err = e
|
|
return
|
|
}
|
|
err = errors.New(fmt.Sprint(ex))
|
|
}
|
|
}()
|
|
return fn()
|
|
}
|